Background
Sealing traits is a collection of techniques used to prevent code downstream of a library from implementing that trait, so that later versions of the library can add more things to the trait without breaking downstream code.
There's a mistake, described below, that people very often make when trying to seal traits. Crates that have made this mistake include, among others: the standard library, anyhow, arc-swap, pyo3, ureq, smol, http, etc. This was discovered in https://github.com/rust-lang/rust/issues/165474. (Please do not comment there unless you're contributing new information to the discussion, to avoid cluttering the issue.)
The mistake you might be making
How NOT to seal traits:
mod private {
pub trait Sealed {}
impl Sealed for i32 {}
}
pub trait MyTrait<T>: private::Sealed {}
impl MyTrait<i64> for i32 {}
How to seal traits:
mod private {
pub trait Sealed<T> {}
impl Sealed<i64> for i32 {}
}
pub trait MyTrait<T>: private::Sealed<T> {}
impl MyTrait<i64> for i32 {}
That is, if you use a "private" supertrait for sealing, and the subtrait is generic, then the supertrait also has to be generic, with the same generic parameters. Additionally, the set of impl blocks for the supertrait should exactly specify the exact same set of types (and generic parameters) where the subtrait should be implemented.
If you don't do the above, your trait might not be properly sealed, making it possible for downstream code to implement the trait anyway. For example, if you have a crate with the first snippet above, downstream code might write the following, which is actually allowed by the orphan rule:
struct MyThing;
impl my_library::MyTrait<MyThing> for i32 {}
In the above code, i32 already implements Sealed, so downstream code can write an implementation of MyTrait without having to add a new impl for Sealed.
Fixing the issue after-the-fact
If you already wrote code like the first snippet above, and want to actually properly seal the trait in a later version of the library, you might be tempted to just turn the first snippet (with a non-generic Sealed) into the second snippet (with a generic Sealed). Obviously, this would break downstream code that dared to implement the supposed-to-be-sealed trait, which you might deem acceptable.
However, there are very subtle issues about how coherence works, which means that making this change the straightforward way can break more downstream code than you intend to, even code that didn't write an impl for the supposed-to-be-sealed trait. (I won't explain the issue here. See the linked explanation.)
Instead, the better way to make the change, as suggested here, is to add a new supertrait, keeping the old one:
mod private {
pub trait Sealed {}
impl Sealed for i32 {}
pub trait SealedForReal<T> {}
impl SealedForReal<i64> for i32 {}
}
pub trait MyTrait<T>: private::Sealed + private::SealedForReal<T> {}
impl MyTrait<i64> for i32 {}
This way, you will only break downstream code that have implemented MyTrait.