Closed vandenheuvel closed 4 years ago
There are a few supertraits you can choose in various combinations of NumOps
:
Num
is NumOps
and a few other traits, plus a from_str_radix
method.NumRef
is Num + for<'r> NumOps<&'r Self>
, basically adding a reference RHS.RefNum<Base>
is trait that would be implemented for &Base
, requiring NumOps<Base, Base> + for<'r> NumOps<&'r Base, Base>
-- i.e. reference LHS, reference or value RHS.So you could write the full combination something like this:
trait OpsOnBothValueAndRefs: NumRef
where
for<'r> &'r Self: RefNum<Self>,
{}
However, https://github.com/rust-lang/rust/issues/20671 is the reason such a trait isn't already provided. Since the extra where
clause isn't elaborated, everywhere you write T: OpsOnBothValueAndRefs
, you would also have to repeat the for<'r> &'r T: RefNum<T>
, which loses a lot of the value of a combined trait in the first place.
That explains a lot. Thank you very much!
I noticed e.g. this implementation to implement numerical operations on references, in this case
impl<'a, 'b> Add<&'b BigInt> for &'a BigInt
. Is there a trait that allows one to require that such implementations on references exist? E.g. something like below: