Trait ref_ops::RefBitAnd[][src]

pub trait RefBitAnd<Rhs>: BitAnd<Rhs> {
    fn ref_bitand(&self, other: Rhs) -> Self::Output;
}
Expand description

An escape hatch for implimenting BitAnd for references to newtypes.

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

use core::ops::BitAnd;

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

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

    fn bitand(self, other: &'b A<U>) -> Self::Output {
        A(self.0.bitand(&other.0))
    }
}

pub fn f<T, U>(a: T, b: U)
where
    for<'a, 'b> &'a T: BitAnd<&'b U>,
{
    let a_b = (&a).bitand(&b);

    // to do something with `a`, `b`, and `a_b`
    todo!();
}

pub fn g<T, U>(a: T, b: U)
where
    for<'a, 'b> &'a T: BitAnd<&'b U>,
{
    f(a, b);
}

assert!(&A(6) & &A(5) == A(4));

but the following code does:

use core::ops::BitAnd;
use ref_ops::RefBitAnd;

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

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

    fn bitand(self, other: &'a A<U>) -> Self::Output {
        A(self.0.ref_bitand(&other.0))
    }
}

pub fn f<T, U>(a: T, b: U)
where
    for<'a, 'b> &'a T: BitAnd<&'b U>,
{
    let a_b = (&a).bitand(&b);

    // to do something with `a`, `b`, and `a_b`
    todo!();
}

pub fn g<T, U>(a: T, b: U)
where
    for<'a, 'b> &'a T: BitAnd<&'b U>,
{
    f(a, b);
}

assert!(&A(6) & &A(5) == A(4));

Required methods

Implementors