15 releases

0.6.0 Oct 6, 2024
0.6.0-rc3 Aug 15, 2024
0.5.0 Apr 21, 2023
0.4.0 Jan 30, 2022
0.1.3 Dec 17, 2020

#187 in Rust patterns

Download history 98764/week @ 2024-08-04 109175/week @ 2024-08-11 121946/week @ 2024-08-18 118631/week @ 2024-08-25 110006/week @ 2024-09-01 113903/week @ 2024-09-08 105985/week @ 2024-09-15 100062/week @ 2024-09-22 99879/week @ 2024-09-29 90145/week @ 2024-10-06 83469/week @ 2024-10-13 98330/week @ 2024-10-20 140914/week @ 2024-10-27 242179/week @ 2024-11-03 235911/week @ 2024-11-10 221586/week @ 2024-11-17

843,172 downloads per month
Used in 137 crates (49 directly)

MIT/Apache

15KB
151 lines

#[sealed]

MSRV 1.61.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.5"

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.

Dependencies

~220–660KB
~16K SLoC