Expand description
An escape hatch for implementing ops traits for references to newtypes.
As of Rust 1.73.0, the following code does not compile:
ⓘ
use core::ops::Neg;
struct A<T>(T);
impl<'a, T> Neg for &'a A<T>
where
&'a T: Neg,
{
type Output = A<<&'a T as Neg>::Output>;
fn neg(self) -> Self::Output {
A(-&self.0)
}
}
fn f<T>(a: T)
where
for<'a> &'a T: Neg,
{
let minus_a = -&a;
// to do something with `a` and `minus_a`
}
fn g<T>(a: T)
where
for<'a> &'a T: Neg,
{
f(a);
}but the following code does:
use core::ops::Neg;
use ref_ops::RefNeg;
struct A<T>(T);
impl<T> Neg for &A<T>
where
T: RefNeg,
{
type Output = A<T::Output>;
fn neg(self) -> Self::Output {
A(self.0.ref_neg())
}
}
fn f<T>(a: T)
where
for<'a> &'a T: Neg,
{
let minus_a = -&a;
// to do something with `a` and `minus_a`
}
fn g<T>(a: T)
where
for<'a> &'a T: Neg,
{
f(a);
}Traits§
- RefAdd
addoperation through references.- RefBit
And bitandoperation through references.- RefBit
Or bitoroperation through references.- RefBit
Xor bitxoroperation through references.- RefDiv
divoperation through references.- RefMul
muloperation through references.- RefMut
Add addoperation through mutable references.- RefMut
BitAnd bitandoperation through mutable references.- RefMut
BitOr bitoroperation through mutable references.- RefMut
BitXor bitxoroperation through mutable references.- RefMut
Div divoperation through mutable references.- RefMut
Mul muloperation through mutable references.- RefMut
Neg negoperation through mutable references.- RefMut
Not notoperation through mutable references.- RefMut
Rem remoperation through mutable references.- RefMut
Shl shloperation through mutable references.- RefMut
Shr shroperation through mutable references.- RefMut
Sub suboperation through mutable references.- RefNeg
negoperation through references.- RefNot
notoperation through references.- RefRem
remoperation through references.- RefShl
shloperation through references.- RefShr
shroperation through references.- RefSub
suboperation through references.