aboutsummaryrefslogtreecommitdiff
path: root/helix-event/src/cancel.rs
diff options
context:
space:
mode:
Diffstat (limited to 'helix-event/src/cancel.rs')
-rw-r--r--helix-event/src/cancel.rs19
1 files changed, 19 insertions, 0 deletions
diff --git a/helix-event/src/cancel.rs b/helix-event/src/cancel.rs
new file mode 100644
index 00000000..f027be80
--- /dev/null
+++ b/helix-event/src/cancel.rs
@@ -0,0 +1,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)
+ }
+ }
+}