Expand description
An escape hatch for implimenting
Mul
for references to structs.
As of Rust 1.64.0, the following code does not compile:
ⓘ
use as_is::ToOwned;
use core::ops::Mul;
#[derive(PartialEq)]
struct A<T: ?Sized>(T);
impl<'a, 'b, T: ?Sized + ToOwned, U, O> Mul<&'b A<U>> for &'a A<T>
where
T::Owned: Mul<&'b U, Output = O>,
&'a T: Mul<&'b U, Output = O>,
{
type Output = A<O>;
fn mul(self, rhs: &'b A<U>) -> Self::Output {
A(self.0.mul(&rhs.0))
}
}
fn _f<T, U>(a: T, b: U)
where
for<'a, 'b> &'a T: Mul<&'b U>,
{
let _a_op_b = (&a).mul(&b);
// to do something with `a`, `b`, and `_a_op_b`
todo!();
}
fn _g<T, U>(a: T, b: U)
where
for<'a, 'b> &'a T: Mul<&'b U>,
{
_f(a, b);
}
assert!(&A(2.0) * &A(3.0) == A(6.0));but the following code does:
use as_is::ToOwned;
use as_is::ref_ops::RefMul;
use core::ops::Mul;
#[derive(PartialEq)]
struct A<T: ?Sized>(T);
impl<'a, T: ?Sized + ToOwned, U, O> Mul<&'a A<U>> for &A<T>
where
T::Owned: Mul<&'a U, Output = O>,
T: RefMul<&'a U, Output = O>,
{
type Output = A<O>;
fn mul(self, rhs: &'a A<U>) -> Self::Output {
A(self.0.ref_mul(&rhs.0))
}
}
fn _f<T, U>(a: T, b: U)
where
for<'a, 'b> &'a T: Mul<&'b U>,
{
let _a_op_b = (&a).mul(&b);
// to do something with `a`, `b`, and `_a_op_b`
todo!();
}
fn _g<T, U>(a: T, b: U)
where
for<'a, 'b> &'a T: Mul<&'b U>,
{
_f(a, b);
}
assert!(&A(2.0) * &A(3.0) == A(6.0));