1#![allow(unused_macros)]
6
7pub trait Entry<'a> {
8 type Key;
9 type Value;
10
11 fn key(&self) -> &Self::Key;
12
13 fn or_insert(self, default: Self::Value) -> &'a mut Self::Value;
14}
15
16pub trait Store<K, V> {
17 fn get(&self, key: &K) -> Option<&V>;
18
19 fn get_mut(&mut self, key: &K) -> Option<&mut V>;
20
21 fn insert(&mut self, key: K, value: V) -> Option<V>;
22
23 fn remove(&mut self, key: &K) -> Option<V>;
24}
25
26macro_rules! entry {
30 ($($prefix:ident)::* -> $call:ident($($arg:tt),*)) => {
31 $($prefix)::*::Entry::$call($($arg),*)
32 };
33
34}
35
36macro_rules! impl_entry {
37 ($($prefix:ident)::* where $($preds:tt)* ) => {
38
39 impl<'a, K, V> Entry<'a> for $($prefix)::*::Entry<'a, K, V> where $($preds)* {
40 type Key = K;
41 type Value = V;
42
43 fn key(&self) -> &Self::Key {
44 entry!($($prefix)::* -> key(self))
45 }
46
47 fn or_insert(self, default: Self::Value) -> &'a mut Self::Value {
48 entry!($($prefix)::* -> or_insert(self, default))
49 }
50 }
51
52 };
53
54}
55
56macro_rules! impl_store {
57 ($t:ty, where $($preds:tt)* ) => {
58
59 impl<K, V> Store<K, V> for $t where $($preds)* {
60 fn get(&self, key: &K) -> Option<&V> {
61 <$t>::get(self, &key)
62 }
63
64 fn get_mut(&mut self, key: &K) -> Option<&mut V> {
65 <$t>::get_mut(self, &key)
66 }
67
68 fn insert(&mut self, key: K, value: V) -> Option<V> {
69 <$t>::insert(self, key, value)
70 }
71
72 fn remove(&mut self, key: &K) -> Option<V> {
73 <$t>::remove(self, &key)
74 }
75 }
76
77 };
78}
79
80#[cfg(feature = "alloc")]
81impl_entry!(alloc::collections::btree_map where K: Ord);
82#[cfg(feature = "alloc")]
83impl_store!(alloc::collections::BTreeMap<K, V>, where K: Ord);
84
85#[cfg(feature = "std")]
86impl_entry!(std::collections::hash_map where K: Eq + core::hash::Hash);
87#[cfg(feature = "std")]
88impl_store!(std::collections::HashMap<K, V>, where K: Eq + core::hash::Hash);