pub fn visit(value: &impl Valuable, visit: &mut dyn Visit)
Expand description
Inspects a value by calling the relevant Visit
methods with value
’s
data.
This method calls Visit::visit_value()
with the provided Valuable
instance. See Visit
documentation for more details.
Examples
Extract a single field from a struct. Note: if the same field is repeatedly
extracted from a struct, it is preferable to obtain the associated
NamedField
once and use it repeatedly.
use valuable::{NamedValues, Valuable, Value, Visit};
#[derive(Valuable)]
struct MyStruct {
foo: usize,
bar: usize,
}
struct GetFoo(usize);
impl Visit for GetFoo {
fn visit_named_fields(&mut self, named_values: &NamedValues<'_>) {
if let Some(foo) = named_values.get_by_name("foo") {
if let Some(val) = foo.as_usize() {
self.0 = val;
}
}
}
fn visit_value(&mut self, value: Value<'_>) {
if let Value::Structable(v) = value {
v.visit(self);
}
}
}
let my_struct = MyStruct {
foo: 123,
bar: 456,
};
let mut get_foo = GetFoo(0);
valuable::visit(&my_struct, &mut get_foo);
assert_eq!(123, get_foo.0);
Visit
: Visit NamedField
: crate::NamedField