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