logo

Crate pasts

source · []
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

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.

Add the following to your Cargo.toml:

[dependencies]
pasts = "0.8"
aysnc-std = "1.0"
use core::time::Duration;
 
use async_std::task::sleep;
use pasts::{prelude::*, Loop};
 
// 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 mut state = State { counter: 0 };
 
    let one = || sleep(Duration::from_secs_f64(1.0));
    let two = || sleep(Duration::from_secs_f64(2.0));
 
    Loop::new(&mut state)
        .on(one, State::one)
        .on(two, State::two)
        .await;
}
 
fn main() {
    pasts::block_on(run())
}

Modules

Types that are almost always needed

Structs

Composable asynchronous event loop.

Type-erased repeating async function

Traits

Trait that implements block_on and block_on_pinned methods for an Executor

Trait for implementing custom executors. Useful when targetting no-std.

Functions

Run a future to completion on the current thread.