pub trait RefNeg: Sealed {
type Output;
// Required method
fn ref_neg(&self) -> Self::Output;
}
Expand description
neg
operation through references.
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.neg())
}
}
fn _f<T>(a: T)
where
for<'a> &'a T: Neg,
{
let _op_a = (&a).neg();
// to do something with `a` and `_op_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 _op_a = (&a).neg();
// to do something with `a` and `_op_a`
}
fn _g<T>(a: T)
where
for<'a> &'a T: Neg,
{
_f(a);
}