Trait ref_ops::RefBitOr[][src]

pub trait RefBitOr<Rhs>: BitOr<Rhs> {
    fn ref_bitor(&self, other: Rhs) -> Self::Output;
}
Expand description

An escape hatch for implimenting BitOr for references to newtypes.

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

use core::ops::BitOr;

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

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

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

pub fn f<T, U>(a: T, b: U)
where
    for<'a, 'b> &'a T: BitOr<&'b U>,
{
    let a_b = (&a).bitor(&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: BitOr<&'b U>,
{
    f(a, b);
}

assert!(&A(3) | &A(5) == A(7));

but the following code does:

use core::ops::BitOr;
use ref_ops::RefBitOr;

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

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

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

pub fn f<T, U>(a: T, b: U)
where
    for<'a, 'b> &'a T: BitOr<&'b U>,
{
    let a_b = (&a).bitor(&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: BitOr<&'b U>,
{
    f(a, b);
}

assert!(&A(3) | &A(5) == A(7));

Required methods

Implementors