minerva 0.2.0

Causal ordering for distributed systems
extern crate alloc;

use super::{Dot, DotFun};

/// A borrowing iterator over a [`DotFun`]'s `(dot, &value)` entries in
/// ascending dot order (the read side of the payload store).
pub struct DotFunIter<'a, V> {
    /// The underlying map walk, already ascending by dot.
    inner: alloc::collections::btree_map::Iter<'a, Dot, V>,
}

impl<'a, V> DotFunIter<'a, V> {
    pub(super) const fn new(inner: alloc::collections::btree_map::Iter<'a, Dot, V>) -> Self {
        Self { inner }
    }
}

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

impl<V> core::fmt::Debug for DotFunIter<'_, V> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.write_str("DotFunIter")
    }
}

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

    fn next(&mut self) -> Option<Self::Item> {
        self.inner.next().map(|(&dot, value)| (dot, value))
    }

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

impl<V> ExactSizeIterator for DotFunIter<'_, V> {
    fn len(&self) -> usize {
        self.inner.len()
    }
}

impl<V> core::iter::FusedIterator for DotFunIter<'_, V> {}

impl<'a, V> IntoIterator for &'a DotFun<V> {
    type Item = (Dot, &'a V);
    type IntoIter = DotFunIter<'a, V>;

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