Expand description
An escape hatch for implimenting
Shr
for references to newtypes.
As of Rust 1.52.1, the following code does not compile:
ⓘ
use core::ops::Shr;
#[derive(PartialEq)]
struct A<T>(T);
impl<'a, 'b, T, U> Shr<&'b A<U>> for &'a A<T>
where
T: Shr<&'b U>,
&'a T: Shr<&'b U, Output = T::Output>,
{
type Output = A<T::Output>;
fn shr(self, other: &'b A<U>) -> Self::Output {
A(self.0.shr(&other.0))
}
}
pub fn f<T, U>(a: T, b: U)
where
for<'a, 'b> &'a T: Shr<&'b U>,
{
let a_b = (&a).shr(&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: Shr<&'b U>,
{
f(a, b);
}
assert!(&A(12) >> &A(2) == A(3));but the following code does:
use core::ops::Shr;
use ref_ops::RefShr;
#[derive(PartialEq)]
struct A<T>(T);
impl<'a, T, U> Shr<&'a A<U>> for &A<T>
where
T: RefShr<&'a U>,
{
type Output = A<T::Output>;
fn shr(self, other: &'a A<U>) -> Self::Output {
A(self.0.ref_shr(&other.0))
}
}
pub fn f<T, U>(a: T, b: U)
where
for<'a, 'b> &'a T: Shr<&'b U>,
{
let a_b = (&a).shr(&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: Shr<&'b U>,
{
f(a, b);
}
assert!(&A(12) >> &A(2) == A(3));