pub trait RefAdd<Rhs = Self>: Sealed<Rhs> {
type Output;
// Required method
fn ref_add(&self, rhs: Rhs) -> Self::Output;
}
Expand description
add
operation through references.
As of Rust 1.73.0, the following code does not compile:
ⓘ
use core::ops::Add;
struct A<T>(T);
impl<'a, 'b, T, U> Add<&'b A<U>> for &'a A<T>
where
&'a T: Add<&'b U>,
{
type Output = A<<&'a T as Add<&'b U>>::Output>;
fn add(self, rhs: &'b A<U>) -> Self::Output {
A(self.0.add(&rhs.0))
}
}
fn _f<T, U>(a: T, b: U)
where
for<'a, 'b> &'a T: Add<&'b U>,
{
let _a_op_b = (&a).add(&b);
// to do something with `a`, `b`, and `_a_op_b`
}
fn _g<T, U>(a: T, b: U)
where
for<'a, 'b> &'a T: Add<&'b U>,
{
_f(a, b);
}
but the following code does:
use core::ops::Add;
use ref_ops::RefAdd;
struct A<T>(T);
impl<'a, T, U> Add<&'a A<U>> for &A<T>
where
T: RefAdd<&'a U>,
{
type Output = A<T::Output>;
fn add(self, rhs: &'a A<U>) -> Self::Output {
A(self.0.ref_add(&rhs.0))
}
}
fn _f<T, U>(a: T, b: U)
where
for<'a, 'b> &'a T: Add<&'b U>,
{
let _a_op_b = (&a).add(&b);
// to do something with `a`, `b`, and `_a_op_b`
}
fn _g<T, U>(a: T, b: U)
where
for<'a, 'b> &'a T: Add<&'b U>,
{
_f(a, b);
}