Expand description
Minimal and simpler alternative to the futures crate.
Optional Features
The std feature is enabled by default, disable it to use on no_std
.
Getting Started
Add the following to your Cargo.toml:
[dependencies]
pasts = "0.10"
aysnc-std = "1.0"
Multi-Tasking On Multiple Iterators of Futures
This example runs two timers in parallel using the async-std
crate
counting from 0 to 6. The “one” task will always be run for count 6 and
stop the program, although which task will run for count 5 may be either
“one” or “two” because they trigger at the same time.
use core::time::Duration;
use async_std::task::sleep;
use pasts::{prelude::*, Loop, Task};
// Exit type for State.
type Exit = ();
// Shared state between tasks on the thread.
struct State {
counter: usize,
}
impl State {
fn one(&mut self, _: ()) -> Poll<Exit> {
println!("One {}", self.counter);
self.counter += 1;
if self.counter > 6 {
Ready(())
} else {
Pending
}
}
fn two(&mut self, _: ()) -> Poll<Exit> {
println!("Two {}", self.counter);
self.counter += 1;
Pending
}
}
async fn run() {
let sleep = |seconds| sleep(Duration::from_secs_f64(seconds));
let mut state = State { counter: 0 };
let one = Task::with_fn_boxed(|| sleep(1.0));
let two = Task::with_fn_boxed(|| sleep(2.0));
Loop::new(&mut state)
.on(one, State::one)
.on(two, State::two)
.await;
}
fn main() {
pasts::block_on(run())
}
Modules
Items that are almost always needed.
Structs
Traits
Trait for implementing custom executors. Useful when targetting no-std.
Functions
Run a future to completion on the current thread.
Like poll_fn
but for asynchronous iteration.