Struct Executor

Source
pub struct Executor<'a, const C: usize = 64> { /* private fields */ }
Expand description

An async executor.

§Examples

A multi-threaded executor:

use async_channel::unbounded;
use easy_parallel::Parallel;

use edge_executor::{Executor, block_on};

let ex: Executor = Default::default();
let (signal, shutdown) = unbounded::<()>();

Parallel::new()
    // Run four executor threads.
    .each(0..4, |_| block_on(ex.run(shutdown.recv())))
    // Run the main future on the current thread.
    .finish(|| block_on(async {
        println!("Hello world!");
        drop(signal);
    }));

Implementations§

Source§

impl<'a, const C: usize> Executor<'a, C>

Source

pub const fn new() -> Self

Creates a new executor.

§Examples
use edge_executor::Executor;

let ex: Executor = Default::default();
Source

pub fn spawn<F>(&self, fut: F) -> Task<F::Output>
where F: Future + Send + 'a, F::Output: Send + 'a,

Spawns a task onto the executor.

§Examples
use edge_executor::Executor;

let ex: Executor = Default::default();

let task = ex.spawn(async {
    println!("Hello world");
});

Note that if the executor’s queue size is equal to the number of currently spawned and running tasks, spawning this additional task might cause the executor to panic later, when the task is scheduled for polling.

Source

pub fn try_tick(&self) -> bool

Attempts to run a task if at least one is scheduled.

Running a scheduled task means simply polling its future once.

§Examples
use edge_executor::Executor;

let ex: Executor = Default::default();
assert!(!ex.try_tick()); // no tasks to run

let task = ex.spawn(async {
    println!("Hello world");
});
assert!(ex.try_tick()); // a task was found
Source

pub async fn tick(&self)

Runs a single task asynchronously.

Running a task means simply polling its future once.

If no tasks are scheduled when this method is called, it will wait until one is scheduled.

§Examples
use edge_executor::{Executor, block_on};

let ex: Executor = Default::default();

let task = ex.spawn(async {
    println!("Hello world");
});
block_on(ex.tick()); // runs the task
Source

pub async fn run<F>(&self, fut: F) -> F::Output
where F: Future + Send + 'a,

Runs the executor asynchronously until the given future completes.

§Examples
use edge_executor::{Executor, block_on};

let ex: Executor = Default::default();

let task = ex.spawn(async { 1 + 2 });
let res = block_on(ex.run(async { task.await * 2 }));

assert_eq!(res, 6);

Trait Implementations§

Source§

impl<'a, const C: usize> Default for Executor<'a, C>

Source§

fn default() -> Self

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

impl<'a, const C: usize> Send for Executor<'a, C>

Source§

impl<'a, const C: usize> Sync for Executor<'a, C>

Auto Trait Implementations§

§

impl<'a, const C: usize = 64> !Freeze for Executor<'a, C>

§

impl<'a, const C: usize = 64> !RefUnwindSafe for Executor<'a, C>

§

impl<'a, const C: usize> Unpin for Executor<'a, C>

§

impl<'a, const C: usize = 64> !UnwindSafe for Executor<'a, C>

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.