[][src]Struct smol::Executor

pub struct Executor<'a> { /* fields omitted */ }

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

impl<'a> Executor<'a>[src]

pub const fn new() -> Executor<'a>[src]

Creates a new executor.

Examples

use async_executor::Executor;

let ex = Executor::new();

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

Notable traits for Task<T>

impl<T> Future for Task<T> type Output = T;
where
    T: 'a + Send
[src]

Spawns a task onto the executor.

Examples

use async_executor::Executor;

let ex = Executor::new();

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

pub fn try_tick(&self) -> bool[src]

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

pub async fn tick(&'_ self)[src]

Run 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

pub async fn run<T>(&'_ self, future: impl Future<Output = T>) -> T[src]

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

impl<'a> Debug for Executor<'a>[src]

impl<'a> Default for Executor<'a>[src]

impl<'_> Drop for Executor<'_>[src]

impl<'_> RefUnwindSafe for Executor<'_>[src]

impl<'_> Send for Executor<'_>[src]

impl<'_> Sync for Executor<'_>[src]

impl<'_> UnwindSafe for Executor<'_>[src]

Auto Trait Implementations

impl<'a> Unpin for Executor<'a>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.