BooleanWhereClauseItem?

#1

Maybe it’s a over-use of the inference engine, but i wonder if it is feasible to add a “boolean” where clause item. I imagine it would use some same mechanism from const generics.

Example code:

struct S<T,U>;
impl<T, U> S<T,U> where const TypeId::of::<T> == TypeId::of::<U> {
     fn some_method() {
        ..
     }
}
1 Like
#2

In this particular example, you could impl<T> S<T, T>. Trying to match T != U would be new though.

1 Like
#3

It’s kinda possible on Nightly since forever using auto traits, see this little playground example

4 Likes
#4

@jer Interesting! I never learnt this before.

I’m adding more examples:

//ignore missing #[fundamental] here.
impl<T, const N: usize> [T; N]  {
    fn prefix<const M: usize>(&self) -> [T; M] 
        where T: Clone, const M <= N {
        ...
    }
}
#5

If all you want are type equality constraints, then take a look at this tracking issue: https://github.com/rust-lang/rust/issues/20041

1 Like