Struct pasts::Executor

source ·
pub struct Executor<P: Pool = DefaultPool>(_);
Expand description

Pasts’ executor.

Run a Future

It’s relatively simple to block on a future, and run it to completion:

use pasts::Executor;

fn main() {
    Executor::default().block_on(async {
        println!("Hello from a future!");
    });
}

Spawn a Future

You may spawn tasks on an Executor. Only once all tasks have completed, can block_on() return.

use core::time::Duration;

use pasts::Executor;

async fn sleep(seconds: f64) {
    async_std::task::sleep(Duration::from_secs_f64(seconds)).await;
}

fn main() {
    let executor = Executor::default();

    // Spawn before blocking puts the task on a queue.
    executor.spawn_boxed(async {
        sleep(3.0).await;
        println!("3 seconds");
    });

    // Calling `block_on()` starting executing queued tasks.
    executor.clone().block_on(async move {
        // Spawn tasks (without being queued)
        executor.spawn_boxed(async {
            sleep(1.0).await;
            println!("1 second");
        });
        executor.spawn_boxed(async {
            sleep(2.0).await;
            println!("2 seconds");
        });

        // Finish this task before spawned tasks will complete.
        sleep(0.5).await;
        println!("½ second");
    });
}

Recursive block_on()

One cool feature about the pasts executor is that you can run it from within the context of another:

use pasts::Executor;

fn main() {
    Executor::default().block_on(async {
        Executor::default().block_on(async {
            println!("Hello from the future running on the inner executor!");
        });

        println!("Hello from the future running on the outer executor!");
    });
}

Or even resume the executor from within it’s own context:

use pasts::Executor;

fn main() {
    let executor = Executor::default();

    executor.clone().block_on(async move {
        println!("Hello from a future!");

        executor.block_on(async {
            println!("Resuming execution from within the executor context!");
        });
    });
}

Implementations§

source§

impl<P: Pool> Executor<P>

source

pub fn new(pool: P) -> Self

Create a new executor that can only spawn tasks from the current thread.

Custom executors can be built by implementing Pool.

source

pub fn block_on(self, f: impl Future<Output = ()> + 'static)

Block on a future and return it’s result.

Platform-Specific Behavior

When building with feature web, spawns task and returns immediately instead of blocking.

source§

impl<P: Pool> Executor<P>

source

pub fn spawn_notify(&self, n: LocalBoxNotify<'static>)

Spawn a LocalBoxNotify on this executor.

Execution of the LocalBoxNotify will halt after the first poll that returns Ready.

source

pub fn spawn_boxed(&self, f: impl Future<Output = ()> + 'static)

Box and spawn a future on this executor.

Trait Implementations§

source§

impl<P: Pool> Clone for Executor<P>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<P: Pool + Debug> Debug for Executor<P>

source§

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

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

impl Default for Executor

source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<P> RefUnwindSafe for Executor<P>where P: RefUnwindSafe,

§

impl<P> Send for Executor<P>where P: Send + Sync,

§

impl<P> Sync for Executor<P>where P: Send + Sync,

§

impl<P> Unpin for Executor<P>

§

impl<P> UnwindSafe for Executor<P>where P: RefUnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.