12 releases

0.5.0 Apr 21, 2023
0.4.0 Jan 30, 2022
0.3.0 Jun 4, 2021
0.2.1 May 17, 2021
0.1.3 Dec 17, 2020

#330 in Rust patterns

Download history 39005/week @ 2024-01-29 33586/week @ 2024-02-05 33019/week @ 2024-02-12 34104/week @ 2024-02-19 43658/week @ 2024-02-26 43553/week @ 2024-03-04 51322/week @ 2024-03-11 49704/week @ 2024-03-18 40961/week @ 2024-03-25 49704/week @ 2024-04-01 57088/week @ 2024-04-08 59679/week @ 2024-04-15 61184/week @ 2024-04-22 70665/week @ 2024-04-29 76761/week @ 2024-05-06 75792/week @ 2024-05-13

286,126 downloads per month
Used in 104 crates (39 directly)


135 lines


MSRV 1.56.0

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

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;

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


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.


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.


~19K SLoC