wsio_core/traits/task/
spawner.rs1use std::sync::Arc;
2
3use anyhow::Result;
4use tokio::{
5 select,
6 spawn,
7};
8use tokio_util::sync::CancellationToken;
9
10pub trait TaskSpawner: Send + Sync + 'static {
11 fn cancel_token(&self) -> Arc<CancellationToken>;
12
13 #[inline]
14 fn spawn_task<F: Future<Output = Result<()>> + Send + 'static>(&self, future: F) {
15 let cancel_token = self.cancel_token().clone();
16 spawn(async move {
17 select! {
18 _ = cancel_token.cancelled() => {},
19 _ = future => {},
20 }
21 });
22 }
23}