I'm trying to write code that makes a references a non-Copy
type as needed, while working with the value directly if it is Copy
(because it is a reference). Consider the following example:
struct Wrapper<F>(F);
impl<F> Wrapper<F> {
fn f<'a, G, O>(&'a self, other: &Wrapper<G>) -> O
where for<'r> &'r G: Add<&'a F, Output = O> {
&other.0 + &self.0
}
}
The above code compiles fine. I would now like to call the method (or a similar one) with a few different types. Some are references, some aren't:
// Three different ways of calling
fn g1<'a, T, S>(
x: &'a Wrapper<T>, y: &'a Wrapper<T>,
) -> S
where for<'r> &'r T: Add<&'a T> {
x.f(&y)
}
fn g2<T, S>(
x: &Wrapper<T>, y: &Wrapper<&T>,
) -> S
where for<'r> &'r T: Add<&'r T> {
x.f(&y)
}
fn g3<T, S>(
x: &Wrapper<&T>, y: &Wrapper<&T>,
) -> S
where for<'r> &'r T: Add<&'r T> {
x.f(&y)
}
This doesn't work, as the interface of Wrapper::f
seems to be too restrictive.
Is there a way to write Wrapper::f
such that one can use it with both reference and non-reference types, without requiring an impl on the F <-> &&G
combination?