Enum valuable::EnumDef[][src]

#[non_exhaustive]
pub enum EnumDef<'a> {
    Static {
        name: &'static str,
        variants: &'static [VariantDef<'static>],
    },
    Dynamic {
        name: &'a str,
        variants: &'a [VariantDef<'a>],
    },
}
Expand description

An enum’s variants, variant fields, and other enum-level information.

Returned by Enumerable::definition(), EnumDef provides the caller with information about the enum’s definition.

Variants (Non-exhaustive)

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.

Static

Fields

This variant is marked as non-exhaustive
Non-exhaustive enum variants could have additional fields added in future. Therefore, non-exhaustive enum variants cannot be constructed in external crates and cannot be matched against.
name: &'static str

The enum’s name

variants: &'static [VariantDef<'static>]

The enum’s variants

The enum is statically-defined, all variants and variant-level fields are known ahead of time.

Most Enumerable definitions for Rust enum types will be EnumDef::Static.

Examples

A statically defined enum

use valuable::{Valuable, Enumerable, EnumDef};

#[derive(Valuable)]
enum MyEnum {
    Foo,
    Bar(u32),
}

let my_enum = MyEnum::Bar(123);

let variants = match my_enum.definition() {
    EnumDef::Static { name, variants, .. } => {
        assert_eq!("MyEnum", name);
        variants
    }
    _ => unreachable!(),
};

assert_eq!(2, variants.len());
assert_eq!("Foo", variants[0].name());
assert_eq!("Bar", variants[1].name());

Dynamic

Fields

This variant is marked as non-exhaustive
Non-exhaustive enum variants could have additional fields added in future. Therefore, non-exhaustive enum variants cannot be constructed in external crates and cannot be matched against.
name: &'a str

The enum’s name

variants: &'a [VariantDef<'a>]

The enum’s variants

The enum is dynamically-defined, not all variants and fields are known ahead of time.

Examples

The enum variant is tracked as a string

use valuable::{Enumerable, EnumDef, Fields, VariantDef, Valuable, Value, Variant, Visit};

/// A dynamic enum
struct DynEnum {
    // The enum name
    name: String,

    // The current variant
    variant: String,
}

impl Valuable for DynEnum {
    fn as_value(&self) -> Value<'_> {
        Value::Enumerable(self)
    }

    fn visit(&self, _visit: &mut dyn Visit) {
        // No variant fields, so there is nothing to call here.
    }
}

impl Enumerable for DynEnum {
    fn definition(&self) -> EnumDef<'_> {
        EnumDef::new_dynamic(&self.name, &[])
    }

    fn variant(&self) -> Variant<'_> {
        Variant::Dynamic(VariantDef::new(&self.variant, Fields::Unnamed(0)))
    }
}

Implementations

Create a new EnumDef::Static instance.

This should be used when an enum’s variants are fixed and known ahead of time.

Examples
use valuable::{EnumDef, Fields, VariantDef};

static VARIANTS: &[VariantDef<'static>] = &[
    VariantDef::new("Bar", Fields::Unnamed(1)),
];

let def = EnumDef::new_static( "Foo", VARIANTS);

Create a new EnumDef::Dynamic instance.

This is used when the enum’s variants may vary at runtime.

Examples
use valuable::{EnumDef, Fields, VariantDef};

let def = EnumDef::new_dynamic(
    "Foo",
    &[VariantDef::new("Bar", Fields::Unnamed(1))]
);

Returns the enum’s name

Examples
use valuable::{Enumerable, Valuable};

#[derive(Valuable)]
enum Foo {
    Bar,
    Baz,
}

let def = Foo::Bar.definition();
assert_eq!("Foo", def.name());

Returns the enum’s variants

Examples
use valuable::{Enumerable, Valuable};

#[derive(Valuable)]
enum Foo {
    Bar,
    Baz,
}

let def = Foo::Bar.definition();
let variants = def.variants();

assert_eq!(2, variants.len());
assert_eq!("Bar", variants[0].name());

Returns true if the enum is statically defined.

Examples

With a static enum

use valuable::{Enumerable, Valuable};

#[derive(Valuable)]
enum Foo {
    Bar,
    Baz,
}

let def = Foo::Bar.definition();
assert!(def.is_static());

With a dynamic enum

use valuable::{EnumDef, Fields, VariantDef};

let def = EnumDef::new_dynamic("Foo", &[]);
assert!(!def.is_static());

Returns true if the enum is dynamically defined.

Examples

With a static enum

use valuable::{Enumerable, Valuable};

#[derive(Valuable)]
enum Foo {
    Bar,
    Baz,
}

let def = Foo::Bar.definition();
assert!(!def.is_dynamic());

With a dynamic enum

use valuable::{EnumDef, Fields, VariantDef};

let def = EnumDef::new_dynamic("Foo", &[]);
assert!(def.is_dynamic());

Trait Implementations

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.