evicting_cache_map 0.6.0

An Evicting LRU cache supporting prune hooks
Documentation
use ordered_hash_map::ordered_map;

use core::fmt;
use core::iter::FusedIterator;

/// An ordered iterator over the elements of the OrderedHashMap.
///
/// Created by the [`iter`] method of [`EvictingCacheMap`]. See its documentation for details.
///
/// [`iter`]: super::EvictingCacheMap::iter
/// [`EvictingCacheMap`]: super::EvictingCacheMap
pub struct Iter<'a, K, V> {
    inner: ordered_map::Iter<'a, K, V>,
}

impl<K, V> Clone for Iter<'_, K, V> {
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
        }
    }
}

impl<K, V> fmt::Debug for Iter<'_, K, V>
where
    K: fmt::Debug,
    V: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.inner.fmt(f)
    }
}

impl<'a, K, V> Iterator for Iter<'a, K, V> {
    type Item = (&'a K, &'a V);

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        self.inner.next()
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.inner.size_hint()
    }
}

impl<K, V> DoubleEndedIterator for Iter<'_, K, V> {
    #[inline]
    fn next_back(&mut self) -> Option<Self::Item> {
        self.inner.next_back()
    }
}

impl<K, V> ExactSizeIterator for Iter<'_, K, V> {}

impl<K, V> FusedIterator for Iter<'_, K, V> {}

impl<'a, K, V, const N: usize, F, S> IntoIterator for &'a super::EvictingCacheMap<K, V, N, F, S>
where
    F: Fn(K, V),
{
    type Item = (&'a K, &'a V);
    type IntoIter = Iter<'a, K, V>;

    fn into_iter(self) -> Self::IntoIter {
        Self::IntoIter {
            inner: self.inner.iter(),
        }
    }
}

/// An ordered, mutable iterator over the elements of the OrderedHashMap.
///
/// Note that mutation through this iterator does not promote keys.
///
/// Created by the [`iter_mut`] method of [`EvictingCacheMap`]. See its documentation for details.
///
/// [`iter_mut`]: super::EvictingCacheMap::iter_mut
/// [`EvictingCacheMap`]: super::EvictingCacheMap
pub struct IterMut<'a, K, V> {
    inner: ordered_map::IterMut<'a, K, V>,
}

impl<K, V> fmt::Debug for IterMut<'_, K, V>
where
    K: fmt::Debug,
    V: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.inner.fmt(f)
    }
}

impl<'a, K, V> Iterator for IterMut<'a, K, V> {
    type Item = (&'a K, &'a mut V);

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        self.inner.next()
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.inner.size_hint()
    }
}

impl<K, V> DoubleEndedIterator for IterMut<'_, K, V> {
    #[inline]
    fn next_back(&mut self) -> Option<Self::Item> {
        self.inner.next_back()
    }
}

impl<K, V> ExactSizeIterator for IterMut<'_, K, V> {}

impl<K, V> FusedIterator for IterMut<'_, K, V> {}

impl<'a, K, V, const N: usize, F, S> IntoIterator for &'a mut super::EvictingCacheMap<K, V, N, F, S>
where
    F: Fn(K, V),
{
    type Item = (&'a K, &'a mut V);
    type IntoIter = IterMut<'a, K, V>;

    fn into_iter(self) -> Self::IntoIter {
        Self::IntoIter {
            inner: self.inner.iter_mut(),
        }
    }
}