Skip to content

Commit

Permalink
Merge pull request #129 from smessmer/feature/into_iter
Browse files Browse the repository at this point in the history
Implement into_iter()
  • Loading branch information
jeromefroe authored Apr 4, 2022
2 parents b5c59c6 + 346d99b commit 7314e3e
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,52 @@ impl<'a, K, V> FusedIterator for IterMut<'a, K, V> {}
unsafe impl<'a, K: Send, V: Send> Send for IterMut<'a, K, V> {}
unsafe impl<'a, K: Sync, V: Sync> Sync for IterMut<'a, K, V> {}

/// An iterator that moves out of a `LruCache`.
///
/// This `struct` is created by the [`into_iter`] method on [`LruCache`][`LruCache`]. See its
/// documentation for more.
///
/// [`into_iter`]: struct.LruCache.html#method.into_iter
/// [`LruCache`]: struct.LruCache.html
pub struct IntoIter<K, V>
where
K: Hash + Eq,
{
cache: LruCache<K, V>,
}

impl<K, V> Iterator for IntoIter<K, V>
where
K: Hash + Eq,
{
type Item = (K, V);

fn next(&mut self) -> Option<(K, V)> {
self.cache.pop_lru()
}

fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.cache.len();
(len, Some(len))
}

fn count(self) -> usize {
self.cache.len()
}
}

impl<K, V> ExactSizeIterator for IntoIter<K, V> where K: Hash + Eq {}
impl<K, V> FusedIterator for IntoIter<K, V> where K: Hash + Eq {}

impl<K: Hash + Eq, V> IntoIterator for LruCache<K, V> {
type Item = (K, V);
type IntoIter = IntoIter<K, V>;

fn into_iter(self) -> IntoIter<K, V> {
IntoIter{cache: self}
}
}

#[cfg(test)]
mod tests {
use super::LruCache;
Expand Down Expand Up @@ -1568,6 +1614,27 @@ mod tests {
assert_eq!(iter_clone.next(), None);
}

#[test]
fn test_into_iter() {
let mut cache = LruCache::new(3);
cache.put("a", 1);
cache.put("b", 2);
cache.put("c", 3);

let mut iter = cache.into_iter();
assert_eq!(iter.len(), 3);
assert_eq!(iter.next(), Some(("a", 1)));

assert_eq!(iter.len(), 2);
assert_eq!(iter.next(), Some(("b", 2)));

assert_eq!(iter.len(), 1);
assert_eq!(iter.next(), Some(("c", 3)));

assert_eq!(iter.len(), 0);
assert_eq!(iter.next(), None);
}

#[test]
fn test_that_pop_actually_detaches_node() {
let mut cache = LruCache::new(5);
Expand Down

0 comments on commit 7314e3e

Please sign in to comment.