pub trait Enumerable: Valuable {
// Required methods
fn definition(&self) -> EnumDef<'_>;
fn variant(&self) -> Variant<'_>;
}Expand description
An enum-like Valuable sub-type.
Implemented by Valuable types that have an enum-like shape. Fields may
be named or unnamed (tuple). Values that implement Enumerable must return
Value::Enumerable from their Valuable::as_value implementation.
§Inspecting
The variant() method returns the Enumerable instance’s variant. The
Enumerable may also have unnamed fields (tuple) or named fields.
Inspecting the field values is done by visiting the enum. When visiting an
Enumerable, either the visit_named_fields() or the
visit_unnamed_fields() methods of Visit are called. Each method may
be called multiple times per Enumerable, but the two methods are never
mixed.
use valuable::{Valuable, Value, Visit};
#[derive(Valuable)]
enum MyEnum {
Foo,
Bar(u32),
}
struct PrintVariant;
impl Visit for PrintVariant {
fn visit_unnamed_fields(&mut self, values: &[Value<'_>]) {
for value in values {
println!(" - {:?}", value);
}
}
fn visit_value(&mut self, value: Value<'_>) {
match value {
Value::Enumerable(v) => {
println!("{}", v.variant().name());
v.visit(self)
}
_ => {}
}
}
}
let my_enum = MyEnum::Bar(123);
valuable::visit(&my_enum, &mut PrintVariant);If the enum is statically defined, then all variants, and variant fields
are known ahead of time and may be accessed via the EnumDef instance
returned by definition().
§Implementing
Implementing Enumerable is usually done by adding #[derive(Valuable)] to
a Rust enum definition.
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!(variants[0].fields().is_unnamed());Required Methods§
Sourcefn definition(&self) -> EnumDef<'_>
fn definition(&self) -> EnumDef<'_>
Trait Implementations§
Source§impl Debug for dyn Enumerable + '_
impl Debug for dyn Enumerable + '_
Source§impl<'a> From<&'a dyn Enumerable> for Value<'a>
A Rust enum value
impl<'a> From<&'a dyn Enumerable> for Value<'a>
A Rust enum value
§Examples
use valuable::{Value, Valuable};
#[derive(Valuable)]
enum MyEnum {
Foo,
Bar,
}
let my_enum = MyEnum::Foo;
let v = Value::Enumerable(&my_enum);Source§fn from(src: &'a dyn Enumerable) -> Value<'a>
fn from(src: &'a dyn Enumerable) -> Value<'a>
Implementations on Foreign Types§
Source§impl<T, E> Enumerable for Result<T, E>
impl<T, E> Enumerable for Result<T, E>
Source§impl<T: ?Sized + Enumerable> Enumerable for &T
impl<T: ?Sized + Enumerable> Enumerable for &T
Source§impl<T: ?Sized + Enumerable> Enumerable for &mut T
impl<T: ?Sized + Enumerable> Enumerable for &mut T
Source§impl<T: ?Sized + Enumerable> Enumerable for Box<T>
Available on crate feature alloc only.
impl<T: ?Sized + Enumerable> Enumerable for Box<T>
alloc only.Source§impl<T: ?Sized + Enumerable> Enumerable for Rc<T>
Available on crate feature alloc only.
impl<T: ?Sized + Enumerable> Enumerable for Rc<T>
alloc only.Source§impl<T: ?Sized + Enumerable> Enumerable for Arc<T>
Available on non-valuable_no_atomic_cas and crate feature alloc only.
impl<T: ?Sized + Enumerable> Enumerable for Arc<T>
valuable_no_atomic_cas and crate feature alloc only.