1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use crate::Node;
use std::collections::HashMap;
pub type StateKV = HashMap<Vec<u8>, Vec<u8>>;
pub struct State<W> {
pub child: W,
pub state: StateKV,
}
impl<W> State<W>
where
W: Into<Node>,
{
pub fn get(&self, k: &[u8]) -> Vec<u8> {
self.state.get(k).unwrap_or(&vec![]).to_vec()
}
pub fn set(&mut self, k: &[u8], v: &[u8]) {
self.state.insert(k.to_vec(), v.to_vec());
}
}
impl<W> Into<Node> for State<W>
where
W: Into<Node>,
{
fn into(self) -> Node {
let mut n = self.child.into();
n.state = Some(self.state);
n
}
}