Struct LocalExecutor

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

A thread-local executor.

The executor can only be run on the thread that created it.

§Examples

use edge_executor::{LocalExecutor, block_on};

let local_ex: LocalExecutor = Default::default();

block_on(local_ex.run(async {
    println!("Hello world!");
}));

Implementations§

Source§

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

Source

pub const fn new() -> Self

Creates a single-threaded executor.

§Examples
use edge_executor::LocalExecutor;

let local_ex: LocalExecutor = Default::default();
Source

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

Spawns a task onto the executor.

§Examples
use edge_executor::LocalExecutor;

let local_ex: LocalExecutor = Default::default();

let task = local_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::LocalExecutor;

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

let task = local_ex.spawn(async {
    println!("Hello world");
});
assert!(local_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::{LocalExecutor, block_on};

let local_ex: LocalExecutor = Default::default();

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

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

Runs the executor asynchronously until the given future completes.

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

let local_ex: LocalExecutor = Default::default();

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

assert_eq!(res, 6);

Trait Implementations§

Source§

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

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

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

§

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

§

impl<'a, const C: usize = 64> !Send for LocalExecutor<'a, C>

§

impl<'a, const C: usize = 64> !Sync for LocalExecutor<'a, C>

§

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

§

impl<'a, const C: usize = 64> !UnwindSafe for LocalExecutor<'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.