Struct BusyLocalPool

Source
pub struct BusyLocalPool<'a, Ret = ()> { /* private fields */ }
Expand description

A single-threaded task pool for polling futures to completion.

This executor allows you to multiplex any number of tasks onto a single thread. It’s appropriate to poll strictly I/O-bound futures that do very little work in between I/O actions.

To get a handle to the pool that implements Spawn, use the spawner() method. Because the executor is single-threaded, it supports a special form of task spawning for non-Send futures, via spawn_local_obj.

Implementations§

Source§

impl<'a, Ret> LocalPool<'a, Ret>

Source

pub fn new(cap: usize) -> Self

Create a new, empty pool of tasks.

Source

pub fn spawner(&self) -> Spawner<'a, Ret>

Source

pub fn spawn<F>(&mut self, f: F)
where F: UnsafeFutureObj<'a, Ret>,

Source

pub fn run(&mut self) -> Vec<Ret>

Run all tasks in the pool to completion.


use minimal_executor::LocalPool;

let mut pool: LocalPool<'_, ()> = LocalPool::new();

// ... spawn some initial tasks using `spawn.spawn()` or `spawn.spawn_local()`

// run *all* tasks in the pool to completion, including any newly-spawned ones.
pool.run();

The function will block the calling thread until all tasks in the pool are complete, including any spawned while running existing tasks.

Source

pub fn try_run_one(&mut self) -> Poll<Ret>

Runs all tasks and returns after completing one future or until no more progress can be made. Returns true if one future was completed, false otherwise.


use futures::task::LocalSpawnExt;
use futures::future::{ready, pending};
use minimal_executor::LocalPool;

let mut pool: LocalPool<'_, ()> = LocalPool::new();
pool.spawn(Box::pin(ready(())));
pool.spawn(Box::pin(ready(())));
pool.spawn(Box::pin(pending()));

// Run the two ready tasks and return true for them.
pool.try_run_one(); // returns true after completing one of the ready futures
pool.try_run_one(); // returns true after completing the other ready future

// the remaining task can not be completed
assert!(pool.try_run_one().is_pending()); // returns false

This function will not block the calling thread and will return the moment that there are no tasks left for which progress can be made or after exactly one task was completed; Remaining incomplete tasks in the pool can continue with further use of one of the pool’s run or poll methods. Though only one task will be completed, progress may be made on multiple tasks.

Source

pub fn poll_though(&mut self) -> Poll<Option<Ret>>

Source

pub fn poll_once(&mut self) -> Poll<Option<Ret>>

Trait Implementations§

Source§

impl<'a, Ret: Debug> Debug for LocalPool<'a, Ret>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a, Ret> Freeze for LocalPool<'a, Ret>

§

impl<'a, Ret> RefUnwindSafe for LocalPool<'a, Ret>

§

impl<'a, Ret = ()> !Send for LocalPool<'a, Ret>

§

impl<'a, Ret = ()> !Sync for LocalPool<'a, Ret>

§

impl<'a, Ret> Unpin for LocalPool<'a, Ret>

§

impl<'a, Ret> UnwindSafe for LocalPool<'a, Ret>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.