11 unstable releases (3 breaking)

0.4.0 Jan 30, 2022
0.3.0 Jun 4, 2021
0.2.1 May 17, 2021
0.1.3 Dec 17, 2020

#151 in Rust patterns

Download history 15210/week @ 2022-06-04 12215/week @ 2022-06-11 15794/week @ 2022-06-18 10909/week @ 2022-06-25 12662/week @ 2022-07-02 12406/week @ 2022-07-09 13249/week @ 2022-07-16 10478/week @ 2022-07-23 13538/week @ 2022-07-30 12430/week @ 2022-08-06 15086/week @ 2022-08-13 12408/week @ 2022-08-20 16870/week @ 2022-08-27 14171/week @ 2022-09-03 16629/week @ 2022-09-10 13538/week @ 2022-09-17

63,669 downloads per month
Used in 38 crates (13 directly)

MIT/Apache

14KB
135 lines

#[sealed]

MSRV 1.51.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.4"

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.

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.

Dependencies

~0.7–1.2MB
~24K SLoC