asynciter 0.1.0

Asynchronous iterator.
Documentation
use std::future::Future;

use crate::AsyncIterator;

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

impl<I, P> Filter<I, P> {
    #[inline]
    pub fn new(inner: I, predicate: P) -> Self {
        Self { inner, predicate }
    }
}

impl<I, P> AsyncIterator for Filter<I, P>
where
    I: AsyncIterator,
    P: FnMut(&I::Item) -> bool,
{
    type Item = I::Item;

    fn next(&mut self) -> impl Future<Output = Option<I::Item>> {
        self.inner.find(&mut self.predicate)
    }

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

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

impl<I, P> AsyncFilter<I, P> {
    #[inline]
    pub fn new(inner: I, predicate: P) -> Self {
        Self { inner, predicate }
    }
}

impl<I, P, F> AsyncIterator for AsyncFilter<I, P>
where
    I: AsyncIterator,
    P: FnMut(&I::Item) -> F,
    F: Future<Output = bool>,
{
    type Item = I::Item;

    fn next(&mut self) -> impl Future<Output = Option<I::Item>> {
        self.inner.afind(&mut self.predicate)
    }

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