pub trait TreeAny {
// Required methods
fn ref_any_by_key<K>(&self, keys: K) -> Result<&dyn Any, Traversal>
where K: Keys;
fn mut_any_by_key<K>(&mut self, keys: K) -> Result<&mut dyn Any, Traversal>
where K: Keys;
// Provided methods
fn ref_by_key<T: Any, K: IntoKeys>(&self, keys: K) -> Result<&T, Traversal> { ... }
fn mut_by_key<T: Any, K: IntoKeys>(
&mut self,
keys: K,
) -> Result<&mut T, Traversal> { ... }
}
Expand description
Access any node by keys.
This uses the dyn Any
trait object.
use core::any::Any;
use miniconf::{Indices, IntoKeys, JsonPath, Leaf, TreeAny, TreeKey};
#[derive(TreeKey, TreeAny, Default)]
struct S {
foo: Leaf<u32>,
bar: [Leaf<u16>; 2],
};
let mut s = S::default();
for node in S::nodes::<Indices<[_; 2]>, 2>() {
let (key, node) = node.unwrap();
let a = s
.ref_any_by_key(key.into_iter().take(node.depth()).into_keys())
.unwrap();
assert!([0u32.type_id(), 0u16.type_id()].contains(&(&*a).type_id()));
}
let val: &mut u16 = s.mut_by_key(&JsonPath::from(".bar[1]")).unwrap();
*val = 3;
assert_eq!(*s.bar[1], 3);
let val: &u16 = s.ref_by_key(&JsonPath::from(".bar[1]")).unwrap();
assert_eq!(*val, 3);
Required Methods§
Provided Methods§
Object Safety§
This trait is not object safe.