Async Iter
This library offers an asynchronous version of the rust's Iterator
trait and utilities.
Quick Start
Add to Dependencies
Add async-iter to your dependencies:
Entrance to Utilities
Then, import the basic traits:
use
Api Call Stack
All api names and restrictions are the same as the std ones.
You just need to change your .iter()
or .into_iter()
to .aiter()
or .into_aiter()
:
assert_eq!;
Asynchronous Alternative
If you want to use asynchronous functions inside your iterators,
api prefixed with a
will accept a function returning a Future
while works the same as synchronous ones:
async .aiter
// `amap` accepts `FnMut(Item) -> Future<Output>`
.amap
// `afor_each` accepts `FnMut(Item) -> Future<Output = ()>`
.afor_each
.await;
Awaited Asynchronous Iterator
If you have an iterator which yields Future
s, use .awaited()
to
change them into an AsyncIterator
:
// impl Iterator<Output: Future<Output = i32>>
let sync_it = .map;
// impl AsyncIterator<Output = i32>
let async_it = sync_it.aiter.awaited;
Internal Implementation
All internal implementations can be found in the asynciter::iterimpl
module, and are exported publicly, but direct usage is not recommended.
Notes
For those api which accept FnXXX(&Self::Item) -> Future
, there's another
extra restriction. Since the Future
will be used later than the function
call, the reference might outlive the function call's lifetime, leading
to compiler failures. To keep code safe, the reference must be processed
and dropped outside of the Future
.
For example, the following code will not pass the compiler check:
.aiter
.afilter
Instead, we must use the following solution:
.map
.aiter
.afilter
.for_each.await;
License
This project is licensed under the MIT License or Apache V2.0 License.