Struct smol::LocalExecutor
source · pub struct LocalExecutor<'a> { /* private fields */ }
Expand description
A thread-local executor.
The executor can only be run on the thread that created it.
Examples
use async_executor::LocalExecutor;
use futures_lite::future;
let local_ex = LocalExecutor::new();
future::block_on(local_ex.run(async {
println!("Hello world!");
}));
Implementations§
source§impl<'a> LocalExecutor<'a>
impl<'a> LocalExecutor<'a>
sourcepub const fn new() -> LocalExecutor<'a>
pub const fn new() -> LocalExecutor<'a>
Creates a single-threaded executor.
Examples
use async_executor::LocalExecutor;
let local_ex = LocalExecutor::new();
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true
if there are no unfinished tasks.
Examples
use async_executor::LocalExecutor;
let local_ex = LocalExecutor::new();
assert!(local_ex.is_empty());
let task = local_ex.spawn(async {
println!("Hello world");
});
assert!(!local_ex.is_empty());
assert!(local_ex.try_tick());
assert!(local_ex.is_empty());
sourcepub fn spawn<T>(&self, future: impl Future<Output = T> + 'a) -> Task<T> ⓘwhere
T: 'a,
pub fn spawn<T>(&self, future: impl Future<Output = T> + 'a) -> Task<T> ⓘwhere
T: 'a,
Spawns a task onto the executor.
Examples
use async_executor::LocalExecutor;
let local_ex = LocalExecutor::new();
let task = local_ex.spawn(async {
println!("Hello world");
});
sourcepub fn try_tick(&self) -> bool
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();
assert!(!ex.try_tick()); // no tasks to run
let task = ex.spawn(async {
println!("Hello world");
});
assert!(ex.try_tick()); // a task was found
sourcepub async fn tick(&self) -> impl Future<Output = ()>
pub async fn tick(&self) -> impl Future<Output = ()>
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();
let task = ex.spawn(async {
println!("Hello world");
});
future::block_on(ex.tick()); // runs the task
sourcepub async fn run<T>(
&self,
future: impl Future<Output = T>
) -> impl Future<Output = T>
pub async fn run<T>(
&self,
future: impl Future<Output = T>
) -> impl Future<Output = T>
Runs the executor until the given future completes.
Examples
use async_executor::LocalExecutor;
use futures_lite::future;
let local_ex = LocalExecutor::new();
let task = local_ex.spawn(async { 1 + 2 });
let res = future::block_on(local_ex.run(async { task.await * 2 }));
assert_eq!(res, 6);
Trait Implementations§
source§impl<'a> Debug for LocalExecutor<'a>
impl<'a> Debug for LocalExecutor<'a>
source§impl<'a> Default for LocalExecutor<'a>
impl<'a> Default for LocalExecutor<'a>
source§fn default() -> LocalExecutor<'a>
fn default() -> LocalExecutor<'a>
Returns the “default value” for a type. Read more