Trait ref_ops::RefNot[][src]

pub trait RefNot: Not {
    fn ref_not(&self) -> Self::Output;
}
Expand description

An escape hatch for implimenting Not for references to newtypes.

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

use core::ops::Not;

#[derive(PartialEq)]
struct A<T>(T);

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

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

fn f<T>(a: T)
where
    for<'a> &'a T: Not,
{
    let not_a = (&a).not();

    // to do something with `a` and `not_a`
    todo!();
}

fn g<T>(a: T)
where
    for<'a> &'a T: Not,
{
    f(a);
}

assert!(!&A(true) == A(false));

but the following code does:

use core::ops::Not;
use ref_ops::RefNot;

#[derive(PartialEq)]
struct A<T>(T);

impl<T> Not for &A<T>
where
    T: RefNot,
{
    type Output = A<T::Output>;

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

fn f<T>(a: T)
where
    for<'a> &'a T: Not,
{
    let not_a = (&a).not();

    // to do something with `a` and `not_a`
    todo!();
}

fn g<T>(a: T)
where
    for<'a> &'a T: Not,
{
    f(a);
}

assert!(!&A(true) == A(false));

Required methods

Implementors