pub struct StaticLocalExecutor { /* private fields */ }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
impl StaticLocalExecutor
Sourcepub const fn new() -> Self
Available on crate feature static only.
pub const fn new() -> Self
static only.Creates a new StaticLocalExecutor.
§Examples
use async_executor::StaticLocalExecutor;
thread_local! {
static EXECUTOR: StaticLocalExecutor = StaticLocalExecutor::new();
}Sourcepub fn spawn<T: 'static>(
&'static self,
future: impl Future<Output = T> + 'static,
) -> Task<T>
Available on crate feature static only.
pub fn spawn<T: 'static>( &'static self, future: impl Future<Output = T> + 'static, ) -> Task<T>
static only.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");
});Sourcepub unsafe fn spawn_scoped<'a, T: 'a>(
&'static self,
future: impl Future<Output = T> + 'a,
) -> Task<T>
Available on crate feature static only.
pub unsafe fn spawn_scoped<'a, T: 'a>( &'static self, future: impl Future<Output = T> + 'a, ) -> Task<T>
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.
Sourcepub fn try_tick(&self) -> bool
Available on crate feature static only.
pub fn try_tick(&self) -> bool
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::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 foundSourcepub async fn tick(&self)
Available on crate feature static only.
pub async fn tick(&self)
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::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 taskSourcepub async fn run<T>(&self, future: impl Future<Output = T>) -> T
Available on crate feature static only.
pub async fn run<T>(&self, future: impl Future<Output = T>) -> T
static only.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
Available on crate feature static only.
impl Debug for StaticLocalExecutor
static only.Source§impl Default for StaticLocalExecutor
Available on crate feature static only.
impl Default for StaticLocalExecutor
static only.impl RefUnwindSafe for StaticLocalExecutor
static only.impl UnwindSafe for StaticLocalExecutor
static only.