asynciter 0.1.0

Asynchronous iterator.
Documentation
use std::{fmt::Debug, future::Future};

use crate::AsyncIterator;

#[must_use = "iterators are lazy and do nothing unless consumed"]
#[derive(Clone)]
pub struct Scan<I, St, F> {
    iter: I,
    f: F,
    state: St,
}

impl<I, St, F> Scan<I, St, F> {
    pub fn new(iter: I, state: St, f: F) -> Self {
        Self { iter, state, f }
    }
}

impl<I: Debug, St: Debug, F> Debug for Scan<I, St, F> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Scan")
            .field("iter", &self.iter)
            .field("state", &self.state)
            .finish()
    }
}

impl<B, I, St, F> AsyncIterator for Scan<I, St, F>
where
    I: AsyncIterator,
    F: FnMut(&mut St, I::Item) -> Option<B>,
{
    type Item = B;

    #[inline]
    async fn next(&mut self) -> Option<B> {
        let a = self.iter.next().await?;
        (self.f)(&mut self.state, a)
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        let (_, upper) = self.iter.size_hint();
        (0, upper) // can't know a lower bound, due to the scan function
    }
}

#[must_use = "iterators are lazy and do nothing unless consumed"]
#[derive(Clone)]
pub struct AsyncScan<I, St, F> {
    iter: I,
    f: F,
    state: St,
}

impl<I, St, F> AsyncScan<I, St, F> {
    pub fn new(iter: I, state: St, f: F) -> Self {
        Self { iter, state, f }
    }
}

impl<I: Debug, St: Debug, F> Debug for AsyncScan<I, St, F> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Scan")
            .field("iter", &self.iter)
            .field("state", &self.state)
            .finish()
    }
}

impl<B, I, St, F, O> AsyncIterator for AsyncScan<I, St, F>
where
    I: AsyncIterator,
    F: FnMut(&mut St, I::Item) -> O,
    O: Future<Output = Option<B>>,
{
    type Item = B;

    #[inline]
    async fn next(&mut self) -> Option<B> {
        let a = self.iter.next().await?;
        (self.f)(&mut self.state, a).await
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        let (_, upper) = self.iter.size_hint();
        (0, upper) // can't know a lower bound, due to the scan function
    }
}