Struct Executor

Source
pub struct Executor { /* private fields */ }
Expand description

A multi-threaded executor.

Implementations§

Source§

impl Executor

Source

pub fn new() -> Executor

Creates a new multi-threaded executor.

§Examples
use multitask::Executor;

let ex = Executor::new();
Source

pub fn spawn<T: Send + 'static>( &self, future: impl Future<Output = T> + Send + 'static, ) -> Task<T>

Spawns a future onto this executor.

Returns a Task handle for the spawned future.

§Examples
use multitask::Executor;

let ex = Executor::new();
let task = ex.spawn(async { println!("hello") });
Source

pub fn ticker(&self, notify: impl Fn() + Send + Sync + 'static) -> Ticker

Creates a new ticker for executing tasks.

In a multi-threaded executor, each executor thread will create its own ticker and then keep calling Ticker::tick() in a loop.

§Examples
use blocking::block_on;
use multitask::Executor;
use std::thread;

let ex = Executor::new();

// Create two executor threads.
for _ in 0..2 {
    let (p, u) = parking::pair();
    let ticker = ex.ticker(move || u.unpark());
    thread::spawn(move || {
        loop {
            if !ticker.tick() {
                p.park();
            }
        }
    });
}

// Spawn a future and wait for one of the threads to run it.
let task = ex.spawn(async { 1 + 2 });
assert_eq!(block_on(task), 3);

Trait Implementations§

Source§

impl Debug for Executor

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for Executor

Source§

fn default() -> Executor

Returns the “default value” for a type. Read more
Source§

impl RefUnwindSafe for Executor

Source§

impl UnwindSafe for Executor

Auto Trait Implementations§

§

impl Freeze for Executor

§

impl Send for Executor

§

impl Sync for Executor

§

impl Unpin for Executor

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, 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.