Crate pasts

source ·
Expand description

Minimal and simpler alternative to the futures crate.

Optional Features

Only the std feature is enabled by default

  • Disable 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.14"

## This example uses async_main for convenience, but it is *not* required to
## use pasts.
async_main = { version = "0.4", features = ["pasts"] }

## This example uses async-std for a sleep future, but async-std is *not*
## required to use pasts.
async-std = "1.12"

## Also not required for pasts, but allows for portability with WebAssembly
## in the browser.
[features]
web = ["async_main/web", "pasts/web"]

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_main::{async_main, LocalSpawner};
use async_std::task::sleep;
use pasts::{notify, prelude::*, Loop};

/// Shared state between tasks on the thread.
struct App<'a> {
    counter: usize,
    one: &'a mut (dyn Notify<Event = ()> + Unpin),
    two: &'a mut (dyn Notify<Event = ()> + Unpin),
}

impl App<'_> {
    fn one(&mut self, (): ()) -> Poll {
        println!("One {}", self.counter);
        self.counter += 1;

        if self.counter > 6 {
            Ready(())
        } else {
            Pending
        }
    }

    fn two(&mut self, (): ()) -> Poll {
        println!("Two {}", self.counter);
        self.counter += 1;

        Pending
    }

    async fn run(&mut self) {
        Loop::new(self)
            .on(|s| &mut s.one, Self::one)
            .on(|s| &mut s.two, Self::two)
            .await;
    }
}

#[async_main]
async fn main(_spawner: LocalSpawner) {
    let sleep = |seconds| sleep(Duration::from_secs_f64(seconds));
    let mut app = App {
        counter: 0,
        one: &mut notify::future_fn(|| Box::pin(sleep(1.0))),
        two: &mut notify::future_fn(|| Box::pin(sleep(2.0))),
    };

    app.run().await;
}

Modules

  • Asynchronous event notifys
  • Items that are almost always needed.

Structs

  • Pasts’ executor.
  • Composable asynchronous event loop.

Traits

  • Trait for implementing the parking / unparking threads.
  • Storage for a task pool.