I’ve been messing a lot with type level programing in the last few months, but always end up being stuck at the point where I need to add bounds for types that are equal, or not.
So I’m looking for a way to implement a trait that would provide such info:
// type level boolean
trait Boolean {} // type level boolean
struct True;
impl Boolean for True {}
struct False;
impl Boolean for False {}
// the trait I want to implement
trait IsEq<T> {
// for <T as IsEq<T>> this is True, otherwise this is False
type Eq: Boolean;
}
This is more complete than a simple IsSame<T>
trait :
trait IsSame<T> {}
impl<T> IsSame<T> for T {}
Because it allows to give bounds for types that needs to be different :
fn f<T, U: IsEq<T>>() where <U as IsEq<T>>::Eq: IsFalse { ... } // IsFalse is a trait implemented only for False
So far, I’ve tried various solutions, all using nightly features:
- Specialization allow to provide the default impl to be False, and a specialized impl for equal types, but it’s nowhere near stabilization.
- generic const exprs, with const type id, allow to check type id equality and go back to the type level world to somehow make this works.
- Negative impls should allow this with the
IsSame<T>
trait, but I’ve been unable to make it work.
I’ve seen that the not eq operator !=
in where clauses have been in discussion for quite a while now (https://github.com/rust-lang/rfcs/issues/1834) but are not planned to be stabilized either any time soon.
I’m running out of options, and this trait would really be useful in a lot of type level programming applications. Is it even possible to implement it in stable Rust ?