pub trait Structable: Valuable {
// Required method
fn definition(&self) -> StructDef<'_>;
}Expand description
A struct-like Valuable sub-type.
Implemented by Valuable types that have a struct-like shape. Fields may
be named or unnamed (tuple). Values that implement Structable must return
Value::Structable from their Valuable::as_value implementation.
§Inspecting
Inspecting fields contained by a Structable instance is done by visiting
the struct. When visiting a Structable, either the visit_named_fields()
or the visit_unnamed_fields() methods of Visit are called. Each method
may be called multiple times per Structable, but the two methods are never
mixed.
use valuable::{NamedValues, Valuable, Value, Visit};
#[derive(Valuable)]
struct MyStruct {
foo: u32,
bar: u32,
}
struct PrintFields;
impl Visit for PrintFields {
fn visit_named_fields(&mut self, named_values: &NamedValues<'_>) {
for (field, value) in named_values.iter() {
println!("{}: {:?}", field.name(), value);
}
}
fn visit_value(&mut self, value: Value<'_>) {
match value {
Value::Structable(v) => v.visit(self),
_ => {} // do nothing for other types
}
}
}
let my_struct = MyStruct {
foo: 123,
bar: 456,
};
valuable::visit(&my_struct, &mut PrintFields);If the struct is statically defined, then all fields are known ahead of
time and may be accessed via the StructDef instance returned by
definition(). NamedField instances returned by definition()
maybe used to efficiently extract specific field values.
§Implementing
Implementing Structable is usually done by adding #[derive(Valuable)] to
a Rust struct definition.
use valuable::{Fields, Valuable, Structable, StructDef};
#[derive(Valuable)]
struct MyStruct {
foo: &'static str,
}
let my_struct = MyStruct { foo: "Hello" };
let fields = match my_struct.definition() {
StructDef::Static { name, fields, .. } => {
assert_eq!("MyStruct", name);
fields
}
_ => unreachable!(),
};
match fields {
Fields::Named(named_fields) => {
assert_eq!(1, named_fields.len());
assert_eq!("foo", named_fields[0].name());
}
_ => unreachable!(),
}Required Methods§
Sourcefn definition(&self) -> StructDef<'_>
fn definition(&self) -> StructDef<'_>
Trait Implementations§
Source§impl Debug for dyn Structable + '_
impl Debug for dyn Structable + '_
Source§impl<'a> From<&'a dyn Structable> for Value<'a>
A Rust struct value
impl<'a> From<&'a dyn Structable> for Value<'a>
A Rust struct value
§Examples
use valuable::{Value, Valuable};
#[derive(Valuable)]
struct MyStruct {
field: u32,
}
let my_struct = MyStruct {
field: 123,
};
let v = Value::Structable(&my_struct);Source§fn from(src: &'a dyn Structable) -> Value<'a>
fn from(src: &'a dyn Structable) -> Value<'a>
Implementations on Foreign Types§
Source§impl<T> Structable for &Twhere
T: Structable + ?Sized,
impl<T> Structable for &Twhere
T: Structable + ?Sized,
fn definition(&self) -> StructDef<'_>
Source§impl<T> Structable for &mut Twhere
T: Structable + ?Sized,
impl<T> Structable for &mut Twhere
T: Structable + ?Sized,
fn definition(&self) -> StructDef<'_>
Source§impl<T> Structable for Box<T>where
T: Structable + ?Sized,
Available on crate feature alloc only.
impl<T> Structable for Box<T>where
T: Structable + ?Sized,
alloc only.fn definition(&self) -> StructDef<'_>
Source§impl<T> Structable for Rc<T>where
T: Structable + ?Sized,
Available on crate feature alloc only.
impl<T> Structable for Rc<T>where
T: Structable + ?Sized,
alloc only.fn definition(&self) -> StructDef<'_>
Source§impl<T> Structable for Arc<T>where
T: Structable + ?Sized,
Available on non-valuable_no_atomic_cas and crate feature alloc only.
impl<T> Structable for Arc<T>where
T: Structable + ?Sized,
valuable_no_atomic_cas and crate feature alloc only.