asynciter 0.1.0

Asynchronous iterator.
Documentation
use std::fmt::Debug;

use crate::AsyncIterator;

#[must_use = "iterators are lazy and do nothing unless consumed"]
#[derive(Clone)]
pub struct Inspect<I, F> {
    inner: I,
    op: F,
}

impl<I: AsyncIterator + Debug, F> Debug for Inspect<I, F> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Inspect").field("inner", &self.inner).finish()
    }
}

impl<I, F> Inspect<I, F> {
    #[inline]
    pub fn new(inner: I, op: F) -> Self {
        Self { inner, op }
    }
}

impl<I, F> Inspect<I, F>
where
    I: AsyncIterator,
    F: FnMut(&I::Item),
{
    #[inline]
    fn do_inspect(&mut self, val: Option<I::Item>) -> Option<I::Item> {
        if let Some(ref a) = val {
            (self.op)(a);
        }

        val
    }
}

impl<I, F> AsyncIterator for Inspect<I, F>
where
    I: AsyncIterator,
    F: FnMut(&I::Item),
{
    type Item = I::Item;

    async fn next(&mut self) -> Option<I::Item> {
        let next = self.inner.next().await;

        self.do_inspect(next)
    }

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