How do I implement generic commutative std::ops involving a builtin type for trait objects?

前端 未结 1 1435
没有蜡笔的小新
没有蜡笔的小新 2021-01-19 01:11

I have:

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

pub trait Hilbert:
    Add + Sub + Mul + Div + Neg + Mul + Div

        
1条回答
  •  梦毁少年i
    2021-01-19 01:17

    Why is this disallowed?

    Rust enforces a policy that an implementation must be defined in the same crate as either the trait or the type. Neither Mul nor f64 are in your crate.

    This prevents ambiguity about which implementation is going to be used. It makes it easy for the compiler to enforce that at most one instance of a trait exists per type, since it only has to check the implementations in those crates. If any other crate could define instances then the compiler would have to look everywhere. But also a human, trying to reason about the code, would have to be familiar with every crate, in order to guess which implementation would end up being used. Trait implementations are not named items in Rust, so you couldn't even be explicit about it. Here's some background

    A common workaround is to use a wrapper type. There's zero runtime cost to doing so, but it will make the API a bit more cumbersome.

    You can also define your own numeric trait, which just implies all of Add, Mul etc, implement that for all the primitive types, and use it as the bound in Hilbert instead of all the individual traits.

    But this is going to be messy whichever route you go. And I would question the benefit of using the same operator for scalars, non-scalars and mixed. It would be far simpler to just add a new method to your API:

    fn scale(self, by: f64) -> Self;
    

    Apart from not getting into a complicated mess with all those trait bounds and workarounds, the intent of the code is much clearer. You won't have to look at the types of each variable to distinguish this from a multiplication of two scalars.

    fn g(x: T) -> f64 {
        let a = x.scale(2.0).dot(&x);
        let b = x.scale(2.0).dot(&x);
        a + b
    }
    

    0 讨论(0)
提交回复
热议问题