I have written a playground script which demonstrates defining a struct
implementing some standard traits
and then defining a subtrait
for use in function definitions to prevent the verbosity of those trait bounds. I.e. I have:
fn some_op(a: T, b: T, c:T) -> T
where T: RefFieldOps<T> { a + b - c }
instead of
fn some_op(a: T, b: T, c:T) -> T
where T: Add<T> + Sub<T> { a + b - c }
The issue is that this isn't really what I want. I actually want the operations to work by reference.
I can get this work when explicitly stating the trait bounds:
fn some_op<T>(a: &T, b: &T, c: &T) -> T
where for<'a> &'a T : Add<&'a T, Output = T> + Sub<&'a T, Output = T>
{
&(a + b) - c
}
And ideally I would like a subtrait that summarises all of the cross operations, something like:
fn some_op<T>(a: &T, b: &T, c: &T) -> T
where
for<'a> 'a T: Add<&'a T, Output=T> + Add<'a T, Output=T> + Sub<&'a T, Output=T> + Sub<'a T, Output=T>
for<'a> &'a T : Add<&'a T, Output = T> + Add<'a T, Output = T> + Sub<&'a T, Output = T> + Sub<'a T, Output=T>
{
&(a + b) - c
}
An answer I received on Stack Overflow suggested the use of the experimental trait_alias (Rust Playground)
But I was hoping that this might exist already and I just didnt understand how to do it properly?