Struct async_executor::StaticLocalExecutor

source ·
pub struct StaticLocalExecutor { /* private fields */ }
Available on crate feature static only.
Expand description

A static async LocalExecutor created from LocalExecutor::leak.

This is primarily intended to be used in thread_local variables, or can be created in non-static contexts via LocalExecutor::leak.

Spawning, running, and finishing tasks are optimized with the assumption that the executor will never be Drop’ed. A static executor may require signficantly less overhead in both single-threaded and mulitthreaded use cases.

As this type does not implement Drop, losing the handle to the executor or failing to consistently drive the executor with StaticLocalExecutor::tick or StaticLocalExecutor::run will cause the all spawned tasks to permanently leak. Any tasks at the time will not be cancelled.

Implementations§

source§

impl StaticLocalExecutor

source

pub const fn new() -> Self

Creates a new StaticLocalExecutor.

§Examples
use async_executor::StaticLocalExecutor;

thread_local! {
    static EXECUTOR: StaticLocalExecutor = StaticLocalExecutor::new();
}
source

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

Spawns a task onto the executor.

Note: unlike LocalExecutor::spawn, this function requires being called with a 'static borrow on the executor.

§Examples
use async_executor::LocalExecutor;

let ex = LocalExecutor::new().leak();

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

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

Spawns a non-'static task onto the executor.

§Safety

The caller must ensure that the returned task terminates or is cancelled before the end of ’a.

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 async_executor::LocalExecutor;

let ex = LocalExecutor::new().leak();
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.

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 async_executor::LocalExecutor;
use futures_lite::future;

let ex = LocalExecutor::new().leak();

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

pub async fn run<T>(&self, future: impl Future<Output = T>) -> T

Runs the executor until the given future completes.

§Examples
use async_executor::LocalExecutor;
use futures_lite::future;

let ex = LocalExecutor::new().leak();

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

assert_eq!(res, 6);

Trait Implementations§

source§

impl Debug for StaticLocalExecutor

source§

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

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

impl Default for StaticLocalExecutor

source§

fn default() -> Self

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

impl RefUnwindSafe for StaticLocalExecutor

source§

impl UnwindSafe for StaticLocalExecutor

Auto Trait Implementations§

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.