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§
Provided Methods§
Sourcefn size_hint(&self) -> (usize, Option<usize>)
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
.
fn awaited(self) -> Awaited<Self>
fn map<F, B>(self, f: F) -> Map<Self, F>
fn amap<F, B, O>(self, f: F) -> AsyncMap<Self, F>
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>
Sourcefn afilter<P, F>(self, predicate: P) -> AsyncFilter<Self, P>
fn afilter<P, F>(self, predicate: P) -> AsyncFilter<Self, P>
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
);
fn filter_map<F, B>(self, operation: F) -> FilterMap<Self, F>
fn afilter_map<F, B, O>(self, operation: F) -> AsyncFilterMap<Self, F>
fn copied<'a, T>(self) -> Copied<Self>
fn cloned<'a, T>(self) -> Cloned<Self>
fn inspect<P>(self, op: P) -> Inspect<Self, P>
fn fuse(self) -> Fuse<Self>where
Self: Sized,
fn flatten(self) -> Flatten<Self, Self::Item>
fn flat_map<U, F>(self, f: F) -> FlatMap<Self, U, F>
fn aflat_map<U, F, B>(self, f: F) -> AsyncFlatMap<Self, U, F, B>
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>
fn intersperse_with<G>(self, separator: G) -> IntersperseWith<Self, G>
fn aintersperse_with<G, F>(self, separator: G) -> AsyncIntersperseWith<Self, G>
fn cycle(self) -> Cycle<Self>
fn take(self, n: usize) -> Take<Self>where
Self: Sized,
fn take_while<P>(self, predicate: P) -> TakeWhile<Self, P>
Sourcefn atake_while<P, F>(self, predicate: P) -> AsyncTakeWhile<Self, P>
fn atake_while<P, F>(self, predicate: P) -> AsyncTakeWhile<Self, P>
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
);
fn scan<St, B, F>(self, initial_state: St, f: F) -> Scan<Self, St, F>
Sourcefn ascan<St, B, F, O>(self, initial_state: St, f: F) -> AsyncScan<Self, St, F>
fn ascan<St, B, F, O>(self, initial_state: St, f: F) -> AsyncScan<Self, St, F>
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 impl
s
in other part of the code system.
fn find<P>(&mut self, predicate: P) -> impl Future<Output = Option<Self::Item>>
fn afind<P, F>( &mut self, predicate: P, ) -> impl Future<Output = Option<Self::Item>>
fn find_map<P, B>(&mut self, operation: P) -> impl Future<Output = Option<B>>
fn afind_map<P, F, B>( &mut self, operation: P, ) -> impl Future<Output = Option<B>>
fn collect<B>(self) -> impl Future<Output = B>
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>
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 = ()>
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>>
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>
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>
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.