slice_map/
traits.rs

1use alloc::boxed::Box;
2use slotmap::{Key, SecondaryMap, SlotMap, SparseSecondaryMap};
3
4/// Trait to abstract operations on storage of slices
5pub trait SliceStorage<K, V>: Default {
6    fn insert(&mut self, value: V) -> K;
7    fn remove(&mut self, key: K) -> Option<V>;
8    fn get(&self, key: K) -> Option<&V>;
9    fn iter(&self) -> Box<dyn Iterator<Item = (K, &V)> + '_>;
10    fn values(&self) -> Box<dyn Iterator<Item = &V> + '_>;
11    fn values_mut(&mut self) -> Box<dyn Iterator<Item = &mut V> + '_>;
12    fn is_empty(&self) -> bool;
13}
14
15impl<K, V> SliceStorage<K, V> for SlotMap<K, V>
16where
17    K: Key,
18{
19
20    #[inline(always)]
21    fn insert(&mut self, value: V) -> K {
22        self.insert(value)
23    }
24
25    #[inline(always)]
26    fn remove(&mut self, key: K) -> Option<V> {
27        self.remove(key)
28    }
29
30    #[inline(always)]
31    fn get(&self, key: K) -> Option<&V> {
32        self.get(key)
33    }
34
35    #[inline(always)]
36    fn iter(&self) -> Box<dyn Iterator<Item = (K, &V)> + '_> {
37        Box::new(self.iter())
38    }
39
40    #[inline(always)]
41    fn values(&self) -> Box<dyn Iterator<Item = &V> + '_> {
42        Box::new(self.values())
43    }
44
45    #[inline(always)]
46    fn values_mut(&mut self) -> Box<dyn Iterator<Item = &mut V> + '_> {
47        Box::new(self.values_mut())
48    }
49
50    #[inline(always)]
51    fn is_empty(&self) -> bool {
52        self.is_empty()
53    }
54}
55
56impl<K, V> SliceStorage<K, V> for SecondaryMap<K, V>
57where
58    K: Key,
59{
60    #[inline(always)]
61    fn insert(&mut self, _value: V) -> K {
62        panic!("SecondaryMap does not support insert; keys must be pre-created")
63    }
64
65    #[inline(always)]
66    fn remove(&mut self, key: K) -> Option<V> {
67        self.remove(key)
68    }
69
70    #[inline(always)]
71    fn get(&self, key: K) -> Option<&V> {
72        self.get(key)
73    }
74
75    #[inline(always)]
76    fn iter(&self) -> Box<dyn Iterator<Item = (K, &V)> + '_> {
77        Box::new(self.iter())
78    }
79
80    #[inline(always)]
81    fn values(&self) -> Box<dyn Iterator<Item = &V> + '_> {
82        Box::new(self.values())
83    }
84
85    #[inline(always)]
86    fn values_mut(&mut self) -> Box<dyn Iterator<Item = &mut V> + '_> {
87        Box::new(self.values_mut())
88    }
89
90    #[inline(always)]
91    fn is_empty(&self) -> bool {
92        self.is_empty()
93    }
94}
95
96
97impl<K, V> SliceStorage<K, V> for SparseSecondaryMap<K, V>
98where
99    K: Key,
100{
101    #[inline(always)]
102    fn insert(&mut self, _value: V) -> K {
103        panic!("SecondaryMap does not support insert; keys must be pre-created")
104    }
105
106    #[inline(always)]
107    fn remove(&mut self, key: K) -> Option<V> {
108        self.remove(key)
109    }
110
111    #[inline(always)]
112    fn get(&self, key: K) -> Option<&V> {
113        self.get(key)
114    }
115
116    #[inline(always)]
117    fn iter(&self) -> Box<dyn Iterator<Item = (K, &V)> + '_> {
118        Box::new(self.iter())
119    }
120
121    #[inline(always)]
122    fn values(&self) -> Box<dyn Iterator<Item = &V> + '_> {
123        Box::new(self.values())
124    }
125
126    #[inline(always)]
127    fn values_mut(&mut self) -> Box<dyn Iterator<Item = &mut V> + '_> {
128        Box::new(self.values_mut())
129    }
130
131    #[inline(always)]
132    fn is_empty(&self) -> bool {
133        self.is_empty()
134    }
135}