Struct smol::Executor

source ·
pub struct Executor<'a> { /* private fields */ }
Expand description

An async executor.

Examples

A multi-threaded executor:

use async_channel::unbounded;
use async_executor::Executor;
use easy_parallel::Parallel;
use futures_lite::future;

let ex = Executor::new();
let (signal, shutdown) = unbounded::<()>();

Parallel::new()
    // Run four executor threads.
    .each(0..4, |_| future::block_on(ex.run(shutdown.recv())))
    // Run the main future on the current thread.
    .finish(|| future::block_on(async {
        println!("Hello world!");
        drop(signal);
    }));

Implementations§

Creates a new executor.

Examples
use async_executor::Executor;

let ex = Executor::new();

Returns true if there are no unfinished tasks.

Examples
use async_executor::Executor;

let ex = Executor::new();
assert!(ex.is_empty());

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

assert!(ex.try_tick());
assert!(ex.is_empty());

Spawns a task onto the executor.

Examples
use async_executor::Executor;

let ex = Executor::new();

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

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

let ex = Executor::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

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

let ex = Executor::new();

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

Runs the executor until the given future completes.

Examples
use async_executor::Executor;
use futures_lite::future;

let ex = Executor::new();

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

assert_eq!(res, 6);

Trait Implementations§

Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.