Skip to main content

StaticExecutor

Struct StaticExecutor 

Source
pub struct StaticExecutor { /* private fields */ }
Available on crate features static only.
Expand description

A static-lifetimed async Executor.

This is primarily intended to be used in static variables, or types intended to be used, or can be created in non-static contexts via Executor::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 StaticExecutor::tick or StaticExecutor::run will cause the all spawned tasks to permanently leak. Any tasks at the time will not be cancelled.

Implementations§

Source§

impl StaticExecutor

Source

pub const fn new() -> Self

Available on crate feature static only.

Creates a new StaticExecutor.

§Examples
use async_executor::StaticExecutor;

static EXECUTOR: StaticExecutor = StaticExecutor::new();
Source

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

Available on crate feature static only.

Spawns a task onto the executor.

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

§Examples
use async_executor::StaticExecutor;

static EXECUTOR: StaticExecutor = StaticExecutor::new();

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

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

Available on crate feature static only.

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

Available on crate feature static only.

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::StaticExecutor;

static EXECUTOR: StaticExecutor = StaticExecutor::new();

assert!(!EXECUTOR.try_tick()); // no tasks to run

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

assert!(EXECUTOR.try_tick()); // a task was found
Source

pub async fn tick(&self)

Available on crate feature static only.

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

static EXECUTOR: StaticExecutor = StaticExecutor::new();

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

future::block_on(EXECUTOR.tick()); // runs the task
Source

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

Available on crate feature static only.

Runs the executor until the given future completes.

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

static EXECUTOR: StaticExecutor = StaticExecutor::new();

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

assert_eq!(res, 6);

Trait Implementations§

Source§

impl Debug for StaticExecutor

Available on crate feature static only.
Source§

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

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

impl Default for StaticExecutor

Available on crate feature static only.
Source§

fn default() -> Self

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

impl RefUnwindSafe for StaticExecutor

Available on crate feature static only.
Source§

impl Send for StaticExecutor

Available on crate feature static only.
Source§

impl Sync for StaticExecutor

Available on crate feature static only.
Source§

impl UnwindSafe for StaticExecutor

Available on crate feature static only.

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.