Trait ref_ops::RefMutSub

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

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

sub operation through mutable references.

As of Rust 1.65.0, the following code does not compile:

use core::ops::Sub;

struct A<T>(T);

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

    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

The resulting type after applying sub operation.

Required Methods

Performs sub operation.

Implementors