Trait pasts::Pool

source ·
pub trait Pool {
    type Park: Park;

    // Required methods
    fn push(&self, task: LocalBoxNotify<'static>);
    fn drain(&self, tasks: &mut Vec<LocalBoxNotify<'static>>) -> bool;
}
Expand description

Storage for a task pool.

Implementing Pool For A Custom Executor

This example shows how to create a custom single-threaded executor using Executor::new().

use std::{
    cell::Cell,
    thread::{self, Thread},
};

use pasts::{prelude::*, Executor, Park, Pool};

#[derive(Default)]
struct SingleThreadedPool {
    spawning_queue: Cell<Vec<LocalBoxNotify<'static>>>,
}

impl Pool for SingleThreadedPool {
    type Park = ThreadPark;

    fn push(&self, task: LocalBoxNotify<'static>) {
        let mut queue = self.spawning_queue.take();

        queue.push(task);
        self.spawning_queue.set(queue);
    }

    fn drain(&self, tasks: &mut Vec<LocalBoxNotify<'static>>) -> bool {
        let mut queue = self.spawning_queue.take();
        let mut drained = queue.drain(..).peekable();
        let has_drained = drained.peek().is_some();

        tasks.extend(drained);
        self.spawning_queue.set(queue);

        has_drained
    }
}

struct ThreadPark(Thread);

impl Default for ThreadPark {
    fn default() -> Self {
        Self(thread::current())
    }
}

impl Park for ThreadPark {
    fn park(&self) {
        std::thread::park();
    }

    fn unpark(&self) {
        self.0.unpark();
    }
}

fn main() {
    // Create a custom executor.
    let executor = Executor::new(SingleThreadedPool::default());

    // Block on a future
    executor.block_on(async {
        println!("Hi from inside a future!");
    });
}

Required Associated Types§

source

type Park: Park

Type that handles the sleeping / waking of the executor.

Required Methods§

source

fn push(&self, task: LocalBoxNotify<'static>)

Push a task into the thread pool queue.

source

fn drain(&self, tasks: &mut Vec<LocalBoxNotify<'static>>) -> bool

Drain tasks from the thread pool queue. Should returns true if drained at least one task.

Implementors§