Trait ref_ops::RefNeg

source ·
pub trait RefNeg: Sealed {
    type Output;

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

neg operation through 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 A<T>
where
    &'a T: Neg<Output = O>,
{
    type Output = A<O>;

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

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

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

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

but the following code does:

use core::ops::Neg;
use ref_ops::RefNeg;

struct A<T>(T);


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

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

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

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

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

Required Associated Types

The resulting type after applying neg operation.

Required Methods

Performs neg operation.

Implementors