Trait RefMutSub

Source
pub trait RefMutSub<Rhs = Self>: Sealed<Rhs> {
    type Output;

    // Required method
    fn ref_mut_sub(&mut self, rhs: Rhs) -> Self::Output;
}
Expand description

sub operation through mutable 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 mut A<U>> for &'a mut A<T>
where
    &'a mut T: Sub<&'b mut U>,
{
    type Output = A<<&'a mut T as Sub<&'b mut U>>::Output>;

    fn sub(self, rhs: &'b mut A<U>) -> Self::Output {
        A(self.0.sub(&mut rhs.0))
    }
}

fn _f<T, U>(mut a: T, mut b: U)
where
    for<'a, 'b> &'a mut T: Sub<&'b mut U>,
{
    let _a_op_b = (&mut a).sub(&mut b);

    // to do something with `a`, `b`, and `_a_op_b`
}

fn _g<T, U>(a: T, b: U)
where
    for<'a, 'b> &'a mut T: Sub<&'b mut U>,
{
    _f(a, b);
}

but the following code does:

use core::ops::Sub;
use ref_ops::RefMutSub;

struct A<T>(T);

impl<'a, T, U> Sub<&'a mut A<U>> for &mut A<T>
where
    T: RefMutSub<&'a mut U>,
{
    type Output = A<T::Output>;

    fn sub(self, rhs: &'a mut A<U>) -> Self::Output {
        A(self.0.ref_mut_sub(&mut rhs.0))
    }
}

fn _f<T, U>(mut a: T, mut b: U)
where
    for<'a, 'b> &'a mut T: Sub<&'b mut U>,
{
    let _a_op_b = (&mut a).sub(&mut b);

    // to do something with `a`, `b`, and `_a_op_b`
}

fn _g<T, U>(a: T, b: U)
where
    for<'a, 'b> &'a mut T: Sub<&'b mut U>,
{
    _f(a, b);
}

Required Associated Types§

Source

type Output

The resulting type after applying sub operation.

Required Methods§

Source

fn ref_mut_sub(&mut self, rhs: Rhs) -> Self::Output

Performs sub operation.

Implementors§

Source§

impl<T, Rhs, O> RefMutSub<Rhs> for T
where T: ?Sized, for<'a> &'a mut T: Sub<Rhs, Output = O>,