use std::future::Future;
use super::adapters::{
AsyncEnumerate, AsyncFilter, AsyncMap, AsyncParallelAdapter, AsyncSkip, AsyncTake, AsyncZip,
};
use super::consumers::{AsyncCollect, AsyncFold, AsyncForEach, AsyncReduce};
use super::parallel::{self, ParAsyncFilter, ParAsyncMap};
pub trait AsyncIterator: Send {
type Item: Send;
fn into_vec(self) -> Vec<Self::Item>
where
Self: Sized;
fn map<F, Fut, R>(self, map_fn: F) -> AsyncMap<Self, F>
where
Self: Sized,
F: Fn(Self::Item) -> Fut + Send + Sync,
Fut: Future<Output = R> + Send,
R: Send,
{
AsyncMap::new(self, map_fn)
}
fn filter<F, Fut>(self, filter_fn: F) -> AsyncFilter<Self, F>
where
Self: Sized,
F: Fn(&Self::Item) -> Fut + Send + Sync,
Fut: Future<Output = bool> + Send,
{
AsyncFilter::new(self, filter_fn)
}
fn take(self, count: usize) -> AsyncTake<Self>
where
Self: Sized,
{
AsyncTake::new(self, count)
}
fn skip(self, count: usize) -> AsyncSkip<Self>
where
Self: Sized,
{
AsyncSkip::new(self, count)
}
fn enumerate(self) -> AsyncEnumerate<Self>
where
Self: Sized,
{
AsyncEnumerate::new(self)
}
fn zip<J>(self, other: J) -> AsyncZip<Self, J>
where
Self: Sized,
J: AsyncIterator,
{
AsyncZip::new(self, other)
}
fn for_each<F, Fut>(self, func: F) -> AsyncForEach<Self, F>
where
Self: Sized,
F: Fn(Self::Item) -> Fut + Send + Sync,
Fut: Future<Output = ()> + Send,
{
AsyncForEach::new(self, func)
}
fn collect<C>(self) -> AsyncCollect<Self, C>
where
Self: Sized,
C: Default + Extend<Self::Item> + Send,
{
AsyncCollect::new(self)
}
fn fold<T, F, Fut>(self, init: T, fold_fn: F) -> AsyncFold<Self, T, F>
where
Self: Sized,
F: Fn(T, Self::Item) -> Fut + Send + Sync,
Fut: Future<Output = T> + Send,
T: Send,
{
AsyncFold::new(self, init, fold_fn)
}
fn reduce<F, Fut>(self, reduce_fn: F) -> AsyncReduce<Self, F>
where
Self: Sized,
F: Fn(Self::Item, Self::Item) -> Fut + Send + Sync,
Fut: Future<Output = Self::Item> + Send,
{
AsyncReduce::new(self, reduce_fn)
}
fn into_parallel(self) -> AsyncParallelAdapter<Self>
where
Self: Sized,
{
AsyncParallelAdapter::new(self)
}
}
pub trait AsyncParallelIterator: AsyncIterator {
fn par_map<F, Fut, R>(self, concurrency: usize, map_fn: F) -> ParAsyncMap<Self, F>
where
Self: Sized,
F: Fn(Self::Item) -> Fut + Send + Sync,
Fut: Future<Output = R> + Send,
R: Send,
{
ParAsyncMap::new(self, concurrency, map_fn)
}
fn par_filter<F, Fut>(self, concurrency: usize, filter_fn: F) -> ParAsyncFilter<Self, F>
where
Self: Sized,
F: Fn(&Self::Item) -> Fut + Send + Sync,
Fut: Future<Output = bool> + Send,
{
ParAsyncFilter::new(self, concurrency, filter_fn)
}
fn par_for_each<F, Fut>(self, concurrency: usize, func: F) -> impl Future<Output = ()> + Send
where
Self: Sized,
F: Fn(Self::Item) -> Fut + Send + Sync,
Fut: Future<Output = ()> + Send,
{
parallel::for_each(self, concurrency, func)
}
}
pub trait IntoAsyncIterator {
type Item: Send;
type IntoAsyncIter: AsyncIterator<Item = Self::Item>;
fn into_async_iter(self) -> Self::IntoAsyncIter;
}