简体   繁体   English

如何在特征绑定中指定`std :: ops :: Mul`的预期结果?

[英]How do I specify the expected result of a `std::ops::Mul` in a trait bound?

I have: 我有:

use std::ops::{Add, Div, Mul, Neg, Sub};

pub trait Hilbert: Add + Sub + Mul + Div + Neg + Mul<f64> + Div<f64> + Sized {
    fn dot(&self, other: &Self) -> f64;
    fn magnitude(&self) -> f64;
}

fn g<T: Hilbert>(x: T) -> f64 {
    return (x * 2.0).dot(x);
}

...which yields: ...产生:

error[E0599]: no method named `dot` found for type `<T as std::ops::Mul<f64>>::Output` in the current scope
 --> src/main.rs:9:22
  |
9 |     return (x * 2.0).dot(x);
  |                      ^^^
  |
  = help: items from traits can only be used if the trait is implemented and in scope
  = note: the following trait defines an item `dot`, perhaps you need to implement it:
          candidate #1: `Hilbert`

I interpret this to mean that Rust can't guarantee that the type T , which has trait Hilbert , has an implementation of std::ops::Mul whose ::Output type is equal to T (a Hilbert ). 我的解释是,Rust无法保证具有特征Hilbert T类型具有std::ops::Mul的实现,其::Output类型等于T (一个Hilbert )。

But I know (and / or wish to demand) that this is the case for all Hilbert s, so that functions like g() are possible to write. 但是我知道(和/或希望要求)所有Hilbert都是这种情况,因此可以编写g()类的函数。

I would think to impl std::ops::Mul::Output for Hilbert : 我会觉得IMPL std::ops::Mul::OutputHilbert

impl<T: Hilbert> Mul<f64> for T {
    type Output = T;
}

...but this has the simultaneous problems that (a) I can't "partially implement" a trait, and would be forced to produce a generic implementation of the function Mul::mul() for all Hilberts , but the actual implementation of Mul::mul() will depend on the specific implementation of Hilbert ; ...但是这同时存在一些问题,即(a)我无法“部分实现”特征,并且将被迫为所有Hilberts生成函数Mul::mul()的泛型实现,但实际实现是Mul::mul()取决于Hilbert的具体实现; and (b) it seems I am not allowed to write this trait at all: (b)似乎根本不允许我写这个特征:

error[E0210]: type parameter `T` must be used as the type parameter for some local type (e.g. `MyStruct<T>`); only traits defined in the current crate can be implemented for a type parameter
  --> src/main.rs:12:1
   |
12 | / impl<T: Hilbert> Mul<f64> for T {
13 | |     type Output = T;
14 | | }
   | |_^

How do I persuade Rust that Hilbert * f64 -> Hilbert must hold? 我如何说服Hilbert * f64 > Hilbert必须持有的Rust?

How do I persuade Rust that Hilbert * f64 -> Hilbert must hold? 我如何说服Hilbert * f64 > Hilbert必须持有的Rust?

You add a trait bound <T as Mul<f64>>::Output: Hilbert . 您添加一个以<T as Mul<f64>>::Output: Hilbert绑定的特征。 However, doing so will reveal further issues in your design: 但是,这样做将揭示设计中的其他问题:

  1. Hilbert.dot() takes the second argument as reference, not by value. Hilbert.dot()将第二个参数作为参考,而不是按值。 But changing the relevant line to (x * 2.0).dot(&x) leads to another error: "expected associated type, found type parameter" . 但是将相关行更改为(x * 2.0).dot(&x)会导致另一个错误: “期望的关联类型,找到的类型参数”
  2. This one is because you defined dot to take Self , but there could be different implementations of Hilbert you want to multiply. 这是因为您将dot定义为Self ,但是您可能想要乘以Hilbert不同实现。 dot needs to be generic: fn dot<H: Hilbert>(&self, other: &H) -> f64; dot必须是通用的: fn dot<H: Hilbert>(&self, other: &H) -> f64;
  3. Finally, the borrow checker hits: (x * 2.0).dot(&x) won't let you use x twice, because mul takes its argument by value. 最终,借阅检查器将命中: (x * 2.0).dot(&x)不允许您使用x两次,因为mul会按值取值。 You will either have to add a bound Mul<&'a Self> to be able to pass in a reference (which infects your API with lifetime parameters) or make x cloneable (I don't think copyable would apply). 您要么必须添加绑定的Mul<&'a Self>才能传递参考(这会使用生命周期参数感染您的API),要么使x克隆(我认为可复制并不适用)。

Applying all of the above results in this working(?) compilable code: 将以上所有结果应用于此可工作的可编译代码:

pub trait Hilbert: Add + Sub + Mul + Div + Neg + Mul<f64> + Div<f64> + Sized {
    fn dot<H: Hilbert>(&self, other: &H) -> f64;
    fn magnitude(&self) -> f64;
}

fn g<T: Hilbert + Clone>(x: T) -> f64
where
    <T as Mul<f64>>::Output: Hilbert,
{
    (x.clone() * 2.0).dot(&x)
}

If Hilbert.dot should not be generic because different implementations of Hilbert do not need to interact, the code can be slightly simpler (in terms of trait bounds): 如果由于不同的Hilbert实现不需要交互Hilbert.dot不通用,则代码可以稍微简单一些(就特征界限而言):

pub trait Hilbert:
    Add + Sub + Mul + Div + Neg + Mul<f64, Output = Self> + Div<f64, Output = Self> + Sized
{
    fn dot(&self, other: &Self) -> f64;
    fn magnitude(&self) -> f64;
}

fn g<T: Hilbert + Clone>(x: T) -> f64 {
    (x.clone() * 2.0).dot(&x)
}

However, from what I know about the Hilbert transform, this latter case seems unlikely to be useful. 但是,根据我对希尔伯特变换的了解,后一种情况似乎不太有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未为`T`实现特征`std :: ops :: Fn &lt;(char,)&gt;` - The trait `std::ops::Fn<(char,)>` is not implemented for `T` 如何为类型引用的操作指定通用特征? - How do I specify a generic trait for operations on references to types? 如果在函数内创建引用,如何将泛型类型与需要使用生命周期参数的特征绑定在一起? - How do I bound a generic type with a trait that requires a lifetime parameter if I create the reference inside the function? 如何在另一个泛型类型的特征绑定的类型参数上表达特征? - How can I express a trait bound on a type parameter for another generic type's trait bound? 如何在Scala中的泛型类型上为Float和Double绑定类型? - How do I specify a type bound for Float and Double on a generic type in Scala? 如何为特征的引用指定一个超特征? - How to specify a supertrait for the reference of a trait? 性状受制于普通特征 - Trait bound on generic trait Scala:如何设置通用特征? - Scala: How do I set a generic Trait? 如何在特征本身上写一个特征绑定来引用关联类型? - How to write a trait bound for a reference to an associated type on the trait itself? 我如何要求泛型类型在泛型函数中实现诸如 Add、Sub、Mul 或 Div 之类的操作? - How do I require a generic type implement an operation like Add, Sub, Mul, or Div in a generic function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM