zrx_store/store/adapter/slab/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 [`Slab`].
27
28use slab::Slab;
29
30use crate::store::item::{Key, Value};
31use crate::store::{StoreIterable, StoreIterableMut, StoreKeys, StoreValues};
32
33// ----------------------------------------------------------------------------
34// Structs
35// ----------------------------------------------------------------------------
36
37/// Iterator over the items of a [`Slab`].
38#[derive(Debug)]
39pub struct Iter<'a, K, V> {
40 /// Inner iterator.
41 inner: slab::Iter<'a, (K, V)>,
42}
43
44/// Mutable iterator over the items of a [`Slab`].
45#[derive(Debug)]
46pub struct IterMut<'a, K, V> {
47 /// Inner iterator.
48 inner: slab::IterMut<'a, (K, V)>,
49}
50
51/// Iterator over the keys of a [`Slab`].
52#[derive(Debug)]
53pub struct Keys<'a, K, V> {
54 /// Inner iterator.
55 inner: slab::Iter<'a, (K, V)>,
56}
57
58/// Iterator over the values of a [`Slab`].
59#[derive(Debug)]
60pub struct Values<'a, K, V> {
61 /// Inner iterator.
62 inner: slab::Iter<'a, (K, V)>,
63}
64
65// ----------------------------------------------------------------------------
66// Trait implementations
67// ----------------------------------------------------------------------------
68
69impl<K, V> StoreIterable<K, V> for Slab<(K, V)>
70where
71 K: Key,
72 V: Value,
73{
74 type Iter<'a> = Iter<'a, K, V>
75 where
76 Self: 'a;
77
78 /// Creates an iterator over the items of the store.
79 ///
80 /// # Examples
81 ///
82 /// ```
83 /// use slab::Slab;
84 /// use zrx_store::{StoreIterable, StoreMut};
85 ///
86 /// // Create store and initial state
87 /// let mut store = Slab::new();
88 /// StoreMut::insert(&mut store, "key", 42);
89 ///
90 /// // Create iterator over the store
91 /// for (key, value) in StoreIterable::iter(&store) {
92 /// println!("{key}: {value}");
93 /// }
94 /// ```
95 #[inline]
96 fn iter(&self) -> Self::Iter<'_> {
97 Iter { inner: Slab::iter(self) }
98 }
99}
100
101impl<K, V> StoreIterableMut<K, V> for Slab<(K, V)>
102where
103 K: Key,
104 V: Value,
105{
106 type IterMut<'a> = IterMut<'a, K, V>
107 where
108 Self: 'a;
109
110 /// Creates a mutable iterator over the items of the store.
111 ///
112 /// # Examples
113 ///
114 /// ```
115 /// use slab::Slab;
116 /// use zrx_store::{StoreIterableMut, StoreMut};
117 ///
118 /// // Create store and initial state
119 /// let mut store = Slab::new();
120 /// StoreMut::insert(&mut store, "key", 42);
121 ///
122 /// // Create iterator over the store
123 /// for (key, value) in StoreIterableMut::iter_mut(&mut store) {
124 /// println!("{key}: {value}");
125 /// }
126 /// ```
127 #[inline]
128 fn iter_mut(&mut self) -> Self::IterMut<'_> {
129 IterMut { inner: Slab::iter_mut(self) }
130 }
131}
132
133impl<K, V> StoreKeys<K, V> for Slab<(K, V)>
134where
135 K: Key,
136{
137 type Keys<'a> = Keys<'a, K, V>
138 where
139 Self: 'a;
140
141 /// Creates an iterator over the keys of the store.
142 ///
143 /// # Examples
144 ///
145 /// ```
146 /// use slab::Slab;
147 /// use zrx_store::{StoreKeys, StoreMut};
148 ///
149 /// // Create store and initial state
150 /// let mut store = Slab::new();
151 /// StoreMut::insert(&mut store, "key", 42);
152 ///
153 /// // Create iterator over the store
154 /// for key in StoreKeys::keys(&store) {
155 /// println!("{key}");
156 /// }
157 /// ```
158 #[inline]
159 fn keys(&self) -> Self::Keys<'_> {
160 Keys { inner: Slab::iter(self) }
161 }
162}
163
164impl<K, V> StoreValues<K, V> for Slab<(K, V)>
165where
166 K: Key,
167 V: Value,
168{
169 type Values<'a> = Values<'a, K, V>
170 where
171 Self: 'a;
172
173 /// Creates an iterator over the values of the store.
174 ///
175 /// # Examples
176 ///
177 /// ```
178 /// use slab::Slab;
179 /// use zrx_store::{StoreValues, StoreMut};
180 ///
181 /// // Create store and initial state
182 /// let mut store = Slab::new();
183 /// StoreMut::insert(&mut store, "key", 42);
184 ///
185 /// // Create iterator over the store
186 /// for key in StoreValues::values(&store) {
187 /// println!("{key}");
188 /// }
189 /// ```
190 #[inline]
191 fn values(&self) -> Self::Values<'_> {
192 Values { inner: Slab::iter(self) }
193 }
194}
195
196// ----------------------------------------------------------------------------
197
198impl<'a, K, V> Iterator for Iter<'a, K, V>
199where
200 K: Key,
201{
202 type Item = (&'a K, &'a V);
203
204 /// Returns the next item.
205 #[inline]
206 fn next(&mut self) -> Option<Self::Item> {
207 self.inner.next().map(|(_, (key, value))| (key, value))
208 }
209
210 /// Returns the bounds on the remaining length of the iterator.
211 #[inline]
212 fn size_hint(&self) -> (usize, Option<usize>) {
213 self.inner.size_hint()
214 }
215}
216
217impl<K, V> ExactSizeIterator for Iter<'_, K, V>
218where
219 K: Key,
220{
221 /// Returns the exact remaining length of the iterator.
222 #[inline]
223 fn len(&self) -> usize {
224 self.inner.len()
225 }
226}
227
228// ----------------------------------------------------------------------------
229
230impl<'a, K, V> Iterator for IterMut<'a, K, V>
231where
232 K: Key,
233{
234 type Item = (&'a K, &'a mut V);
235
236 /// Returns the next item.
237 #[inline]
238 fn next(&mut self) -> Option<Self::Item> {
239 self.inner.next().map(|(_, (key, value))| (&*key, value))
240 }
241
242 /// Returns the bounds on the remaining length of the iterator.
243 #[inline]
244 fn size_hint(&self) -> (usize, Option<usize>) {
245 self.inner.size_hint()
246 }
247}
248
249impl<K, V> ExactSizeIterator for IterMut<'_, K, V>
250where
251 K: Key,
252{
253 /// Returns the exact remaining length of the iterator.
254 #[inline]
255 fn len(&self) -> usize {
256 self.inner.len()
257 }
258}
259
260// ----------------------------------------------------------------------------
261
262impl<'a, K, V> Iterator for Keys<'a, K, V>
263where
264 K: Key,
265{
266 type Item = &'a K;
267
268 /// Returns the next item.
269 #[inline]
270 fn next(&mut self) -> Option<Self::Item> {
271 self.inner.next().map(|(_, (key, _))| key)
272 }
273
274 /// Returns the bounds on the remaining length of the iterator.
275 #[inline]
276 fn size_hint(&self) -> (usize, Option<usize>) {
277 self.inner.size_hint()
278 }
279}
280
281impl<K, V> ExactSizeIterator for Keys<'_, K, V>
282where
283 K: Key,
284{
285 /// Returns the exact remaining length of the iterator.
286 #[inline]
287 fn len(&self) -> usize {
288 self.inner.len()
289 }
290}
291
292// ----------------------------------------------------------------------------
293
294impl<'a, K, V> Iterator for Values<'a, K, V>
295where
296 K: Key,
297{
298 type Item = &'a V;
299
300 /// Returns the next item.
301 #[inline]
302 fn next(&mut self) -> Option<Self::Item> {
303 self.inner.next().map(|(_, (_, value))| value)
304 }
305
306 /// Returns the bounds on the remaining length of the iterator.
307 #[inline]
308 fn size_hint(&self) -> (usize, Option<usize>) {
309 self.inner.size_hint()
310 }
311}
312
313impl<K, V> ExactSizeIterator for Values<'_, K, V>
314where
315 K: Key,
316{
317 /// Returns the exact remaining length of the iterator.
318 #[inline]
319 fn len(&self) -> usize {
320 self.inner.len()
321 }
322}