I recently came into a situation where I wanted my generic T to be restricted to trait only types. This kind of got me thinking : would it be possible to introduce a meta trait system for the rust type system to model the language elements of rust like struct, enum, trait etc. It would allow me to do something like:
use meta;
struct Object<T> where meta T: meta::Trait, T: OtherTraitBounds {
data: Box<dyn T>
}
I've run into this limitation before when trying to build a dynamic dispatch system similar to COM's IUnknown in Rust. If I remember correctly, I never found a good solution to something like the following signature:
The size of the result changes depending on whether T is Sized or not, but the body of the function is a bunch of "does the TypeId of T equal the TypeId of trait X?" checks. You can't use regular, safe casting, so you have to resort to transmutes. The compiler really doesn't like it when you combine transmutes and inconsistently sized generic types.
I forget how I implemented it in the end, but there was some amount of hiding things inside boxes of sized types and then un-boxing them later with unsafe transmutes inside a macro... it wasn't pretty.