Type Definition pasts::Task[][src]

type Task<T> = Pin<Box<dyn Future<Output = T>>>;
Expand description

A boxed, pinned future.

You should use this in conjunction with Loop::poll if you need a dynamic number of tasks (for instance, a web server).

Example

This example spawns two tasks on the same thread, and then terminates when they both complete.

use core::task::Poll;
use pasts::{Loop, Task};

type Exit = ();

struct State {
    tasks: Vec<Task<&'static str>>,
}

impl State {
    fn completion(&mut self, id: usize, val: &str) -> Poll<Exit> {
        self.tasks.remove(id);
        println!("Received message from completed task: {}", val);
        if self.tasks.is_empty() {
            Poll::Ready(())
        } else {
            Poll::Pending
        }
    }
}

async fn run() {
    let mut state = State {
        tasks: vec![Box::pin(async { "Hello" }), Box::pin(async { "World" })],
    };

    Loop::new(&mut state)
        .poll(|s| &mut s.tasks, State::completion)
        .await;
}

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