zrx_store/store/adapter/collections/iter.rs
1// Copyright (c) 2025-2026 Zensical and contributors
2
3// SPDX-License-Identifier: MIT
4// All contributions are certified under the DCO
5
6// Permission is hereby granted, free of charge, to any person obtaining a copy
7// of this software and associated documentation files (the "Software"), to
8// deal in the Software without restriction, including without limitation the
9// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10// sell copies of the Software, and to permit persons to whom the Software is
11// furnished to do so, subject to the following conditions:
12
13// The above copyright notice and this permission notice shall be included in
14// all copies or substantial portions of the Software.
15
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22// IN THE SOFTWARE.
23
24// ----------------------------------------------------------------------------
25
26//! Iterator implementations for collections.
27
28use std::collections::btree_map::{self, BTreeMap};
29use std::collections::hash_map::{self, HashMap};
30use std::hash::BuildHasher;
31use std::ops::RangeBounds;
32
33use crate::store::item::{Key, Value};
34use crate::store::{
35 StoreIterable, StoreIterableMut, StoreKeys, StoreRange, StoreValues,
36};
37
38// ----------------------------------------------------------------------------
39// Trait implementations
40// ----------------------------------------------------------------------------
41
42impl<K, V, S> StoreIterable<K, V> for HashMap<K, V, S>
43where
44 K: Key,
45 V: Value,
46 S: BuildHasher,
47{
48 type Iter<'a> = hash_map::Iter<'a, K, V>
49 where
50 Self: 'a;
51
52 /// Creates an iterator over the items of the store.
53 ///
54 /// # Examples
55 ///
56 /// ```
57 /// use std::collections::HashMap;
58 /// use zrx_store::StoreMut;
59 ///
60 /// // Create store and initial state
61 /// let mut store = HashMap::new();
62 /// store.insert("key", 42);
63 ///
64 /// // Create iterator over the store
65 /// for (key, value) in store {
66 /// println!("{key}: {value}");
67 /// }
68 /// ```
69 #[inline]
70 fn iter(&self) -> Self::Iter<'_> {
71 HashMap::iter(self)
72 }
73}
74
75impl<K, V, S> StoreIterableMut<K, V> for HashMap<K, V, S>
76where
77 K: Key,
78 V: Value,
79 S: BuildHasher,
80{
81 type IterMut<'a> = hash_map::IterMut<'a, K, V>
82 where
83 Self: 'a;
84
85 /// Creates a mutable iterator over the items of the store.
86 ///
87 /// # Examples
88 ///
89 /// ```
90 /// use std::collections::HashMap;
91 /// use zrx_store::{StoreIterableMut, StoreMut};
92 ///
93 /// // Create store and initial state
94 /// let mut store = HashMap::new();
95 /// store.insert("key", 42);
96 ///
97 /// // Create iterator over the store
98 /// for (key, value) in store.iter_mut() {
99 /// println!("{key}: {value}");
100 /// }
101 /// ```
102 #[inline]
103 fn iter_mut(&mut self) -> Self::IterMut<'_> {
104 HashMap::iter_mut(self)
105 }
106}
107
108impl<K, V, S> StoreKeys<K, V> for HashMap<K, V, S>
109where
110 K: Key,
111 S: BuildHasher,
112{
113 type Keys<'a> = hash_map::Keys<'a, K, V>
114 where
115 Self: 'a;
116
117 /// Creates an iterator over the keys of the store.
118 ///
119 /// # Examples
120 ///
121 /// ```
122 /// use std::collections::HashMap;
123 /// use zrx_store::{StoreKeys, StoreMut};
124 ///
125 /// // Create store and initial state
126 /// let mut store = HashMap::new();
127 /// store.insert("key", 42);
128 ///
129 /// // Create iterator over the store
130 /// for key in store.keys() {
131 /// println!("{key}");
132 /// }
133 /// ```
134 #[inline]
135 fn keys(&self) -> Self::Keys<'_> {
136 HashMap::keys(self)
137 }
138}
139
140impl<K, V, S> StoreValues<K, V> for HashMap<K, V, S>
141where
142 K: Key,
143 V: Value,
144 S: BuildHasher,
145{
146 type Values<'a> = hash_map::Values<'a, K, V>
147 where
148 Self: 'a;
149
150 /// Creates an iterator over the values of the store.
151 ///
152 /// # Examples
153 ///
154 /// ```
155 /// use std::collections::HashMap;
156 /// use zrx_store::{StoreMut, StoreValues};
157 ///
158 /// // Create store and initial state
159 /// let mut store = HashMap::new();
160 /// store.insert("key", 42);
161 ///
162 /// // Create iterator over the store
163 /// for value in store.values() {
164 /// println!("{value}");
165 /// }
166 /// ```
167 #[inline]
168 fn values(&self) -> Self::Values<'_> {
169 HashMap::values(self)
170 }
171}
172
173// ----------------------------------------------------------------------------
174
175impl<K, V> StoreIterable<K, V> for BTreeMap<K, V>
176where
177 K: Key,
178 V: Value,
179{
180 type Iter<'a> = btree_map::Iter<'a, K, V>
181 where
182 Self: 'a;
183
184 /// Creates an iterator over the items of the store.
185 ///
186 /// # Examples
187 ///
188 /// ```
189 /// use std::collections::BTreeMap;
190 /// use zrx_store::{StoreIterable, StoreMut};
191 ///
192 /// // Create store and initial state
193 /// let mut store = BTreeMap::new();
194 /// store.insert("key", 42);
195 ///
196 /// // Create iterator over the store
197 /// for (key, value) in store.iter() {
198 /// println!("{key}: {value}");
199 /// }
200 /// ```
201 #[inline]
202 fn iter(&self) -> Self::Iter<'_> {
203 BTreeMap::iter(self)
204 }
205}
206
207impl<K, V> StoreIterableMut<K, V> for BTreeMap<K, V>
208where
209 K: Key,
210 V: Value,
211{
212 type IterMut<'a> = btree_map::IterMut<'a, K, V>
213 where
214 Self: 'a;
215
216 /// Creates a mutable iterator over the items of the store.
217 ///
218 /// # Examples
219 ///
220 /// ```
221 /// use std::collections::BTreeMap;
222 /// use zrx_store::{StoreIterableMut, StoreMut};
223 ///
224 /// // Create store and initial state
225 /// let mut store = BTreeMap::new();
226 /// store.insert("key", 42);
227 ///
228 /// // Create iterator over the store
229 /// for (key, value) in store.iter_mut() {
230 /// println!("{key}: {value}");
231 /// }
232 /// ```
233 #[inline]
234 fn iter_mut(&mut self) -> Self::IterMut<'_> {
235 BTreeMap::iter_mut(self)
236 }
237}
238
239impl<K, V> StoreKeys<K, V> for BTreeMap<K, V>
240where
241 K: Key,
242{
243 type Keys<'a> = btree_map::Keys<'a, K, V>
244 where
245 Self: 'a;
246
247 /// Creates an iterator over the keys of the store.
248 ///
249 /// # Examples
250 ///
251 /// ```
252 /// use std::collections::BTreeMap;
253 /// use zrx_store::{StoreKeys, StoreMut};
254 ///
255 /// // Create store and initial state
256 /// let mut store = BTreeMap::new();
257 /// store.insert("key", 42);
258 ///
259 /// // Create iterator over the store
260 /// for key in store.keys() {
261 /// println!("{key}");
262 /// }
263 /// ```
264 #[inline]
265 fn keys(&self) -> Self::Keys<'_> {
266 BTreeMap::keys(self)
267 }
268}
269
270impl<K, V> StoreValues<K, V> for BTreeMap<K, V>
271where
272 K: Key,
273 V: Value,
274{
275 type Values<'a> = btree_map::Values<'a, K, V>
276 where
277 Self: 'a;
278
279 /// Creates an iterator over the values of the store.
280 ///
281 /// # Examples
282 ///
283 /// ```
284 /// use std::collections::BTreeMap;
285 /// use zrx_store::{StoreMut, StoreValues};
286 ///
287 /// // Create store and initial state
288 /// let mut store = BTreeMap::new();
289 /// store.insert("key", 42);
290 ///
291 /// // Create iterator over the store
292 /// for value in store.values() {
293 /// println!("{value}");
294 /// }
295 /// ```
296 #[inline]
297 fn values(&self) -> Self::Values<'_> {
298 BTreeMap::values(self)
299 }
300}
301
302impl<K, V> StoreRange<K, V> for BTreeMap<K, V>
303where
304 K: Key,
305 V: Value,
306{
307 type Range<'a> = btree_map::Range<'a, K, V>
308 where
309 Self: 'a;
310
311 /// Creates an iterator over a range of items in a store.
312 ///
313 /// # Examples
314 ///
315 /// ```
316 /// use std::collections::BTreeMap;
317 /// use zrx_store::{StoreMut, StoreRange};
318 ///
319 /// // Create store and initial state
320 /// let mut store = BTreeMap::new();
321 /// store.insert("a", 42);
322 /// store.insert("b", 84);
323 ///
324 /// // Create iterator over the store
325 /// for (key, value) in store.range("b"..) {
326 /// println!("{key}: {value}");
327 /// }
328 /// ```
329 #[inline]
330 fn range<R>(&self, range: R) -> Self::Range<'_>
331 where
332 R: RangeBounds<K>,
333 {
334 BTreeMap::range(self, range)
335 }
336}