summaryrefslogtreecommitdiff
path: root/helix-event/src/cancel.rs
blob: f027be80e8dee775781de17c2cabf5e5838bc106 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
use std::future::Future;

pub use oneshot::channel as cancelation;
use tokio::sync::oneshot;

pub type CancelTx = oneshot::Sender<()>;
pub type CancelRx = oneshot::Receiver<()>;

pub async fn cancelable_future<T>(future: impl Future<Output = T>, cancel: CancelRx) -> Option<T> {
    tokio::select! {
        biased;
        _ = cancel => {
            None
        }
        res = future => {
            Some(res)
        }
    }
}