#[seal]Expand description
Seal a trait.
§Sealed Traits
Sealed traits are documented in the Rust API Guidelines as a way to prevent downstream implementations while providing a public trait API. This implementation enforces that the trait is implemented for the exact set of types specified, no more and no less.
use traitful::seal;
// Make it so that only cats and cat-people can meow
#[seal(Cat, CatPerson)]
pub trait Meow {
fn meow() {
println!("meow");
}
}
pub struct Cat;
impl Meow for Cat {}
pub struct CatPerson;
// Will fail to compile if this line is commented out
impl Meow for CatPerson {}§Generics
You can also seal traits with generics:
use traitful::seal;
pub struct Newtype<T>(T);
impl<T> Generic<T> for Newtype<T> {}
#[seal(Newtype<T>)]
pub trait Generic<T> {}§Generics with for syntax
You can also seal traits to types with generics without adding them to the
trait by using the for syntax.
use traitful::seal;
pub struct Newtype<T>(T);
impl<T> MaybeGeneric for Newtype<T> {}
pub struct UnitStruct;
impl MaybeGeneric for UnitStruct {}
#[seal(for<T> Newtype<T>, UnitStruct)]
pub trait MaybeGeneric {}