Trait enum_debug::EnumDebug

source ·
pub trait EnumDebug {
    // Required methods
    fn variant_names() -> &'static [&'static str];
    fn variant_name(&self) -> &'static str;

    // Provided methods
    fn type_name() -> &'static str { ... }
    fn variant(&self) -> EnumDebugFormatter<'_, Self> { ... }
    fn variants() -> Copied<Iter<'static, &'static str>> { ... }
}
Expand description

Exposes the names of the variants in an enum.

By itself, this enum doesn’t add a lot over just defining your own functions. However, if you use the derive-feature, you can automatically generate it based on your normal enum definition.

See [derive] for more information on the macro itself.

§Examples

use enum_debug::EnumDebug;

enum Jedi {
    ObiWanKenobi,
    AnakinSkywalker,
    MaceWindu,
    MasterYoda,
}
impl EnumDebug for Jedi {
    // NOTE: Not necessary, but otherwise it will use the Rust internal type name
    #[inline]
    fn type_name() -> &'static str { "Jedi" }

    #[inline]
    fn variant_names() -> &'static [&'static str] {
        &["ObiWanKenobi", "AnakinSkywalker", "MaceWindu", "MasterYoda"]
    }

    #[inline]
    fn variant_name(&self) -> &'static str {
        match self {
            Self::ObiWanKenobi => Self::variant_names()[0],
            Self::AnakinSkywalker => Self::variant_names()[1],
            Self::MaceWindu => Self::variant_names()[2],
            Self::MasterYoda => Self::variant_names()[3],
        }
    }
}

assert_eq!(format!("{}", Jedi::ObiWanKenobi.variant()), "ObiWanKenobi");
assert_eq!(format!("{:?}", Jedi::AnakinSkywalker.variant()), "Jedi::AnakinSkywalker");
assert_eq!(Jedi::MaceWindu.variant_name(), "MaceWindu");

Required Methods§

source

fn variant_names() -> &'static [&'static str]

Returns all variants in the trait as a list of names.

§Returns

A static slice of &'static strs with the names of all variants.

If you have derived this automatically, then the order is the same as defined.

§Example
use enum_debug::EnumDebug;

enum Jedi {
    ObiWanKenobi,
    AnakinSkywalker,
    MaceWindu,
    MasterYoda,
}
impl EnumDebug for Jedi {
    // e.g. derived
}

assert_eq!(Jedi::variant_names(), &[
    "ObiWanKenobi",
    "AnakinSkywalker",
    "MaceWindu",
    "MasterYoda"
]);
source

fn variant_name(&self) -> &'static str

Returns the static name of the variant.

§Returns

A &'static str with the name of the current variant.

§Example
use enum_debug::EnumDebug;

enum Jedi {
    ObiWanKenobi,
    AnakinSkywalker,
    MaceWindu,
    MasterYoda,
}
impl EnumDebug for Jedi {
    // e.g. derived
}

assert_eq!(Jedi::ObiWanKenobi.variant_name(), "ObiWanKenobi");
assert_eq!(Jedi::AnakinSkywalker.variant_name(), "AnakinSkywalker");

Provided Methods§

source

fn type_name() -> &'static str

Returns the static name of the type used for EnumDebug-printing.

§Returns

A &'static str with the name of the current type.

If you have derived this automatically, then this equals the name of the enum. See [derive] for some alternative ways of deriving type names.

§Example
use enum_debug::EnumDebug;

enum Jedi {
    ObiWanKenobi,
    AnakinSkywalker,
    MaceWindu,
    MasterYoda,
}
impl EnumDebug for Jedi {
    // e.g. derived
}

assert_eq!(Jedi::type_name(), "Jedi");
source

fn variant(&self) -> EnumDebugFormatter<'_, Self>

Returns a formatter for this enum that writes its variant name.

§Returns

A new instance of an EnumDebugFormatter that implements Debug and Display.

In the former, it also includes the name of the type itself (as given by Self::type_name()).

§Example
use enum_debug::EnumDebug;

enum Jedi {
    ObiWanKenobi,
    AnakinSkywalker,
    MaceWindu,
    MasterYoda,
}
impl EnumDebug for Jedi {
    // e.g. derived
}

assert_eq!(format!("{}", Jedi::ObiWanKenobi.variant()), "ObiWanKenobi");
assert_eq!(format!("{:?}", Jedi::AnakinSkywalker.variant()), "Jedi::AnakinSkywalker");
source

fn variants() -> Copied<Iter<'static, &'static str>>

Returns an iterator over all variants in this enum.

§Returns

An Iter that generates the name of the variants as defined by Self::variant_names().

If you have derived this automatically, then the order is the same as defined.

§Example
use enum_debug::EnumDebug;

enum Jedi {
    ObiWanKenobi,
    AnakinSkywalker,
    MaceWindu,
    MasterYoda,
}
impl EnumDebug for Jedi {
    // e.g. derived
}

assert_eq!(Jedi::variants().collect::<Vec<&'static str>>(), vec![
    "ObiWanKenobi",
    "AnakinSkywalker",
    "MaceWindu",
    "MasterYoda"
]);

Object Safety§

This trait is not object safe.

Implementors§