reth::tasks

Trait TaskSpawner

pub trait TaskSpawner:
    Send
    + Sync
    + Unpin
    + Debug
    + DynClone {
    // Required methods
    fn spawn(
        &self,
        fut: Pin<Box<dyn Future<Output = ()> + Send>>,
    ) -> JoinHandle<()> ;
    fn spawn_critical(
        &self,
        name: &'static str,
        fut: Pin<Box<dyn Future<Output = ()> + Send>>,
    ) -> JoinHandle<()> ;
    fn spawn_blocking(
        &self,
        fut: Pin<Box<dyn Future<Output = ()> + Send>>,
    ) -> JoinHandle<()> ;
    fn spawn_critical_blocking(
        &self,
        name: &'static str,
        fut: Pin<Box<dyn Future<Output = ()> + Send>>,
    ) -> JoinHandle<()> ;
}
Expand description

A type that can spawn tasks.

The main purpose of this type is to abstract over TaskExecutor so it’s more convenient to provide default impls for testing.

§Examples

Use the TokioTaskExecutor that spawns with [tokio::task::spawn]

use reth_tasks::{TaskSpawner, TokioTaskExecutor};
let executor = TokioTaskExecutor::default();

let task = executor.spawn(Box::pin(async {
    // -- snip --
}));
task.await.unwrap();

Use the TaskExecutor that spawns task directly onto the tokio runtime via the [Handle].

fn t() {
 use reth_tasks::TaskSpawner;
let rt = tokio::runtime::Runtime::new().unwrap();
let manager = TaskManager::new(rt.handle().clone());
let executor = manager.executor();
let task = TaskSpawner::spawn(&executor, Box::pin(async {
    // -- snip --
}));
rt.block_on(task).unwrap();

The TaskSpawner trait is DynClone so Box<dyn TaskSpawner> are also Clone.

Required Methods§

fn spawn(&self, fut: Pin<Box<dyn Future<Output = ()> + Send>>) -> JoinHandle<()>

Spawns the task onto the runtime. See also [Handle::spawn].

fn spawn_critical( &self, name: &'static str, fut: Pin<Box<dyn Future<Output = ()> + Send>>, ) -> JoinHandle<()>

This spawns a critical task onto the runtime.

fn spawn_blocking( &self, fut: Pin<Box<dyn Future<Output = ()> + Send>>, ) -> JoinHandle<()>

Spawns a blocking task onto the runtime.

fn spawn_critical_blocking( &self, name: &'static str, fut: Pin<Box<dyn Future<Output = ()> + Send>>, ) -> JoinHandle<()>

This spawns a critical blocking task onto the runtime.

Trait Implementations§

§

impl<'clone> Clone for Box<dyn TaskSpawner + 'clone>

§

fn clone(&self) -> Box<dyn TaskSpawner + 'clone>

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
§

impl<'clone> Clone for Box<dyn TaskSpawner + Send + 'clone>

§

fn clone(&self) -> Box<dyn TaskSpawner + Send + 'clone>

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
§

impl<'clone> Clone for Box<dyn TaskSpawner + Sync + Send + 'clone>

§

fn clone(&self) -> Box<dyn TaskSpawner + Sync + Send + 'clone>

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
§

impl<'clone> Clone for Box<dyn TaskSpawner + Sync + 'clone>

§

fn clone(&self) -> Box<dyn TaskSpawner + Sync + 'clone>

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

Implementations on Foreign Types§

§

impl<'a, T> TaskSpawner for &'a T
where T: 'a + TaskSpawner + ?Sized, &'a T: Send + Sync + Unpin + Debug + DynClone,

§

fn spawn(&self, fut: Pin<Box<dyn Future<Output = ()> + Send>>) -> JoinHandle<()>

§

fn spawn_critical( &self, name: &'static str, fut: Pin<Box<dyn Future<Output = ()> + Send>>, ) -> JoinHandle<()>

§

fn spawn_blocking( &self, fut: Pin<Box<dyn Future<Output = ()> + Send>>, ) -> JoinHandle<()>

§

fn spawn_critical_blocking( &self, name: &'static str, fut: Pin<Box<dyn Future<Output = ()> + Send>>, ) -> JoinHandle<()>

§

impl<T> TaskSpawner for Arc<T>
where T: TaskSpawner + ?Sized, Arc<T>: Send + Sync + Unpin + Debug + DynClone,

§

fn spawn(&self, fut: Pin<Box<dyn Future<Output = ()> + Send>>) -> JoinHandle<()>

§

fn spawn_critical( &self, name: &'static str, fut: Pin<Box<dyn Future<Output = ()> + Send>>, ) -> JoinHandle<()>

§

fn spawn_blocking( &self, fut: Pin<Box<dyn Future<Output = ()> + Send>>, ) -> JoinHandle<()>

§

fn spawn_critical_blocking( &self, name: &'static str, fut: Pin<Box<dyn Future<Output = ()> + Send>>, ) -> JoinHandle<()>

Implementors§