TL;DR, I am proposing a grammar like
impl <T,const N> Copy for [T;N]
where N==0 {
// currently N==0 is not a legal constrain, since we could directly evaluate N. But we could allow it later since we need it.
} or where T:Copy {}
trait Foo {
fn foo() where Self:Bar {
<Self as Bar>::bar()
} or where Self:Baz {
<Self as Baz>::baz()
}
}
the or
conjunction could be either removed(since it is not necessary, where clause after a }
is currently an error.)
All of us know that, for array [T;N]
If the element T
is Copy
, then [T;N]
should be Copy
for all N
If the const parameter N
is 0
, then [T;N]
is a ZST-type [T;0]
, which should also be Copy
.
Thus, our constrains of impl<T,const N> Copy for [T;N]
should be
where T:Copy *OR* N==0,
and for now, rust could not recognize or
pattern in where clause, since we could not write a simple program that fits either constrains (otherwise, the constrains could be strengthen into T:Copy *AND* N==0
)
Thus, a simple idea come up into my mind: is it possible add a or
clause to impl block?
impl <T,const N> Copy for [T;N]
where N==0 {
// currently N==0 is not a legal constrain, since we could directly evaluate N. But we could allow it later since we need it.
} or where T:Copy {}
Further, we could place where
clause in the trait, evaluate it later.
Suppose we have
trait Foo {
fn foo() where Self:Bar {
<Self as Bar>::bar()
} or where Self:Baz {
<Self as Baz>::baz()
}
}
trait Bar {
fn bar();
}
trait Baz {
fn baz();
}
the advantages of such implementation is that
Firstly, we could rely the specialization result, since all the specialization is controlled by crate owner rather than user.
Secondly, it allow us define different specialization trait for same object, for example
trait NaiveCalculation {...}
trait SimdCalculation {...}
trait ParallelCalculation {...}
trait ParallelSimdCalculation {...}
trait Calculation {...}
impl<T> Calculation for T
#[cfg(features="simd","rayon")]
where T:ParallelSimdCalculation {...}
or
#[cfg(features="simd")]
where T: SimdCalculation {...}
or
#[cfg(features="rayon")]
where T: ParallelCalculation {...}
or where T:NaiveCalculation{...}
Currently, we could not define simple things like
impl <T:Bar> Foo for T{}
default impl <T:Baz> Foo for T{}
since we could not ensure T
is never Bar+Baz
but if we have or where
clause, things could be easy.
Is it possible? What's the shortcoming?