associative_cache/
iter.rs

1//! Various iterator implementations and type definitions for
2//! `AssociativeCache`.
3
4use super::*;
5
6impl<'a, K, V, C, I, R> IntoIterator for &'a AssociativeCache<K, V, C, I, R>
7where
8    C: Capacity,
9    R: Replacement<V, C>,
10{
11    type Item = (&'a K, &'a V);
12    type IntoIter = Iter<'a, K, V>;
13
14    #[inline]
15    fn into_iter(self) -> Self::IntoIter {
16        Iter {
17            len: self.len(),
18            inner: self.entries.iter(),
19        }
20    }
21}
22
23impl<'a, K, V, C, I, R> IntoIterator for &'a mut AssociativeCache<K, V, C, I, R>
24where
25    C: Capacity,
26    R: Replacement<V, C>,
27{
28    type Item = (&'a K, &'a mut V);
29    type IntoIter = IterMut<'a, K, V>;
30
31    #[inline]
32    fn into_iter(self) -> Self::IntoIter {
33        IterMut {
34            len: self.len(),
35            inner: self.entries.iter_mut(),
36        }
37    }
38}
39
40impl<K, V, C, I, R> IntoIterator for AssociativeCache<K, V, C, I, R>
41where
42    C: Capacity,
43    R: Replacement<V, C>,
44{
45    type Item = (K, V);
46    type IntoIter = IntoIter<K, V>;
47
48    #[inline]
49    fn into_iter(self) -> Self::IntoIter {
50        IntoIter {
51            len: self.len(),
52            inner: self.entries.into_iter(),
53        }
54    }
55}
56
57/// An iterator over shared borrows of the cache keys and values.
58///
59/// See `AssociativeCache::iter` for details.
60#[derive(Debug)]
61pub struct Iter<'a, K, V> {
62    len: usize,
63    inner: std::slice::Iter<'a, Option<(K, V)>>,
64}
65
66impl<'a, K, V> Iterator for Iter<'a, K, V> {
67    type Item = (&'a K, &'a V);
68
69    #[inline]
70    fn next(&mut self) -> Option<Self::Item> {
71        loop {
72            match self.inner.next() {
73                None => return None,
74                Some(None) => continue,
75                Some(Some((k, v))) => {
76                    debug_assert!(self.len > 0);
77                    self.len -= 1;
78                    return Some((k, v));
79                }
80            }
81        }
82    }
83
84    #[inline]
85    fn size_hint(&self) -> (usize, Option<usize>) {
86        (self.len, Some(self.len))
87    }
88}
89
90impl<K, V> ExactSizeIterator for Iter<'_, K, V> {}
91
92/// An iterator over shared borrows of the cache keys and mutable borrows of the
93/// cache values.
94///
95/// See `AssociativeCache::iter_mut` for details.
96#[derive(Debug)]
97pub struct IterMut<'a, K, V> {
98    len: usize,
99    inner: std::slice::IterMut<'a, Option<(K, V)>>,
100}
101
102impl<'a, K, V> Iterator for IterMut<'a, K, V> {
103    type Item = (&'a K, &'a mut V);
104
105    #[inline]
106    fn next(&mut self) -> Option<Self::Item> {
107        loop {
108            match self.inner.next() {
109                None => return None,
110                Some(None) => continue,
111                Some(Some((k, v))) => {
112                    debug_assert!(self.len > 0);
113                    self.len -= 1;
114                    return Some((k, v));
115                }
116            }
117        }
118    }
119
120    #[inline]
121    fn size_hint(&self) -> (usize, Option<usize>) {
122        (self.len, Some(self.len))
123    }
124}
125
126impl<K, V> ExactSizeIterator for IterMut<'_, K, V> {}
127
128/// An iterator that consumes and takes ownership of a cache's keys and values.
129///
130/// See `AssociativeCache::into_iter` for details.
131#[derive(Debug)]
132pub struct IntoIter<K, V> {
133    len: usize,
134    inner: std::vec::IntoIter<Option<(K, V)>>,
135}
136
137impl<K, V> Iterator for IntoIter<K, V> {
138    type Item = (K, V);
139
140    #[inline]
141    fn next(&mut self) -> Option<Self::Item> {
142        loop {
143            match self.inner.next() {
144                None => return None,
145                Some(None) => continue,
146                Some(Some(x)) => {
147                    debug_assert!(self.len > 0);
148                    self.len -= 1;
149                    return Some(x);
150                }
151            }
152        }
153    }
154
155    #[inline]
156    fn size_hint(&self) -> (usize, Option<usize>) {
157        (self.len, Some(self.len))
158    }
159}
160
161impl<K, V> ExactSizeIterator for IntoIter<K, V> {}