How can I require that a reference to a generic type can be compared for equality against the generic type?

后端 未结 1 439
执笔经年
执笔经年 2021-01-25 12:27

I\'m trying to implement an algorithm which relies on modular exponentiation. I couldn\'t find any modular exponentiation construct for native types like u64 (only

1条回答
  •  生来不讨喜
    2021-01-25 12:47

    You can either ignore the problem and compare a reference to a reference or non-reference to a non-reference:

    if modulus == &T::one() {
    // Or
    if *modulus == T::one() {
    

    Or you can use higher-ranked trait bounds:

    impl PowM for T
    where
        T: Num + Two + ShrAssign + Rem + PartialOrd,
        for <'a> &'a T: PartialEq,
    {
        // ...
    }
    

    In either case, you need to require that T implements Copy or that it implements Clone and then add appropriate calls to .clone().

    See also:

    • How does for<> syntax differ from a regular lifetime bound?

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