Trait AsyncIterator

Source
pub trait AsyncIterator {
    type Item;

Show 44 methods // Required method fn next(&mut self) -> impl Future<Output = Option<Self::Item>>; // Provided methods fn size_hint(&self) -> (usize, Option<usize>) { ... } fn awaited(self) -> Awaited<Self> where Self: Sized, Self::Item: Future { ... } fn map<F, B>(self, f: F) -> Map<Self, F> where F: FnMut(Self::Item) -> B, Self: Sized { ... } fn amap<F, B, O>(self, f: F) -> AsyncMap<Self, F> where F: FnMut(Self::Item) -> O, O: Future<Output = B>, Self: Sized { ... } fn enumerate(self) -> Enumerate<Self> where Self: Sized { ... } fn peekable(self) -> Peekable<Self> where Self: Sized { ... } fn filter<P>(self, predicate: P) -> Filter<Self, P> where Self: Sized, P: FnMut(&Self::Item) -> bool { ... } fn afilter<P, F>(self, predicate: P) -> AsyncFilter<Self, P> where Self: Sized, P: FnMut(&Self::Item) -> F, F: Future<Output = bool> { ... } fn filter_map<F, B>(self, operation: F) -> FilterMap<Self, F> where Self: Sized, F: FnMut(Self::Item) -> Option<B> { ... } fn afilter_map<F, B, O>(self, operation: F) -> AsyncFilterMap<Self, F> where Self: Sized, F: FnMut(Self::Item) -> O, O: Future<Output = Option<B>> { ... } fn copied<'a, T>(self) -> Copied<Self> where T: 'a + Copy, Self: Sized + AsyncIterator<Item = &'a T> { ... } fn cloned<'a, T>(self) -> Cloned<Self> where T: 'a + Clone, Self: Sized + AsyncIterator<Item = &'a T> { ... } fn inspect<P>(self, op: P) -> Inspect<Self, P> where Self: Sized, P: FnMut(&Self::Item) { ... } fn fuse(self) -> Fuse<Self> where Self: Sized { ... } fn flatten(self) -> Flatten<Self, Self::Item> where Self: Sized, Self::Item: IntoAsyncIterator { ... } fn flat_map<U, F>(self, f: F) -> FlatMap<Self, U, F> where Self: Sized, U: IntoAsyncIterator, F: FnMut(Self::Item) -> U { ... } fn aflat_map<U, F, B>(self, f: F) -> AsyncFlatMap<Self, U, F, B> where Self: Sized, U: IntoAsyncIterator, F: FnMut(Self::Item) -> B, B: Future<Output = U> { ... } fn zip<U>(self, other: U) -> Zip<Self, <U as IntoAsyncIterator>::AsyncIter> where Self: Sized, U: IntoAsyncIterator { ... } fn intersperse(self, separator: Self::Item) -> Intersperse<Self> where Self: Sized, Self::Item: Clone { ... } fn intersperse_with<G>(self, separator: G) -> IntersperseWith<Self, G> where Self: Sized, G: FnMut() -> Self::Item { ... } fn aintersperse_with<G, F>( self, separator: G, ) -> AsyncIntersperseWith<Self, G> where Self: Sized, G: FnMut() -> F, F: Future<Output = Self::Item> { ... } fn cycle(self) -> Cycle<Self> where Self: Sized + Clone { ... } fn take(self, n: usize) -> Take<Self> where Self: Sized { ... } fn take_while<P>(self, predicate: P) -> TakeWhile<Self, P> where Self: Sized, P: FnMut(&Self::Item) -> bool { ... } fn atake_while<P, F>(self, predicate: P) -> AsyncTakeWhile<Self, P> where Self: Sized, P: FnMut(&Self::Item) -> F, F: Future<Output = bool> { ... } fn scan<St, B, F>(self, initial_state: St, f: F) -> Scan<Self, St, F> where Self: Sized, F: FnMut(&mut St, Self::Item) -> Option<B> { ... } fn ascan<St, B, F, O>( self, initial_state: St, f: F, ) -> AsyncScan<Self, St, F> where Self: Sized, F: FnMut(&mut St, Self::Item) -> O, O: Future<Output = Option<B>> { ... } fn find<P>( &mut self, predicate: P, ) -> impl Future<Output = Option<Self::Item>> where Self: Sized, P: FnMut(&Self::Item) -> bool { ... } fn afind<P, F>( &mut self, predicate: P, ) -> impl Future<Output = Option<Self::Item>> where Self: Sized, P: FnMut(&Self::Item) -> F, F: Future<Output = bool> { ... } fn find_map<P, B>( &mut self, operation: P, ) -> impl Future<Output = Option<B>> where Self: Sized, P: FnMut(Self::Item) -> Option<B> { ... } fn afind_map<P, F, B>( &mut self, operation: P, ) -> impl Future<Output = Option<B>> where Self: Sized, P: FnMut(Self::Item) -> F, F: Future<Output = Option<B>> { ... } fn collect<B>(self) -> impl Future<Output = B> where B: FromAsyncIterator<Self::Item>, Self: Sized { ... } fn fold<B>( self, init: B, f: impl FnMut(B, Self::Item) -> B, ) -> impl Future<Output = B> where Self: Sized { ... } fn afold<B, F>( self, init: B, f: impl FnMut(B, Self::Item) -> F, ) -> impl Future<Output = B> where Self: Sized, F: Future<Output = B> { ... } fn for_each(self, f: impl FnMut(Self::Item)) -> impl Future<Output = ()> where Self: Sized { ... } fn afor_each<F>( self, f: impl FnMut(Self::Item) -> F, ) -> impl Future<Output = ()> where Self: Sized, F: Future<Output = ()> { ... } fn reduce( self, f: impl FnMut(Self::Item, Self::Item) -> Self::Item, ) -> impl Future<Output = Option<Self::Item>> where Self: Sized { ... } fn areduce<F>( self, f: impl FnMut(Self::Item, Self::Item) -> F, ) -> impl Future<Output = Option<Self::Item>> where Self: Sized, F: Future<Output = Self::Item> { ... } fn count(self) -> impl Future<Output = usize> where Self: Sized { ... } fn all( self, f: impl FnMut(Self::Item) -> bool, ) -> impl Future<Output = bool> where Self: Sized { ... } fn aall<F>( self, f: impl FnMut(Self::Item) -> F, ) -> impl Future<Output = bool> where Self: Sized, F: Future<Output = bool> { ... } fn any( self, f: impl FnMut(Self::Item) -> bool, ) -> impl Future<Output = bool> where Self: Sized { ... } fn aany<F>( self, f: impl FnMut(Self::Item) -> F, ) -> impl Future<Output = bool> where Self: Sized, F: Future<Output = bool> { ... }
}

Required Associated Types§

Required Methods§

Source

fn next(&mut self) -> impl Future<Output = Option<Self::Item>>

Provided Methods§

Source

fn size_hint(&self) -> (usize, Option<usize>)

Returns the bounds on the remaining length of the iterator.

Specifically, size_hint() returns a tuple where the first element is the lower bound, and the second element is the upper bound.

The second half of the tuple that is returned is an Option<usize>. A None here means that either there is no known upper bound, or the upper bound is larger than usize.

Source

fn awaited(self) -> Awaited<Self>
where Self: Sized, Self::Item: Future,

Source

fn map<F, B>(self, f: F) -> Map<Self, F>
where F: FnMut(Self::Item) -> B, Self: Sized,

Source

fn amap<F, B, O>(self, f: F) -> AsyncMap<Self, F>
where F: FnMut(Self::Item) -> O, O: Future<Output = B>, Self: Sized,

Source

fn enumerate(self) -> Enumerate<Self>
where Self: Sized,

Source

fn peekable(self) -> Peekable<Self>
where Self: Sized,

Source

fn filter<P>(self, predicate: P) -> Filter<Self, P>
where Self: Sized, P: FnMut(&Self::Item) -> bool,

Source

fn afilter<P, F>(self, predicate: P) -> AsyncFilter<Self, P>
where Self: Sized, P: FnMut(&Self::Item) -> F, F: Future<Output = bool>,

Note:

The <F> must outlive the &Self::Item in <P>, since we will use the future later than applying the predicate. That means if you want to do something with the item, you have to own the item before you create the future.

If you want to do something with the item, see [afilter_map][trait.AsyncIterator.html#method.afilter_map].

use asynciter::{AsyncIterator, ToAsyncIterator};
let v: Vec<i32> = vec![1, 2, 3, 4, 5];
assert_eq!(
    vec![2, 4],
    v.clone()
        .into_iter()
        .aiter()
        // Just like the operation below.
        // We must get the ownership of the value to process it.
        .afilter(|s| {
            let s = *s;
            async move { s % 2 == 0 }
        })
        .collect::<Vec<i32>>()
        .await
);
Source

fn filter_map<F, B>(self, operation: F) -> FilterMap<Self, F>
where Self: Sized, F: FnMut(Self::Item) -> Option<B>,

Source

fn afilter_map<F, B, O>(self, operation: F) -> AsyncFilterMap<Self, F>
where Self: Sized, F: FnMut(Self::Item) -> O, O: Future<Output = Option<B>>,

Source

fn copied<'a, T>(self) -> Copied<Self>
where T: 'a + Copy, Self: Sized + AsyncIterator<Item = &'a T>,

Source

fn cloned<'a, T>(self) -> Cloned<Self>
where T: 'a + Clone, Self: Sized + AsyncIterator<Item = &'a T>,

Source

fn inspect<P>(self, op: P) -> Inspect<Self, P>
where Self: Sized, P: FnMut(&Self::Item),

Source

fn fuse(self) -> Fuse<Self>
where Self: Sized,

Source

fn flatten(self) -> Flatten<Self, Self::Item>
where Self: Sized, Self::Item: IntoAsyncIterator,

Source

fn flat_map<U, F>(self, f: F) -> FlatMap<Self, U, F>
where Self: Sized, U: IntoAsyncIterator, F: FnMut(Self::Item) -> U,

Source

fn aflat_map<U, F, B>(self, f: F) -> AsyncFlatMap<Self, U, F, B>
where Self: Sized, U: IntoAsyncIterator, F: FnMut(Self::Item) -> B, B: Future<Output = U>,

Source

fn zip<U>(self, other: U) -> Zip<Self, <U as IntoAsyncIterator>::AsyncIter>
where Self: Sized, U: IntoAsyncIterator,

Source

fn intersperse(self, separator: Self::Item) -> Intersperse<Self>
where Self: Sized, Self::Item: Clone,

Source

fn intersperse_with<G>(self, separator: G) -> IntersperseWith<Self, G>
where Self: Sized, G: FnMut() -> Self::Item,

Source

fn aintersperse_with<G, F>(self, separator: G) -> AsyncIntersperseWith<Self, G>
where Self: Sized, G: FnMut() -> F, F: Future<Output = Self::Item>,

Source

fn cycle(self) -> Cycle<Self>
where Self: Sized + Clone,

Source

fn take(self, n: usize) -> Take<Self>
where Self: Sized,

Source

fn take_while<P>(self, predicate: P) -> TakeWhile<Self, P>
where Self: Sized, P: FnMut(&Self::Item) -> bool,

Source

fn atake_while<P, F>(self, predicate: P) -> AsyncTakeWhile<Self, P>
where Self: Sized, P: FnMut(&Self::Item) -> F, F: Future<Output = bool>,

Note:

The <F> must outlive the &Self::Item in <P>, since we will use the future later than applying the predicate. That means if you want to do something with the item, you have to own the item before you create the future.

use asynciter::{AsyncIterator, ToAsyncIterator};
let v: Vec<i32> = vec![1, 2, 3, 4, 5];
assert_eq!(
    vec![1, 2, 3, 4],
    v.clone()
        .into_iter()
        .aiter()
        // Just like the operation below.
        // We must get the ownership of the value to process it.
        .atake_while(|s| {
            let s = *s;
            async move { s < 5 }
        })
        .collect::<Vec<i32>>()
        .await
);
Source

fn scan<St, B, F>(self, initial_state: St, f: F) -> Scan<Self, St, F>
where Self: Sized, F: FnMut(&mut St, Self::Item) -> Option<B>,

Source

fn ascan<St, B, F, O>(self, initial_state: St, f: F) -> AsyncScan<Self, St, F>
where Self: Sized, F: FnMut(&mut St, Self::Item) -> O, O: Future<Output = Option<B>>,

Note:

The Future must outlive the FnMut, but it may own the &mut state. That means you cannot operate on the mutable reference within the future(or typically async block).

For example, the following code cannot be compiled, because in the future we use the state which is behind a mutable reference:

use asynciter::{AsyncIterator, ToAsyncIterator};
let a = [1, 2, 3, 4];
let iter = a.iter().aiter().ascan(1, |state, &x| async move {
    *state *= x;
    if *state > 6 {
        return None;
    }
    Some(-*state)
});
assert_eq!(vec![-1, -2, -6], iter.collect::<Vec<_>>().await);

Instead, we must use the following alternative and process the state outside of the async block:

use asynciter::{AsyncIterator, ToAsyncIterator};
let a = [1, 2, 3, 4];
let iter = a.iter().aiter().ascan(1, |state, &x| {
    *state *= x;
    // save the state for usage inside future.
    let imut_state = *state;
    async move {
        if imut_state > 6 {
            return None;
        }
        Some(-imut_state)
    }
});
assert_eq!(vec![-1, -2, -6], iter.collect::<Vec<_>>().await);

This restriction makes ascan hard to use and hard to pass the compiler check. But there’s no easy solution except we use other solutions like dynamic dispatch, which is still not convenient to implement since we use many async trait impls in other part of the code system.

Source

fn find<P>(&mut self, predicate: P) -> impl Future<Output = Option<Self::Item>>
where Self: Sized, P: FnMut(&Self::Item) -> bool,

Source

fn afind<P, F>( &mut self, predicate: P, ) -> impl Future<Output = Option<Self::Item>>
where Self: Sized, P: FnMut(&Self::Item) -> F, F: Future<Output = bool>,

Source

fn find_map<P, B>(&mut self, operation: P) -> impl Future<Output = Option<B>>
where Self: Sized, P: FnMut(Self::Item) -> Option<B>,

Source

fn afind_map<P, F, B>( &mut self, operation: P, ) -> impl Future<Output = Option<B>>
where Self: Sized, P: FnMut(Self::Item) -> F, F: Future<Output = Option<B>>,

Source

fn collect<B>(self) -> impl Future<Output = B>
where B: FromAsyncIterator<Self::Item>, Self: Sized,

Source

fn fold<B>( self, init: B, f: impl FnMut(B, Self::Item) -> B, ) -> impl Future<Output = B>
where Self: Sized,

Source

fn afold<B, F>( self, init: B, f: impl FnMut(B, Self::Item) -> F, ) -> impl Future<Output = B>
where Self: Sized, F: Future<Output = B>,

Source

fn for_each(self, f: impl FnMut(Self::Item)) -> impl Future<Output = ()>
where Self: Sized,

Source

fn afor_each<F>( self, f: impl FnMut(Self::Item) -> F, ) -> impl Future<Output = ()>
where Self: Sized, F: Future<Output = ()>,

Source

fn reduce( self, f: impl FnMut(Self::Item, Self::Item) -> Self::Item, ) -> impl Future<Output = Option<Self::Item>>
where Self: Sized,

Source

fn areduce<F>( self, f: impl FnMut(Self::Item, Self::Item) -> F, ) -> impl Future<Output = Option<Self::Item>>
where Self: Sized, F: Future<Output = Self::Item>,

Source

fn count(self) -> impl Future<Output = usize>
where Self: Sized,

Source

fn all(self, f: impl FnMut(Self::Item) -> bool) -> impl Future<Output = bool>
where Self: Sized,

Source

fn aall<F>(self, f: impl FnMut(Self::Item) -> F) -> impl Future<Output = bool>
where Self: Sized, F: Future<Output = bool>,

Source

fn any(self, f: impl FnMut(Self::Item) -> bool) -> impl Future<Output = bool>
where Self: Sized,

Source

fn aany<F>(self, f: impl FnMut(Self::Item) -> F) -> impl Future<Output = bool>
where Self: Sized, F: Future<Output = bool>,

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<'a, I, T> AsyncIterator for Cloned<I>
where I: AsyncIterator<Item = &'a T>, T: Clone + 'a,

Source§

type Item = T

Source§

impl<'a, I, T> AsyncIterator for Copied<I>
where I: AsyncIterator<Item = &'a T>, T: Copy + 'a,

Source§

type Item = T

Source§

impl<A, B> AsyncIterator for Zip<A, B>

Source§

impl<A, T> AsyncIterator for AsyncOnceWith<T>
where T: FnOnce() -> A,

Source§

type Item = A

Source§

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

Source§

type Item = B

Source§

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>>,

Source§

type Item = B

Source§

impl<I> AsyncIterator for Enumerate<I>
where I: AsyncIterator,

Source§

impl<I> AsyncIterator for Fuse<I>
where I: AsyncIterator,

Source§

impl<I> AsyncIterator for Intersperse<I>
where I: AsyncIterator, I::Item: Clone,

Source§

impl<I> AsyncIterator for Peekable<I>
where I: AsyncIterator,

Source§

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

Source§

impl<I, F, B> AsyncIterator for Awaited<I>
where I: AsyncIterator<Item = F>, F: Future<Output = B>,

Source§

type Item = B

Source§

impl<I, F, B> AsyncIterator for FilterMap<I, F>
where I: AsyncIterator, F: FnMut(I::Item) -> Option<B>,

Source§

type Item = B

Source§

impl<I, F, B, O> AsyncIterator for AsyncFilterMap<I, F>
where I: AsyncIterator, F: FnMut(I::Item) -> O, O: Future<Output = Option<B>>,

Source§

type Item = B

Source§

impl<I, G> AsyncIterator for IntersperseWith<I, G>
where I: AsyncIterator, G: FnMut() -> I::Item,

Source§

impl<I, G, F> AsyncIterator for AsyncIntersperseWith<I, G>
where I: AsyncIterator, G: FnMut() -> F, F: Future<Output = I::Item>,

Source§

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

Source§

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

Source§

impl<I, U, F> AsyncIterator for FlatMap<I, U, F>
where I: AsyncIterator, U: IntoAsyncIterator, F: FnMut(I::Item) -> U,

Source§

impl<I, U, F, B> AsyncIterator for AsyncFlatMap<I, U, F, B>
where I: AsyncIterator, U: IntoAsyncIterator, F: FnMut(I::Item) -> B, B: Future<Output = U>,

Source§

impl<I: AsyncIterator + Clone> AsyncIterator for Cycle<I>

Source§

impl<I: AsyncIterator> AsyncIterator for Take<I>

Source§

impl<I: AsyncIterator<Item = A>, A: IntoAsyncIterator> AsyncIterator for Flatten<I, A>

Source§

impl<I: AsyncIterator, F, B> AsyncIterator for Map<I, F>
where F: FnMut(I::Item) -> B,

Source§

type Item = B

Source§

impl<I: AsyncIterator, F, B, O> AsyncIterator for AsyncMap<I, F>
where F: FnMut(I::Item) -> O, O: Future<Output = B>,

Source§

type Item = B

Source§

impl<I: AsyncIterator, P> AsyncIterator for TakeWhile<I, P>
where P: FnMut(&I::Item) -> bool,

Source§

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

Source§

impl<T> AsyncIterator for AsyncOnce<T>

Source§

type Item = T

Source§

impl<T, F: Future<Output = T>, I: Iterator<Item = F>> AsyncIterator for AsyncIter<T, I, F>

Source§

type Item = T

Source§

impl<T, I: Iterator<Item = T>> AsyncIterator for SyncIter<I>

Source§

type Item = T