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