Trait ref_ops::RefMutNeg

source ·
pub trait RefMutNeg: Sealed {
    type Output;

    fn ref_mut_neg(&mut self) -> Self::Output;
}
Expand description

neg operation through mutable references.

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

use core::ops::Neg;

struct A<T>(T);

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

    fn neg(self) -> Self::Output {
        A(self.0.neg())
    }
}

fn _f<T>(mut a: T)
where
    for<'a> &'a mut T: Neg,
{
    let _op_a = (&mut a).neg();

    // to do something with `a` and `_op_a`
}

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

but the following code does:

use core::ops::Neg;
use ref_ops::RefMutNeg;

struct A<T>(T);


impl<T> Neg for &mut A<T>
where
    T: RefMutNeg,
{
    type Output = A<T::Output>;

    fn neg(self) -> Self::Output {
        A(self.0.ref_mut_neg())
    }
}

fn _f<T>(mut a: T)
where
    for<'a> &'a mut T: Neg,
{
    let _op_a = (&mut a).neg();

    // to do something with `a` and `_op_a`
}

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

Required Associated Types

The resulting type after applying neg operation.

Required Methods

Performs neg operation.

Implementors