unc_sdk/store/iterable_map/
iter.rs

1use std::iter::FusedIterator;
2
3use borsh::{BorshDeserialize, BorshSerialize};
4
5use super::{IterableMap, LookupMap, ToKey, ValueAndIndex, ERR_INCONSISTENT_STATE};
6use crate::env;
7use crate::store::vec;
8
9impl<'a, K, V, H> IntoIterator for &'a IterableMap<K, V, H>
10where
11    K: BorshSerialize + Ord + BorshDeserialize + Clone,
12    V: BorshSerialize + BorshDeserialize,
13    H: ToKey,
14{
15    type Item = (&'a K, &'a V);
16    type IntoIter = Iter<'a, K, V, H>;
17
18    fn into_iter(self) -> Self::IntoIter {
19        self.iter()
20    }
21}
22
23impl<'a, K, V, H> IntoIterator for &'a mut IterableMap<K, V, H>
24where
25    K: BorshSerialize + Ord + BorshDeserialize + Clone,
26    V: BorshSerialize + BorshDeserialize,
27    H: ToKey,
28{
29    type Item = (&'a K, &'a mut V);
30    type IntoIter = IterMut<'a, K, V, H>;
31
32    fn into_iter(self) -> Self::IntoIter {
33        self.iter_mut()
34    }
35}
36
37/// An iterator over elements of a [`IterableMap`].
38///
39/// This `struct` is created by the `iter` method on [`IterableMap`].
40pub struct Iter<'a, K, V, H>
41where
42    K: BorshSerialize + Ord + BorshDeserialize,
43    V: BorshSerialize,
44    H: ToKey,
45{
46    /// Values iterator which contains empty and filled cells.
47    keys: vec::Iter<'a, K>,
48    /// Reference to underlying map to lookup values with `keys`.
49    values: &'a LookupMap<K, ValueAndIndex<V>, H>,
50}
51
52impl<'a, K, V, H> Iter<'a, K, V, H>
53where
54    K: BorshSerialize + Ord + BorshDeserialize,
55    V: BorshSerialize,
56    H: ToKey,
57{
58    pub(super) fn new(map: &'a IterableMap<K, V, H>) -> Self {
59        Self { keys: map.keys.iter(), values: &map.values }
60    }
61}
62
63impl<'a, K, V, H> Iterator for Iter<'a, K, V, H>
64where
65    K: BorshSerialize + Ord + BorshDeserialize + Clone,
66    V: BorshSerialize + BorshDeserialize,
67    H: ToKey,
68{
69    type Item = (&'a K, &'a V);
70
71    fn next(&mut self) -> Option<Self::Item> {
72        <Self as Iterator>::nth(self, 0)
73    }
74
75    fn nth(&mut self, n: usize) -> Option<Self::Item> {
76        let key = self.keys.nth(n)?;
77        let entry = self.values.get(key).unwrap_or_else(|| env::panic_str(ERR_INCONSISTENT_STATE));
78
79        Some((key, &entry.value))
80    }
81
82    fn size_hint(&self) -> (usize, Option<usize>) {
83        self.keys.size_hint()
84    }
85
86    fn count(self) -> usize {
87        self.keys.count()
88    }
89}
90
91impl<'a, K, V, H> ExactSizeIterator for Iter<'a, K, V, H>
92where
93    K: BorshSerialize + Ord + BorshDeserialize + Clone,
94    V: BorshSerialize + BorshDeserialize,
95    H: ToKey,
96{
97}
98impl<'a, K, V, H> FusedIterator for Iter<'a, K, V, H>
99where
100    K: BorshSerialize + Ord + BorshDeserialize + Clone,
101    V: BorshSerialize + BorshDeserialize,
102    H: ToKey,
103{
104}
105
106impl<'a, K, V, H> DoubleEndedIterator for Iter<'a, K, V, H>
107where
108    K: BorshSerialize + Ord + BorshDeserialize + Clone,
109    V: BorshSerialize + BorshDeserialize,
110    H: ToKey,
111{
112    fn next_back(&mut self) -> Option<Self::Item> {
113        <Self as DoubleEndedIterator>::nth_back(self, 0)
114    }
115
116    fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
117        let key = self.keys.nth_back(n)?;
118        let entry = self.values.get(key).unwrap_or_else(|| env::panic_str(ERR_INCONSISTENT_STATE));
119
120        Some((key, &entry.value))
121    }
122}
123
124/// A mutable iterator over elements of a [`IterableMap`].
125///
126/// This `struct` is created by the `iter_mut` method on [`IterableMap`].
127pub struct IterMut<'a, K, V, H>
128where
129    K: BorshSerialize + Ord + BorshDeserialize,
130    V: BorshSerialize,
131    H: ToKey,
132{
133    /// Values iterator which contains empty and filled cells.
134    keys: vec::Iter<'a, K>,
135    /// Exclusive reference to underlying map to lookup values with `keys`.
136    values: &'a mut LookupMap<K, ValueAndIndex<V>, H>,
137}
138
139impl<'a, K, V, H> IterMut<'a, K, V, H>
140where
141    K: BorshSerialize + Ord + BorshDeserialize,
142    V: BorshSerialize,
143    H: ToKey,
144{
145    pub(super) fn new(map: &'a mut IterableMap<K, V, H>) -> Self {
146        Self { keys: map.keys.iter(), values: &mut map.values }
147    }
148    fn get_entry_mut<'b>(&'b mut self, key: &'a K) -> (&'a K, &'a mut V)
149    where
150        K: Clone,
151        V: BorshDeserialize,
152    {
153        let entry =
154            self.values.get_mut(key).unwrap_or_else(|| env::panic_str(ERR_INCONSISTENT_STATE));
155        //* SAFETY: The lifetime can be swapped here because we can assert that the iterator
156        //*         will only give out one mutable reference for every individual key in the bucket
157        //*         during the iteration, and there is no overlap. This operates under the
158        //*         assumption that all elements in the bucket are unique and no hash collisions.
159        //*         Because we use 32 byte hashes and all keys are verified unique based on the
160        //*         `IterableMap` API, this is safe.
161        let value = unsafe { &mut *(&mut entry.value as *mut V) };
162        (key, value)
163    }
164}
165
166impl<'a, K, V, H> Iterator for IterMut<'a, K, V, H>
167where
168    K: BorshSerialize + Ord + BorshDeserialize + Clone,
169    V: BorshSerialize + BorshDeserialize,
170    H: ToKey,
171{
172    type Item = (&'a K, &'a mut V);
173
174    fn next(&mut self) -> Option<Self::Item> {
175        <Self as Iterator>::nth(self, 0)
176    }
177
178    fn nth(&mut self, n: usize) -> Option<Self::Item> {
179        let key = self.keys.nth(n)?;
180        Some(self.get_entry_mut(key))
181    }
182
183    fn size_hint(&self) -> (usize, Option<usize>) {
184        self.keys.size_hint()
185    }
186
187    fn count(self) -> usize {
188        self.keys.count()
189    }
190}
191
192impl<'a, K, V, H> ExactSizeIterator for IterMut<'a, K, V, H>
193where
194    K: BorshSerialize + Ord + BorshDeserialize + Clone,
195    V: BorshSerialize + BorshDeserialize,
196    H: ToKey,
197{
198}
199impl<'a, K, V, H> FusedIterator for IterMut<'a, K, V, H>
200where
201    K: BorshSerialize + Ord + BorshDeserialize + Clone,
202    V: BorshSerialize + BorshDeserialize,
203    H: ToKey,
204{
205}
206
207impl<'a, K, V, H> DoubleEndedIterator for IterMut<'a, K, V, H>
208where
209    K: BorshSerialize + Ord + BorshDeserialize + Clone,
210    V: BorshSerialize + BorshDeserialize,
211    H: ToKey,
212{
213    fn next_back(&mut self) -> Option<Self::Item> {
214        <Self as DoubleEndedIterator>::nth_back(self, 0)
215    }
216
217    fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
218        let key = self.keys.nth_back(n)?;
219        Some(self.get_entry_mut(key))
220    }
221}
222
223/// An iterator over the keys of a [`IterableMap`].
224///
225/// This `struct` is created by the `keys` method on [`IterableMap`].
226pub struct Keys<'a, K: 'a>
227where
228    K: BorshSerialize + BorshDeserialize,
229{
230    inner: vec::Iter<'a, K>,
231}
232
233impl<'a, K> Keys<'a, K>
234where
235    K: BorshSerialize + BorshDeserialize,
236{
237    pub(super) fn new<V, H>(map: &'a IterableMap<K, V, H>) -> Self
238    where
239        K: Ord,
240        V: BorshSerialize,
241        H: ToKey,
242    {
243        Self { inner: map.keys.iter() }
244    }
245}
246
247impl<'a, K> Iterator for Keys<'a, K>
248where
249    K: BorshSerialize + BorshDeserialize,
250{
251    type Item = &'a K;
252
253    fn next(&mut self) -> Option<&'a K> {
254        self.inner.next()
255    }
256
257    fn size_hint(&self) -> (usize, Option<usize>) {
258        self.inner.size_hint()
259    }
260
261    fn count(self) -> usize {
262        self.inner.count()
263    }
264}
265
266impl<'a, K> ExactSizeIterator for Keys<'a, K> where K: BorshSerialize + BorshDeserialize {}
267impl<'a, K> FusedIterator for Keys<'a, K> where K: BorshSerialize + BorshDeserialize {}
268
269impl<'a, K> DoubleEndedIterator for Keys<'a, K>
270where
271    K: BorshSerialize + Ord + BorshDeserialize,
272{
273    fn next_back(&mut self) -> Option<&'a K> {
274        self.inner.next_back()
275    }
276}
277
278/// An iterator over the values of a [`IterableMap`].
279///
280/// This `struct` is created by the `values` method on [`IterableMap`].
281pub struct Values<'a, K, V, H>
282where
283    K: BorshSerialize + Ord + BorshDeserialize,
284    V: BorshSerialize,
285    H: ToKey,
286{
287    inner: Iter<'a, K, V, H>,
288}
289
290impl<'a, K, V, H> Values<'a, K, V, H>
291where
292    K: BorshSerialize + Ord + BorshDeserialize,
293    V: BorshSerialize,
294    H: ToKey,
295{
296    pub(super) fn new(map: &'a IterableMap<K, V, H>) -> Self {
297        Self { inner: map.iter() }
298    }
299}
300
301impl<'a, K, V, H> Iterator for Values<'a, K, V, H>
302where
303    K: BorshSerialize + Ord + BorshDeserialize + Clone,
304    V: BorshSerialize + BorshDeserialize,
305    H: ToKey,
306{
307    type Item = &'a V;
308
309    fn next(&mut self) -> Option<Self::Item> {
310        <Self as Iterator>::nth(self, 0)
311    }
312
313    fn nth(&mut self, n: usize) -> Option<Self::Item> {
314        self.inner.nth(n).map(|(_, v)| v)
315    }
316
317    fn size_hint(&self) -> (usize, Option<usize>) {
318        self.inner.size_hint()
319    }
320
321    fn count(self) -> usize {
322        self.inner.count()
323    }
324}
325
326impl<'a, K, V, H> ExactSizeIterator for Values<'a, K, V, H>
327where
328    K: BorshSerialize + Ord + BorshDeserialize + Clone,
329    V: BorshSerialize + BorshDeserialize,
330    H: ToKey,
331{
332}
333impl<'a, K, V, H> FusedIterator for Values<'a, K, V, H>
334where
335    K: BorshSerialize + Ord + BorshDeserialize + Clone,
336    V: BorshSerialize + BorshDeserialize,
337    H: ToKey,
338{
339}
340
341impl<'a, K, V, H> DoubleEndedIterator for Values<'a, K, V, H>
342where
343    K: BorshSerialize + Ord + BorshDeserialize + Clone,
344    V: BorshSerialize + BorshDeserialize,
345    H: ToKey,
346{
347    fn next_back(&mut self) -> Option<Self::Item> {
348        <Self as DoubleEndedIterator>::nth_back(self, 0)
349    }
350
351    fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
352        self.inner.nth_back(n).map(|(_, v)| v)
353    }
354}
355
356/// A mutable iterator over values of a [`IterableMap`].
357///
358/// This `struct` is created by the `values_mut` method on [`IterableMap`].
359pub struct ValuesMut<'a, K, V, H>
360where
361    K: BorshSerialize + Ord + BorshDeserialize,
362    V: BorshSerialize,
363    H: ToKey,
364{
365    inner: IterMut<'a, K, V, H>,
366}
367
368impl<'a, K, V, H> ValuesMut<'a, K, V, H>
369where
370    K: BorshSerialize + Ord + BorshDeserialize,
371    V: BorshSerialize,
372    H: ToKey,
373{
374    pub(super) fn new(map: &'a mut IterableMap<K, V, H>) -> Self {
375        Self { inner: map.iter_mut() }
376    }
377}
378
379impl<'a, K, V, H> Iterator for ValuesMut<'a, K, V, H>
380where
381    K: BorshSerialize + Ord + BorshDeserialize + Clone,
382    V: BorshSerialize + BorshDeserialize,
383    H: ToKey,
384{
385    type Item = &'a mut V;
386
387    fn next(&mut self) -> Option<Self::Item> {
388        <Self as Iterator>::nth(self, 0)
389    }
390
391    fn nth(&mut self, n: usize) -> Option<Self::Item> {
392        self.inner.nth(n).map(|(_, v)| v)
393    }
394
395    fn size_hint(&self) -> (usize, Option<usize>) {
396        self.inner.size_hint()
397    }
398
399    fn count(self) -> usize {
400        self.inner.count()
401    }
402}
403
404impl<'a, K, V, H> ExactSizeIterator for ValuesMut<'a, K, V, H>
405where
406    K: BorshSerialize + Ord + BorshDeserialize + Clone,
407    V: BorshSerialize + BorshDeserialize,
408    H: ToKey,
409{
410}
411impl<'a, K, V, H> FusedIterator for ValuesMut<'a, K, V, H>
412where
413    K: BorshSerialize + Ord + BorshDeserialize + Clone,
414    V: BorshSerialize + BorshDeserialize,
415    H: ToKey,
416{
417}
418
419impl<'a, K, V, H> DoubleEndedIterator for ValuesMut<'a, K, V, H>
420where
421    K: BorshSerialize + Ord + BorshDeserialize + Clone,
422    V: BorshSerialize + BorshDeserialize,
423    H: ToKey,
424{
425    fn next_back(&mut self) -> Option<Self::Item> {
426        <Self as DoubleEndedIterator>::nth_back(self, 0)
427    }
428
429    fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
430        self.inner.nth_back(n).map(|(_, v)| v)
431    }
432}
433
434/// A draining iterator for [`IterableMap<K, V, H>`].
435#[derive(Debug)]
436pub struct Drain<'a, K, V, H>
437where
438    K: BorshSerialize + BorshDeserialize + Ord,
439    V: BorshSerialize,
440    H: ToKey,
441{
442    keys: vec::Drain<'a, K>,
443    values: &'a mut LookupMap<K, ValueAndIndex<V>, H>,
444}
445
446impl<'a, K, V, H> Drain<'a, K, V, H>
447where
448    K: BorshSerialize + BorshDeserialize + Ord,
449    V: BorshSerialize,
450    H: ToKey,
451{
452    pub(crate) fn new(list: &'a mut IterableMap<K, V, H>) -> Self {
453        Self { keys: list.keys.drain(..), values: &mut list.values }
454    }
455
456    fn remaining(&self) -> usize {
457        self.keys.remaining()
458    }
459
460    fn remove_value(&mut self, key: K) -> (K, V)
461    where
462        K: Clone,
463        V: BorshDeserialize,
464    {
465        let value = self
466            .values
467            .remove(&key)
468            .unwrap_or_else(|| env::panic_str(ERR_INCONSISTENT_STATE))
469            .value;
470
471        (key, value)
472    }
473}
474
475impl<'a, K, V, H> Iterator for Drain<'a, K, V, H>
476where
477    K: BorshSerialize + BorshDeserialize + Ord + Clone,
478    V: BorshSerialize + BorshDeserialize,
479    H: ToKey,
480{
481    type Item = (K, V);
482
483    fn next(&mut self) -> Option<Self::Item> {
484        let key = self.keys.next()?;
485        Some(self.remove_value(key))
486    }
487
488    fn size_hint(&self) -> (usize, Option<usize>) {
489        let remaining = self.remaining();
490        (remaining, Some(remaining))
491    }
492
493    fn count(self) -> usize {
494        self.remaining()
495    }
496}
497
498impl<'a, K, V, H> ExactSizeIterator for Drain<'a, K, V, H>
499where
500    K: BorshSerialize + Ord + BorshDeserialize + Clone,
501    V: BorshSerialize + BorshDeserialize,
502    H: ToKey,
503{
504}
505
506impl<'a, K, V, H> FusedIterator for Drain<'a, K, V, H>
507where
508    K: BorshSerialize + Ord + BorshDeserialize + Clone,
509    V: BorshSerialize + BorshDeserialize,
510    H: ToKey,
511{
512}
513
514impl<'a, K, V, H> DoubleEndedIterator for Drain<'a, K, V, H>
515where
516    K: BorshSerialize + Ord + BorshDeserialize + Clone,
517    V: BorshSerialize + BorshDeserialize,
518    H: ToKey,
519{
520    fn next_back(&mut self) -> Option<Self::Item> {
521        let key = self.keys.next_back()?;
522        Some(self.remove_value(key))
523    }
524}