I've actually wanted sealed traits to be a language-level feature for exactly this reason. If a trait is #[sealed]
(that is, no one but the declaring crate may write implementations for it), and all of its blanket impls are "sufficiently bound" by sealed traits, it is possible for the compiler to enumerate all types which implement the trait.
I feel like this should be an optimization rather than a relaxation of the object-safety rules, though. I believe there has been a separate discussion of whether or not it should be possible to form dyn Trait
for all traits, and only have dyn Trait: Trait
when Trait
is object-safe. This "partial trait object" probably wants a separate syntax like dyn? Trait
.
You can basically already do this with Sized
bounds, but only for functions at the moment, and the trait author needs to opt-in.