I'm trying to create a trait which is the amalgamation of other traits, but with their inputs over the borrowed versions of those values instead of taking ownership of them. For this example I reduced it to one.
For instance:
use std::ops::Add;
trait Foo
where
for<'a> &'a Self: Add<Output = Self>,
{
}
It seems like this should create a trait Foo
where it needs Add
to be defined. However, this doesn't seem to work when I attempt to use it.
#[derive(Debug)]
struct Two<T> {
x: T,
y: T,
}
impl<T> Two<T> where T: Foo {}
This doesn't compile with the error:
error[E0277]: cannot add `&'a T` to `&'a T`
--> src/main.rs:15:1
|
15 | impl<T> Two<T> where T: Foo {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ no implementation for `&'a T + &'a T`
|
= help: the trait `for<'a> std::ops::Add` is not implemented for `&'a T`
= help: consider adding a `where for<'a> &'a T: std::ops::Add` bound
note: required by `Foo`
--> src/main.rs:3:3
|
3 | / trait Foo
4 | | where
5 | | for<'a> &'a Self: Add<Output = Self>,
6 | | {
7 | | }
| |_^
Why do I need that clause if T
requires Foo
which itself has that requirement? Is there some other way to create an amalgamation trait to get better efficiency?