Crate pasts[][src]

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 async_std::task::sleep;
use core::future::Future;
use core::task::Poll;
use core::time::Duration;
use pasts::{Loop, Past};

// Exit type for State.
type Exit = ();

// Shared state between tasks on the thread.
struct State<A: Future<Output = ()>, B: Future<Output = ()>> {
    counter: usize,
    one: Past<(), (), A>,
    two: Past<(), (), B>,
}

impl<A: Future<Output = ()>, B: Future<Output = ()>> State<A, B> {
    fn one(&mut self, _: ()) -> Poll<Exit> {
        println!("One {}", self.counter);
        self.counter += 1;
        if self.counter > 6 {
            Poll::Ready(())
        } else {
            Poll::Pending
        }
    }

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

async fn run() {
    let mut state = State {
        counter: 0,
        one: Past::new((), |()| sleep(Duration::from_secs_f64(1.0))),
        two: Past::new((), |()| sleep(Duration::from_secs_f64(2.0))),
    };

    Loop::new(&mut state)
        .when(|s| &mut s.one, State::one)
        .when(|s| &mut s.two, State::two)
        .await;
}

fn main() {
    pasts::block_on(run())
}

Structs

Loop

An asynchronous event loop.

Past

A repeating async fn.

Traits

Executor

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

Functions

block_on

Run a future to completion on the current thread.

Type Definitions

Task

A boxed, pinned future.