Trait ref_ops::RefMutNeg

source ·
pub trait RefMutNeg: Sealed {
    type Output;

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

neg operation through mutable references.

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

use core::ops::Neg;

struct A<T>(T);

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

    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§

source

type Output

The resulting type after applying neg operation.

Required Methods§

source

fn ref_mut_neg(&mut self) -> Self::Output

Performs neg operation.

Implementors§

source§

impl<T, O> RefMutNeg for Twhere T: ?Sized, for<'a> &'a mut T: Neg<Output = O>,

§

type Output = O