Struct Executor

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

Interface for running concurrent tasks

You call the spawn_pinned or spawn method to add a task to the executor. Just adding a task to the executor does not run it. You need to call the step or run_until_stalled method to run the tasks.

Executor implements Clone but all clones share the same set of tasks. Separately created Executor instances do not share tasks.

Implementations§

Source§

impl<'a> Executor<'a>

Source

pub fn new() -> Self

Creates a new Executor with an empty task queue.

Source

pub fn wake_count(&self) -> usize

Returns the number of tasks that have been woken up but not yet polled.

Source

pub unsafe fn spawn_pinned( &self, future: Pin<Box<dyn Future<Output = ()> + 'a>>, )

Adds a task to the task queue.

The added task is not polled immediately. It will be polled when the executor runs tasks.

§Safety

It may be surprising that this method is unsafe. The reason is that the Waker available in the Context passed to the future’s poll method is thread-unsafe despite Waker being Send and Sync. The Waker is not protected by a lock or atomic operation, and it is your sole responsibility to ensure that the Waker is not passed to or accessed from other threads.

Source

pub unsafe fn spawn<F, T>(&self, future: F) -> Receiver<T>
where F: IntoFuture<Output = T> + 'a, T: 'a,

Adds a task to the task queue.

This method is an extended version of spawn_pinned that can take a non-pinned future and may return a non-unit output. The result of the future will be sent to the returned receiver.

The added task is not polled immediately. It will be polled when the executor runs tasks.

§Safety

See spawn_pinned for safety considerations.

Source

pub fn spawner(&self) -> Spawner<'a>

Returns a Spawner that can spawn tasks.

Source

pub fn step(&self) -> Option<bool>

Runs a task that has been woken up.

This method removes a single task from the task queue and polls it. Returns:

  • Some(true) if the task is complete
  • Some(false) if the task is not complete
  • None if there are no tasks to run

This method panics if the task is polled recursively.

Source

pub fn run_until_stalled(&self) -> usize

Runs tasks until there are no more tasks to run.

This method repeatedly calls step until it returns None, that is, there are no more tasks that have been woken up. Returns the number of completed tasks.

This method panics if a task is polled recursively.

Trait Implementations§

Source§

impl<'a> Clone for Executor<'a>

Source§

fn clone(&self) -> Executor<'a>

Returns a duplicate 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<'a> Debug for Executor<'a>

Source§

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

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

impl<'a> Default for Executor<'a>

Source§

fn default() -> Executor<'a>

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

Auto Trait Implementations§

§

impl<'a> Freeze for Executor<'a>

§

impl<'a> !RefUnwindSafe for Executor<'a>

§

impl<'a> !Send for Executor<'a>

§

impl<'a> !Sync for Executor<'a>

§

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

§

impl<'a> !UnwindSafe for Executor<'a>

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where 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 T
where T: Clone,

Source§

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 T
where U: Into<T>,

Source§

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

Source§

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.