Struct Spawner

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

Interface for spawning tasks

Spawner provides a subset of the functionality of Executor to allow spawning tasks without access to the full executor.

Spawner instances can be cloned and share the same executor state. Spawners maintain a weak reference to the executor state, so they do not prevent the executor from being dropped. If the executor is dropped, the Spawner will not be able to spawn any more tasks.

To obtain a Spawner from an Executor, use the Executor::spawner method. The dead and default functions return a Spawner that can never spawn tasks.

let executor = Executor::new();
let spawner = executor.spawner();
let final_receiver = unsafe {
    executor.spawn(async move {
        let receiver_1 = spawner.spawn(async { 1 }).unwrap();
        let receiver_2 = spawner.spawn(async { 2 }).unwrap();
        receiver_2.await + receiver_1.await
    })
};
executor.run_until_stalled();
assert_eq!(final_receiver.try_receive(), Ok(3));

Implementations§

Source§

impl<'a> Spawner<'a>

Source

pub fn dead() -> Self

Creates a dummy Spawner that is not associated with any executor and thus cannot spawn tasks.

Source

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

Adds the given future to the executor’s task queue so that it will be polled when the executor is run.

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

If the executor has been dropped, this method will return the future wrapped in a SpawnError. The caller can then reuse the future with another executor or handle the error in some other way.

§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, ) -> Result<Receiver<T>, SpawnError<F>>
where F: IntoFuture<Output = T> + 'a, T: 'a,

Adds the given future to the executor’s task queue so that it will be polled when the executor is run.

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.

If the executor has been dropped, this method will return the future wrapped in a SpawnError. The caller can then reuse the future with another executor or handle the error in some other way.

§Safety

See spawn_pinned for safety considerations.

Trait Implementations§

Source§

impl<'a> Clone for Spawner<'a>

Source§

fn clone(&self) -> Spawner<'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 Spawner<'a>

Source§

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

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

impl<'a> Default for Spawner<'a>

Source§

fn default() -> Spawner<'a>

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

Auto Trait Implementations§

§

impl<'a> Freeze for Spawner<'a>

§

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

§

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

§

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

§

impl<'a> Unpin for Spawner<'a>

§

impl<'a> !UnwindSafe for Spawner<'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.