odsek 0.1.0

Lazy, pull-based composition of mathematical interval sets with open/closed endpoints, no_std-compatible.
Documentation
use crate::interval::*;
use crate::Intervals;

/// Adapter that turns any [`Intervals`] stream into a standard
/// [`Iterator`](core::iter::Iterator).
///
/// Calling [`into_iter`](Self::into_iter) yields an
/// [`IntervalsIntoIter`] which threads the previous interval's right endpoint
/// (converted to a left via [`RightT::into_left`]) as the next `pos` argument
/// to [`Intervals::head`].
pub trait IntoIntervals: Sized
where
    Self: Intervals,
{
    /// Consume the stream and return a forward iterator over its intervals.
    fn into_iter(self) -> IntervalsIntoIter<Self, Self::Endpoint>;
}

impl<I> IntoIntervals for I
where
    I: Intervals + Sized,
    I::Endpoint: Clone + EndpointToggle,
{
    fn into_iter(self) -> IntervalsIntoIter<Self, I::Endpoint> {
        IntervalsIntoIter {
            it: self,
            pos: None,
        }
    }
}

/// Forward [`Iterator`] over an [`Intervals`] stream, produced by
/// [`IntoIntervals::into_iter`].
pub struct IntervalsIntoIter<Itr, E> {
    it: Itr,
    pos: Option<LeftT<E>>,
}

impl<V, E, It> Iterator for IntervalsIntoIter<It, E>
where
    It: Intervals<Endpoint = E, Value = V>,
    E: Clone + EndpointToggle,
{
    type Item = Interval<E, V>;
    fn next(&mut self) -> Option<Self::Item> {
        let r = self.it.head(self.pos.clone())?;
        self.pos = Some(r.right().into_left().clone());
        Some(r)
    }
}