concision_traits/
store.rs

1/*
2    Appellation: key_value <module>
3    Created At: 2026.01.13:20:49:22
4    Contrib: @FL03
5*/
6/// The [`StoreEntry`] trait establishes a common interface for all _entries_ within a
7/// key-value store. These types enable in-place manipulation of key-value pairs by allowing
8/// for keys to point to empty or _vacant_ slots within the store.
9pub trait StoreEntry<'a, K, V> {
10    /// checks if the entry is occupied
11    fn is_occupied(&self) -> bool;
12    /// checks if the entry is vacant
13    fn is_vacant(&self) -> bool;
14}
15
16/// The [`RawStore`] trait is used to define an interface for key-value stores like hash-maps,
17/// dictionaries, and similar data structures.
18pub trait RawStore<K, V> {
19    /// retrieves a reference to a value by key
20    fn get(&self, key: &K) -> Option<&V>;
21    /// returns true if the key is associated with a value in the store
22    fn contains_key(&self, key: &K) -> bool {
23        self.get(key).is_some()
24    }
25}
26/// [`RawStoreMut`] extends the [`RawStore`] trait by introducing various mutable operations
27/// and accessors for elements within the store.
28pub trait RawStoreMut<K, V>: RawStore<K, V> {
29    /// retrieves a mutable reference to a value by key
30    fn get_mut(&mut self, key: &K) -> Option<&mut V>;
31    /// inserts a key-value pair into the store
32    fn insert(&mut self, key: K, value: V) -> Option<V>;
33    /// removes a key-value pair from the store by key
34    fn remove(&mut self, key: &K) -> Option<V>;
35}
36/// The [`Store`] trait is a more robust interface for key-value stores, building upon both
37/// [`RawStore`] and [`RawStoreMut`] traits by introducing an `entry` method for in-place
38/// manipulation of key-value pairs.
39pub trait Store<K, V>: RawStoreMut<K, V> {
40    type Entry<'a>: StoreEntry<'a, K, V>
41    where
42        Self: 'a;
43    /// returns the entry corresponding to the given key for in-place manipulation
44    fn entry<'a>(&'a mut self, key: K) -> Self::Entry<'a>;
45}
46/*
47 ************* Implementations *************
48*/
49
50// macro_rules! impl_store {
51//     (<$($src:ident)::*>::$store:ident<$K:ident, $V:ident $(, $($T:ident),*)?> $(where $($where:tt)*)?) => {
52
53//         impl<$K, $V $(, $($T),*)?> RawStore<$K, $V> for $($src)::*::$store<$K, $V $(, $($T),*)?>  {
54//             fn contains_key(&self, key: &K) -> bool {
55//                 $($src)::*::$store::contains_key(self, key)
56//             }
57
58//             fn get(&self, key: &K) -> Option<&V> {
59//                 $($src)::*::$store::get(self, key)
60//             }
61//         }
62
63//         impl<$K, $V $(, $($T),*)?> RawStoreMut<K, V> for $($src)::*::$store<$K, $V $(, $($T),*)?> $(where $($where)*)? {
64//             fn insert(&mut self, key: K, value: V) -> Option<V> {
65//                 $($src)::*::$store::insert(self, key, value)
66//             }
67
68//             fn get_mut(&mut self, key: &K) -> Option<&mut V> {
69//                 $($src)::*::$store::get_mut(self, key)
70//             }
71
72//             fn remove(&mut self, key: &K) -> Option<V> {
73//                 $($src)::*::$store::remove(self, key)
74//             }
75//         }
76
77//         impl<$K, $V $(, $($T),*)?> Store<K, V> for $($src)::*::$store<$K, $V $(, $($T),*)?> $(where $($where)*)? {
78//             type Entry<'a>
79//                 = $($src)::*::Entry<'a, K, V>
80//             where
81//                 Self: 'a;
82
83//             fn entry<'a>(&'a mut self, key: K) -> Self::Entry<'a> {
84//                 $($src)::*::$store::entry(self, key)
85//             }
86//         }
87//     };
88// }
89
90// #[cfg(feature = "alloc")]
91// impl_store! {
92//     <alloc::collections::btree_map>::BTreeMap<K, V> where K: Ord
93// }
94
95// #[cfg(feature = "hashbrown")]
96// impl_store! {
97//     <hashbrown::hash_map>::HashMap<K, V, S> where K: Eq + core::hash::Hash, S: core::hash::BuildHasher,
98// }
99
100// #[cfg(feature = "std")]
101// impl_store! {
102//     <std::collections::hash_map>::HashMap<K, V> where K: Eq + core::hash::Hash,
103// }
104
105#[cfg(feature = "alloc")]
106mod impl_alloc {
107    use super::*;
108
109    use alloc::collections::btree_map::{self, BTreeMap};
110
111    impl<'a, K, V> StoreEntry<'a, K, V> for btree_map::Entry<'a, K, V> {
112        fn is_occupied(&self) -> bool {
113            matches!(self, btree_map::Entry::Occupied(_))
114        }
115
116        fn is_vacant(&self) -> bool {
117            matches!(self, btree_map::Entry::Vacant(_))
118        }
119    }
120
121    impl<K, V> RawStore<K, V> for BTreeMap<K, V>
122    where
123        K: Ord,
124    {
125        fn contains_key(&self, key: &K) -> bool {
126            BTreeMap::contains_key(self, key)
127        }
128
129        fn get(&self, key: &K) -> Option<&V> {
130            BTreeMap::get(self, key)
131        }
132    }
133
134    impl<K, V> Store<K, V> for BTreeMap<K, V>
135    where
136        K: Ord,
137    {
138        type Entry<'a>
139            = btree_map::Entry<'a, K, V>
140        where
141            Self: 'a;
142
143        fn entry<'a>(&'a mut self, key: K) -> Self::Entry<'a> {
144            BTreeMap::entry(self, key)
145        }
146    }
147
148    impl<K, V> RawStoreMut<K, V> for BTreeMap<K, V>
149    where
150        K: Ord,
151    {
152        fn insert(&mut self, key: K, value: V) -> Option<V> {
153            BTreeMap::insert(self, key, value)
154        }
155
156        fn get_mut(&mut self, key: &K) -> Option<&mut V> {
157            BTreeMap::get_mut(self, key)
158        }
159
160        fn remove(&mut self, key: &K) -> Option<V> {
161            BTreeMap::remove(self, key)
162        }
163    }
164}
165
166#[cfg(feature = "hashbrown")]
167mod impl_hashbrown {
168    use super::*;
169    use core::hash::{BuildHasher, Hash};
170    use hashbrown::hash_map::{self, HashMap};
171
172    impl<K, V, S> StoreEntry<'_, K, V> for hash_map::Entry<'_, K, V, S> {
173        fn is_occupied(&self) -> bool {
174            matches!(self, hash_map::Entry::Occupied(_))
175        }
176
177        fn is_vacant(&self) -> bool {
178            matches!(self, hash_map::Entry::Vacant(_))
179        }
180    }
181
182    impl<K, V, S> RawStore<K, V> for HashMap<K, V, S>
183    where
184        K: Eq + Hash,
185        S: BuildHasher,
186    {
187        fn contains_key(&self, key: &K) -> bool {
188            HashMap::contains_key(self, key)
189        }
190
191        fn get(&self, key: &K) -> Option<&V> {
192            HashMap::get(self, key)
193        }
194    }
195
196    impl<K, V, S> RawStoreMut<K, V> for HashMap<K, V, S>
197    where
198        K: Eq + Hash,
199        S: BuildHasher,
200    {
201        fn insert(&mut self, key: K, value: V) -> Option<V> {
202            HashMap::insert(self, key, value)
203        }
204
205        fn get_mut(&mut self, key: &K) -> Option<&mut V> {
206            HashMap::get_mut(self, key)
207        }
208
209        fn remove(&mut self, key: &K) -> Option<V> {
210            HashMap::remove(self, key)
211        }
212    }
213
214    impl<K, V, S> Store<K, V> for HashMap<K, V, S>
215    where
216        K: Eq + Hash,
217        S: BuildHasher,
218    {
219        type Entry<'a>
220            = hash_map::Entry<'a, K, V, S>
221        where
222            Self: 'a;
223
224        fn entry<'a>(&'a mut self, key: K) -> Self::Entry<'a> {
225            HashMap::entry(self, key)
226        }
227    }
228}
229
230#[cfg(feature = "std")]
231mod impl_std {
232    use super::*;
233    use core::hash::Hash;
234    use std::collections::hash_map::{self, HashMap};
235
236    impl<K, V> StoreEntry<'_, K, V> for hash_map::Entry<'_, K, V> {
237        fn is_occupied(&self) -> bool {
238            matches!(self, hash_map::Entry::Occupied(_))
239        }
240
241        fn is_vacant(&self) -> bool {
242            matches!(self, hash_map::Entry::Vacant(_))
243        }
244    }
245
246    impl<K, V> RawStore<K, V> for HashMap<K, V>
247    where
248        K: Eq + Hash,
249    {
250        fn contains_key(&self, key: &K) -> bool {
251            HashMap::contains_key(self, key)
252        }
253
254        fn get(&self, key: &K) -> Option<&V> {
255            HashMap::get(self, key)
256        }
257    }
258
259    impl<K, V> RawStoreMut<K, V> for HashMap<K, V>
260    where
261        K: Eq + Hash,
262    {
263        fn insert(&mut self, key: K, value: V) -> Option<V> {
264            HashMap::insert(self, key, value)
265        }
266        fn get_mut(&mut self, key: &K) -> Option<&mut V> {
267            HashMap::get_mut(self, key)
268        }
269
270        fn remove(&mut self, key: &K) -> Option<V> {
271            HashMap::remove(self, key)
272        }
273    }
274
275    impl<K, V> Store<K, V> for HashMap<K, V>
276    where
277        K: Eq + Hash,
278    {
279        type Entry<'a>
280            = hash_map::Entry<'a, K, V>
281        where
282            Self: 'a;
283
284        fn entry<'a>(&'a mut self, key: K) -> Self::Entry<'a> {
285            HashMap::entry(self, key)
286        }
287    }
288}