#[sealed]
This crate provides a convenient and simple way to implement the sealed trait pattern, as described in the Rust API Guidelines [1].
[]
= "0.7"
Example
In the following code structs A and B implement the sealed trait T,
the C struct, which is not sealed, will error during compilation.
Examples are available in examples/, you can also see a demo in demo/.
;
;
;
// compile error
Details
The attribute generates a private uniquely named module when attached to a
trait definition, when attached to an impl block the generated code simply
implements the sealed trait for the respective type.
// #[sealed]
// trait T {}
;
// #[sealed]
// impl T for A {}
Arguments
The expanded code may be customized with the following attribute arguments.
erase
Turns on trait bounds erasure. This is useful when using the #[sealed]
attribute inside a function. By default, all the bounds are propagated to
the generated Sealed trait.
// #[sealed(erase)]
// trait Trait<T: ?Sized + Default> {}
pub(crate) or pub(in some::path)
Allows to tune visibility of the generated sealing module (the default one is private). This useful when the trait and its impls are defined in different modules.
;
Notice, that just pub is disallowed as breaks the whole idea of sealing.
// compile error
;
Contributing
See CONTRIBUTING.md.