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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use std::any::Any;
use std::collections::BTreeMap;
use std::fmt;
use std::fmt::{Debug, Formatter};

#[derive(Default)]
pub struct Data {
    map: BTreeMap<String, Box<dyn Any + Send + Sync>>,
}

impl Data {

    #[inline]
    pub fn new() -> Data {
        Self {
            map: BTreeMap::default(),
        }
    }

    pub fn insert<T: 'static + Send + Sync>(&mut self, key: impl Into<String>, val: T) {
        self.map.insert(key.into(), Box::new(val));
    }

    pub fn get<T: 'static + Send>(&self, key: &str) -> Option<&T> {
        self.map.get(key).and_then(|boxed| boxed.downcast_ref())
    }

    pub fn get_mut<T: 'static + Send>(&mut self, key: &str) -> Option<&mut T> {
        self.map.get_mut(key).and_then(|boxed| boxed.downcast_mut())
    }

    pub fn contains<T: 'static + Send>(&self, key: &str) -> bool {
        self.map.contains_key(key)
    }

    pub fn remove<T: 'static + Send>(&mut self, key: &str) -> Option<&T> {
        self.map.remove(key).and_then(|boxed| downcast_owned(boxed))
    }

    pub fn clear(&mut self) {
        self.map.clear()
    }
}

impl Debug for Data {

    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let mut debug_struct = f.debug_struct("LocalData");
        for (k, v) in &self.map {
            debug_struct.field(&k, &v);
        }
        debug_struct.finish()
    }
}

fn downcast_owned<T: 'static>(boxed: Box<dyn Any>) -> Option<T> {
    boxed.downcast().ok().map(|boxed| *boxed)
}