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
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use crate::any::element::{AnyMapElement, AnyMapTrait};
use std::any::TypeId;
use std::collections::HashMap;

/// Maps types to a single instance of that type.
///
/// Used to store state per widget type. In effect a sort of singleton storage.
/// Similar to [the `typemap` crate](https://docs.rs/typemap/0.3.3/typemap/).
#[derive(Clone, Debug, Default)]
pub struct TypeMap(HashMap<TypeId, AnyMapElement>);

// ----------------------------------------------------------------------------

impl TypeMap {
    pub fn get<T: AnyMapTrait>(&mut self) -> Option<&T> {
        self.get_mut().map(|x| &*x)
    }

    pub fn get_mut<T: AnyMapTrait>(&mut self) -> Option<&mut T> {
        self.0.get_mut(&TypeId::of::<T>())?.get_mut()
    }
}

impl TypeMap {
    pub fn get_or_insert_with<T: AnyMapTrait>(&mut self, or_insert_with: impl FnOnce() -> T) -> &T {
        &*self.get_mut_or_insert_with(or_insert_with)
    }

    pub fn get_or_default<T: AnyMapTrait + Default>(&mut self) -> &T {
        self.get_or_insert_with(Default::default)
    }

    pub fn get_mut_or_insert_with<T: AnyMapTrait>(
        &mut self,
        or_insert_with: impl FnOnce() -> T,
    ) -> &mut T {
        use std::collections::hash_map::Entry;
        match self.0.entry(TypeId::of::<T>()) {
            Entry::Vacant(vacant) => vacant
                .insert(AnyMapElement::new(or_insert_with()))
                .get_mut()
                .unwrap(), // this unwrap will never panic, because we insert correct type right now
            Entry::Occupied(occupied) => occupied.into_mut().get_mut_or_set_with(or_insert_with),
        }
    }

    pub fn get_mut_or_default<T: AnyMapTrait + Default>(&mut self) -> &mut T {
        self.get_mut_or_insert_with(Default::default)
    }
}

impl TypeMap {
    pub fn insert<T: AnyMapTrait>(&mut self, element: T) {
        self.0
            .insert(TypeId::of::<T>(), AnyMapElement::new(element));
    }

    pub fn remove<T: AnyMapTrait>(&mut self) {
        self.0.remove(&TypeId::of::<T>());
    }

    pub fn clear(&mut self) {
        self.0.clear();
    }
}

// ----------------------------------------------------------------------------

#[cfg(test)]
#[test]
fn basic_usage() {
    #[derive(Debug, Clone, Eq, PartialEq, Default)]
    struct State {
        a: i32,
    }

    let mut map = TypeMap::default();

    assert!(map.get::<State>().is_none());
    map.insert(State { a: 42 });
    map.insert(5i32);
    map.insert((6.0f32, -1i16));

    assert_eq!(*map.get::<State>().unwrap(), State { a: 42 });
    map.get_mut::<State>().unwrap().a = 43;
    assert_eq!(*map.get::<State>().unwrap(), State { a: 43 });

    map.remove::<State>();
    assert!(map.get::<State>().is_none());

    assert_eq!(*map.get_or_insert_with(|| State { a: 55 }), State { a: 55 });
    map.remove::<State>();
    assert_eq!(
        *map.get_mut_or_insert_with(|| State { a: 56 }),
        State { a: 56 }
    );
    map.remove::<State>();
    assert_eq!(*map.get_or_default::<State>(), State { a: 0 });
    map.remove::<State>();
    assert_eq!(*map.get_mut_or_default::<State>(), State { a: 0 });
}

#[cfg(test)]
#[test]
fn cloning() {
    #[derive(Debug, Clone, Eq, PartialEq, Default)]
    struct State {
        a: i32,
    }

    let mut map: TypeMap = Default::default();

    map.insert(State::default());
    map.insert(10i32);

    let mut cloned_map = map.clone();

    map.insert(11.5f32);
    map.insert("aoeu");

    assert_eq!(*cloned_map.get::<State>().unwrap(), State { a: 0 });
    assert_eq!(*cloned_map.get::<i32>().unwrap(), 10i32);
    assert!(cloned_map.get::<f32>().is_none());
    assert!(cloned_map.get::<&'static str>().is_none());
}

#[cfg(test)]
#[test]
fn removing() {
    #[derive(Debug, Clone, Eq, PartialEq, Default)]
    struct State {
        a: i32,
    }

    let mut map: TypeMap = Default::default();

    map.insert(State::default());
    map.insert(10i32);
    map.insert(11.5f32);
    map.insert("aoeu");

    map.remove::<State>();
    assert!(map.get::<State>().is_none());
    assert!(map.get::<i32>().is_some());
    assert!(map.get::<f32>().is_some());
    assert!(map.get::<&'static str>().is_some());

    map.clear();
    assert!(map.get::<State>().is_none());
    assert!(map.get::<i32>().is_none());
    assert!(map.get::<f32>().is_none());
    assert!(map.get::<&'static str>().is_none());
}