Crates.io | sealed |
lib.rs | sealed |
version | 0.6.0 |
source | src |
created_at | 2020-12-17 14:15:07.906176 |
updated_at | 2024-10-06 16:19:07.451936 |
description | Macro for sealing traits and structures |
homepage | |
repository | https://github.com/jmg-duarte/sealed-rs |
max_upload_size | |
id | 323922 |
size | 43,629 |
#[sealed]
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.5"
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
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.
See CONTRIBUTING.md.