jmg-duarte/sealed-rs

Macro for sealing traits and structures

Rust

63

38 commits

updated Jul 22, 2026

See the code

README

#[sealed]

MSRV 1.67.0

This crate provides a convenient and simple way to implement the sealed trait pattern, as described in the Rust API Guidelines [1].

[dependencies]
sealed = "0.6"

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/.

use sealed::sealed;

#[sealed]
trait T {}

pub struct A;

#[sealed]
impl T for A {}

pub struct B;

#[sealed]
impl T for B {}

pub struct C;

impl T for C {} // compile error

Arguments

This is the list of arguments that can be used in a #[sealed] attribute:

  • #[sealed(erase)]: turns on trait bounds erasure. This is useful when using the #[sealed] macro inside a function. For an example, see bound-erasure-fn example.

  • #[sealed(pub(crate))] or #[sealed(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. For an example, see nesting example. Notice, that just pub is disallowed as breaks the whole idea of sealing.

Contributing

See CONTRIBUTING.md.

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
future-proofing
rust
rust-patterns

jmg-duarte/sealed-rs

Macro for sealing traits and structures

Rust

63

38 commits

updated Jul 22, 2026

See the code

README

#[sealed]

MSRV 1.67.0

This crate provides a convenient and simple way to implement the sealed trait pattern, as described in the Rust API Guidelines [1].

[dependencies]
sealed = "0.6"

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/.

use sealed::sealed;

#[sealed]
trait T {}

pub struct A;

#[sealed]
impl T for A {}

pub struct B;

#[sealed]
impl T for B {}

pub struct C;

impl T for C {} // compile error

Arguments

This is the list of arguments that can be used in a #[sealed] attribute:

  • #[sealed(erase)]: turns on trait bounds erasure. This is useful when using the #[sealed] macro inside a function. For an example, see bound-erasure-fn example.

  • #[sealed(pub(crate))] or #[sealed(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. For an example, see nesting example. Notice, that just pub is disallowed as breaks the whole idea of sealing.

Contributing

See CONTRIBUTING.md.

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
future-proofing
rust
rust-patterns

Project health

Overall

3.0/10

Maintained

1/10

Code review

1/10
OpenSSF Scorecard

Significant stargazers

Stepan Koltsov

537 followers · starred Dec 2025

Oleksii Lozovskyi

135 followers · starred Sep 2021

Valentin Kharin

64 followers · starred Apr 2025

Antoine Martin

116 followers · starred May 2021