pub trait RefSub<Rhs = Self>: Sealed<Rhs> {
type Output;
// Required method
fn ref_sub(&self, rhs: Rhs) -> Self::Output;
}
Expand description
sub
operation through references.
As of Rust 1.73.0, the following code does not compile:
ⓘ
use core::ops::Sub;
struct A<T>(T);
impl<'a, 'b, T, U> Sub<&'b A<U>> for &'a A<T>
where
&'a T: Sub<&'b U>,
{
type Output = A<<&'a T as Sub<&'b U>>::Output>;
fn sub(self, rhs: &'b A<U>) -> Self::Output {
A(self.0.sub(&rhs.0))
}
}
fn _f<T, U>(a: T, b: U)
where
for<'a, 'b> &'a T: Sub<&'b U>,
{
let _a_op_b = (&a).sub(&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: Sub<&'b U>,
{
_f(a, b);
}
but the following code does:
use core::ops::Sub;
use ref_ops::RefSub;
struct A<T>(T);
impl<'a, T, U> Sub<&'a A<U>> for &A<T>
where
T: RefSub<&'a U>,
{
type Output = A<T::Output>;
fn sub(self, rhs: &'a A<U>) -> Self::Output {
A(self.0.ref_sub(&rhs.0))
}
}
fn _f<T, U>(a: T, b: U)
where
for<'a, 'b> &'a T: Sub<&'b U>,
{
let _a_op_b = (&a).sub(&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: Sub<&'b U>,
{
_f(a, b);
}