Skip to main content

Enumerable

Trait Enumerable 

Source
pub trait Enumerable<Enum> {
    // Required method
    fn into_enum(self) -> Enum;
}
Expand description

A type that can be turned into the enum of its sealed trait.

enumerate implements this for every permitted type, and makes Enumerable<TheSealedTrait> a supertrait of the sealed trait. Naming the enum in the bound is what lets a caller reach it through the trait alone:


struct Square;

#[enumerate]
#[sealed(Square)]
trait Shape {}

impl Shape for Square {}

// No import: the supertrait bound carries `into_enum` in with `S: Shape`.
fn describe<S: Shape>(s: S) {
    match s.into_enum() {
        AnyShape::Square(s) => { /* .. */ }
    }
}

Note that Enumerable did not have to be imported above: the supertrait bound brings into_enum into scope through S: TheSealedTrait. Calling it on a concrete type rather than a generic one does need the import.

From is implemented alongside it in the other direction, so From::from and Into::into work too.

Required Methods§

Source

fn into_enum(self) -> Enum

Wraps self in the variant of Enum that holds this type.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§