#[sealed]
This crate provides a convenient and simple way to implement the sealed trait pattern, as described in the Rust API Guidelines [1].
toml
[dependencies]
sealed = "0.1"
In the following code structs A
and B
implement the sealed trait T
,
the C
struct, which is not sealed, will error during compilation.
You can see a demo in demo/
.
```rust use sealed::sealed;
trait T {}
pub struct A;
impl T for A {}
pub struct B;
impl T for B {}
pub struct C;
impl T for C {} // compile error
fn main() { return } ```
The macro generates a private
module when attached to a trait
(this raises the limitation that the #[sealed]
macro can only be added to a single trait per module),
when attached to a struct
the generated code simply implements the sealed trait for the respective structure.
```rust // #[sealed] // trait T {} trait T: private::Sealed {} mod private { trait Sealed {} }
// #[sealed] // pub struct A; pub struct A; impl private::Sealed for A {} ```