Sealed traits

One possibility that hasn't been explored is to have two attributes, #[sealed] and #[non_exhaustive]. #[sealed] on a trait forbids impls outside of the defining crate and makes it a semver-breaking change to add new impls within the defining crate, or to remove the attribute. #[non_exhaustive] only forbids impls outside the defining crate (analogous to to how #[non_exhaustive] on a struct or enum forbids construction outside the defining crate).

#[non_exhaustive] would mean "the list of requirements for implementing this trait isn't exhaustive and might change in the future, new required methods or trait bounds may be introduced". #[sealed] would mean "the list of implementations of this trait has been sealed forever and will not change, the trait solver can make full use of that information".

4 Likes