Sorting tuple of types at compile time: I wanted to know if I could, no I'm scared of what I've done.
Hey Rustaceans !
I made a post recently, looking for advices and I fell into the rabbit hole of type-level programming. My goal was to try to sort tuples at compile time, to build some sort of "set" of types (tuples are more like vecs of types).
Now, I succeeded, and I built this thing, I have no clue if it should be allowed or not:
A generic type TupleSort<T> that expand to exactly the tuple you pass into it, but sorted. This means that all the following:
let order1: TupleSort<(u8, bool, String)> = Default::default();
let order2: TupleSort<(bool, u8, String)> = Default::default();
let order3: TupleSort<(String, u8, bool)> = Default::default();
let order4: TupleSort<(String, bool, u8)> = Default::default();
let order5: TupleSort<(u8, String, bool)> = Default::default();
let order6: TupleSort<(bool, String, u8)> = Default::default();
Have the same type, which is (u8, String, bool) (the order is arbitrary, defined by type id so consistent accross one compilation).
My initial goals with this was for archetypes in a ECS, to do all the archetype operations at compile time, but now as my first type-level thingy I was really proud of this and wanted to share this with you ! What do you guys think ?
Type level programming really feels like opening the door of a whole new level of abstraction, and I can't wait to see what more I can do.
For any one who is interested, here is a link to the playground (this is done using the nightly compiler, as to sort types I need an order on them, and thus const type ids).