Expand description
Minimal and simpler alternative to the futures crate.
Optional Features
All features are disabled by default
- Enable
no-std
to use pasts without the standard library. - Enable
web
to use pasts within the javascript DOM.
Getting Started
Add the following to your ./Cargo.toml
:
[dependencies]
pasts = "0.11"
# This example uses async-std for a sleep future, but async-std is *not*
# required to use pasts.
async-std = "1.11"
# Use web feature when compiling to wasm32-unknown-unknown
[target.'cfg(all(target_arch="wasm32",target_os="unknown"))'.dependencies]
pasts = { version = "0.11", features = ["web"] }
wasm-bindgen = "0.2"
Create ./build.rs
:
fn main() {
let async_main = format!("{}/main.rs", std::env::var("OUT_DIR").unwrap());
std::fs::write(
async_main,
r#"
#[cfg_attr(feature = "pasts/web", wasm_bindgen(start))]
pub fn main() {
let executor = Executor::default();
executor.spawn(App::main(executor.clone()));
}"#,
)
.unwrap();
}
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.
include!(concat!(env!("OUT_DIR"), "/main.rs"));
use core::time::Duration;
use async_std::task::sleep;
use pasts::{prelude::*, Join, Loop};
// Exit type for App.
struct Exit;
// Shared state between tasks on the thread.
struct App<'a> {
counter: usize,
one: &'a mut (dyn Notifier<Event = ()> + Unpin),
two: &'a mut (dyn Notifier<Event = ()> + Unpin),
}
impl App<'_> {
fn one(&mut self, _: ()) -> Poll<Exit> {
println!("One {}", self.counter);
self.counter += 1;
if self.counter > 6 {
Ready(Exit)
} else {
Pending
}
}
fn two(&mut self, _: ()) -> Poll<Exit> {
println!("Two {}", self.counter);
self.counter += 1;
Pending
}
async fn main(_executor: Executor) {
let sleep = |seconds| sleep(Duration::from_secs_f64(seconds));
let one = &mut Loop::pin(|| sleep(1.0));
let two = &mut Loop::pin(|| sleep(2.0));
let counter = 0;
let mut app = App { counter, one, two };
Join::new(&mut app)
.on(|s| s.one, App::one)
.on(|s| s.two, App::two)
.await;
}
}
Modules
Items that are almost always needed.
Structs
An executor.
Composable asynchronous event loop.
Traits
Trait for asynchronous event notification.