rsiot/components/cmp_websocket_server/
async_task_utils.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
use std::future::Future;

use tokio::select;
use tokio_util::sync::CancellationToken;
use tracing::warn;

pub async fn cancellable_task<T>(future: impl Future<Output = T>, cancel: CancellationToken) -> T
where
    T: Default,
{
    select! {
        val = future => {val},
        _ = cancel.cancelled() => {
            warn!("Cancel task");
            T::default()
        }
    }
}