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§
Sourcefn spawn(&self, fut: Pin<Box<dyn Future<Output = ()> + Send>>) -> JoinHandle<()> ⓘ
fn spawn(&self, fut: Pin<Box<dyn Future<Output = ()> + Send>>) -> JoinHandle<()> ⓘ
Spawns the task onto the runtime.
See also [Handle::spawn
].
Sourcefn spawn_critical(
&self,
name: &'static str,
fut: Pin<Box<dyn Future<Output = ()> + Send>>,
) -> JoinHandle<()> ⓘ
fn spawn_critical( &self, name: &'static str, fut: Pin<Box<dyn Future<Output = ()> + Send>>, ) -> JoinHandle<()> ⓘ
This spawns a critical task onto the runtime.
Trait Implementations§
Source§impl<'clone> Clone for Box<dyn TaskSpawner + 'clone>
impl<'clone> Clone for Box<dyn TaskSpawner + 'clone>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read more