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