pub trait TreeAny: TreeSchema {
// Required methods
fn ref_any_by_key(&self, keys: impl Keys) -> Result<&dyn Any, ValueError>;
fn mut_any_by_key(
&mut self,
keys: impl Keys,
) -> Result<&mut dyn Any, ValueError>;
// Provided methods
fn ref_by_key<T: Any>(&self, keys: impl IntoKeys) -> Result<&T, ValueError> { ... }
fn mut_by_key<T: Any>(
&mut self,
keys: impl IntoKeys,
) -> Result<&mut T, ValueError> { ... }
}
Expand description
Access any node by keys.
This uses the dyn Any
trait object.
use core::any::Any;
use miniconf::{Indices, IntoKeys, JsonPath, TreeAny, TreeSchema};
#[derive(TreeSchema, TreeAny, Default)]
struct S {
foo: u32,
bar: [u16; 2],
};
let mut s = S::default();
for key in S::SCHEMA.nodes::<Indices<[_; 2]>, 2>() {
let a = s.ref_any_by_key(key.unwrap().into_keys()).unwrap();
assert!([0u32.type_id(), 0u16.type_id()].contains(&(&*a).type_id()));
}
let val: &mut u16 = s.mut_by_key(&JsonPath(".bar[1]")).unwrap();
*val = 3;
assert_eq!(s.bar[1], 3);
let val: &u16 = s.ref_by_key(&JsonPath(".bar[1]")).unwrap();
assert_eq!(*val, 3);
Required Methods§
Sourcefn ref_any_by_key(&self, keys: impl Keys) -> Result<&dyn Any, ValueError>
fn ref_any_by_key(&self, keys: impl Keys) -> Result<&dyn Any, ValueError>
Obtain a reference to a dyn Any
trait object for a leaf node.
Sourcefn mut_any_by_key(
&mut self,
keys: impl Keys,
) -> Result<&mut dyn Any, ValueError>
fn mut_any_by_key( &mut self, keys: impl Keys, ) -> Result<&mut dyn Any, ValueError>
Obtain a mutable reference to a dyn Any
trait object for a leaf node.
Provided Methods§
Sourcefn ref_by_key<T: Any>(&self, keys: impl IntoKeys) -> Result<&T, ValueError>
fn ref_by_key<T: Any>(&self, keys: impl IntoKeys) -> Result<&T, ValueError>
Obtain a reference to a leaf of known type by key.
Sourcefn mut_by_key<T: Any>(
&mut self,
keys: impl IntoKeys,
) -> Result<&mut T, ValueError>
fn mut_by_key<T: Any>( &mut self, keys: impl IntoKeys, ) -> Result<&mut T, ValueError>
Obtain a mutable reference to a leaf of known type by key.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.