How to express if &Self impls some trait, then Self has the impl of some other trait?

I'm writing some trait about Linear Space. This set of traits ensures that:
If some type T meets the requirement of &T:Add<Output=T>, &T:Sub<Output=T>, and &T:Mul<Num, Output=T>, then it can impl my LinearSpace trait.

So that I write

pub trait LinearSpace<Scalar>
where
    Scalar: Num,
    Self: Sized,
    for<'a> &'a Self: Add<Output = Self>,
    for<'a> &'a Self: Sub<Output = Self>,
    for<'a> &'a Self: Mul<Scalar, Output = Self>,
{
}

Then I have some function say foo, which accepts a parameter of LinearSpace:

fn foo<Scalar, T>(x:&T)
where T:LinearSpace<Scalar>,
Scalar: Num+Sized
{
...
}

It is rather nature isn't it? But it's wrong. Rustc requires me to write

fn foo<Scalar, T>(x:&T)
where T:LinearSpace<Scalar>,
Scalar: Num+Sized,
for<'a> &'a T: Add<Output = T>,
for<'a> &'a T: Sub<Output = T>,
for<'a> &'a T: Mul<Scalar, Output = T>,
{
...
}

Is it possible to not write

for<'a> &'a T: Add<Output = T>,
for<'a> &'a T: Sub<Output = T>,
for<'a> &'a T: Mul<Scalar, Output = T>,

in the definition of above function?
Thanks.

No, it is not possible to omit constraints, as that would be a backwards compatibility hazard. For example if you later decide that you don't need the Add constraint on LinearSpace so you remove it, you will then break the function foo. This is bad.

1 Like

https://github.com/rust-lang/rust/issues/47670

1 Like