asynciter 0.1.0

Asynchronous iterator.
Documentation
use crate::AsyncIterator;

#[must_use = "iterators are lazy and do nothing unless consumed"]
#[derive(Debug, Clone)]
pub struct Zip<A, B> {
    a: A,
    b: B,
}

impl<A: AsyncIterator, B: AsyncIterator> Zip<A, B> {
    pub fn new(a: A, b: B) -> Zip<A, B> {
        Zip { a, b }
    }
}

impl<A, B> AsyncIterator for Zip<A, B>
where
    A: AsyncIterator,
    B: AsyncIterator,
{
    type Item = (A::Item, B::Item);

    async fn next(&mut self) -> Option<Self::Item> {
        let a = self.a.next().await?;
        let b = self.b.next().await?;
        Some((a, b))
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let (a_lower, a_upper) = self.a.size_hint();
        let (b_lower, b_upper) = self.b.size_hint();

        let lower = std::cmp::min(a_lower, b_lower);

        let upper = match (a_upper, b_upper) {
            (Some(x), Some(y)) => Some(std::cmp::min(x, y)),
            (Some(x), None) => Some(x),
            (None, Some(y)) => Some(y),
            (None, None) => None,
        };

        (lower, upper)
    }
}