Trait Spawner

Source
pub trait Spawner:
    Clone
    + Send
    + Sync
    + 'static {
    // Required methods
    fn spawn<F, Fut, T>(self, f: F) -> Handle<T> 
       where F: FnOnce(Self) -> Fut + Send + 'static,
             Fut: Future<Output = T> + Send + 'static,
             T: Send + 'static;
    fn spawn_ref<F, T>(&mut self) -> impl FnOnce(F) -> Handle<T> + 'static
       where F: Future<Output = T> + Send + 'static,
             T: Send + 'static;
    fn spawn_blocking<F, T>(self, f: F) -> Handle<T> 
       where F: FnOnce() -> T + Send + 'static,
             T: Send + 'static;
    fn stop(&self, value: i32);
    fn stopped(&self) -> Signal;
}
Expand description

Interface that any task scheduler must implement to spawn tasks.

Required Methods§

Source

fn spawn<F, Fut, T>(self, f: F) -> Handle<T>
where F: FnOnce(Self) -> Fut + Send + 'static, Fut: Future<Output = T> + Send + 'static, T: Send + 'static,

Enqueue a task to be executed.

Unlike a future, a spawned task will start executing immediately (even if the caller does not await the handle).

Spawned tasks consume the context used to create them. This ensures that context cannot be shared between tasks and that a task’s context always comes from somewhere.

Source

fn spawn_ref<F, T>(&mut self) -> impl FnOnce(F) -> Handle<T> + 'static
where F: Future<Output = T> + Send + 'static, T: Send + 'static,

Enqueue a task to be executed (without consuming the context).

Unlike a future, a spawned task will start executing immediately (even if the caller does not await the handle).

In some cases, it may be useful to spawn a task without consuming the context (e.g. starting an actor that already has a reference to context).

§Warning

If this function is used to spawn multiple tasks from the same context, the runtime will panic to prevent accidental misuse.

Source

fn spawn_blocking<F, T>(self, f: F) -> Handle<T>
where F: FnOnce() -> T + Send + 'static, T: Send + 'static,

Enqueue a blocking task to be executed.

This method is designed for synchronous, potentially long-running operations that should not block the asynchronous event loop. The task starts executing immediately, and the returned handle can be awaited to retrieve the result.

§Warning

Blocking tasks cannot be aborted.

Source

fn stop(&self, value: i32)

Signals the runtime to stop execution and that all outstanding tasks should perform any required cleanup and exit. This method is idempotent and can be called multiple times.

This method does not actually kill any tasks but rather signals to them, using the Signal returned by stopped, that they should exit.

Source

fn stopped(&self) -> Signal

Returns an instance of a Signal that resolves when stop is called by any task.

If stop has already been called, the returned Signal will resolve immediately.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl Spawner for commonware_runtime::deterministic::Context

Source§

impl Spawner for commonware_runtime::tokio::Context