scsys_traits/cont/
store.rs

1/*
2    Appellation: store <module>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5
6pub trait Store<K, V> {
7    fn get(&self, key: &K) -> Option<&V>;
8
9    fn get_mut(&mut self, key: &K) -> Option<&mut V>;
10
11    fn insert(&mut self, key: K, value: V) -> Option<V>;
12
13    fn remove(&mut self, key: &K) -> Option<V>;
14}
15
16/*
17 ********* Implementations *********
18*/
19#[allow(unused_macros)]
20macro_rules! impl_store {
21    ($t:ty, where $($preds:tt)* ) => {
22
23        impl<K, V> Store<K, V> for $t where $($preds)* {
24            fn get(&self, key: &K) -> Option<&V> {
25                <$t>::get(self, &key)
26            }
27
28            fn get_mut(&mut self, key: &K) -> Option<&mut V> {
29                <$t>::get_mut(self, &key)
30            }
31
32            fn insert(&mut self, key: K, value: V) -> Option<V> {
33                <$t>::insert(self, key, value)
34            }
35
36            fn remove(&mut self, key: &K) -> Option<V> {
37                <$t>::remove(self, &key)
38            }
39        }
40
41    };
42}
43
44#[cfg(feature = "alloc")]
45impl_store!(alloc::collections::BTreeMap<K, V>, where K: Ord);
46#[cfg(feature = "std")]
47impl_store!(std::collections::HashMap<K, V>, where K: Eq + core::hash::Hash);