1use futures_util::future::{self, AbortHandle, Abortable, Future};
2
3#[derive(Debug)]
4pub struct DropAbortHandle(AbortHandle);
5impl Drop for DropAbortHandle {
6 fn drop(&mut self) {
7 self.0.abort();
8 }
9}
10
11pub fn abortable<Fut>(fut: Fut) -> (Abortable<Fut>, DropAbortHandle)
12where
13 Fut: Future,
14{
15 let (fut, handle) = future::abortable(fut);
16 (fut, DropAbortHandle(handle))
17}
18
19#[cfg(test)]
20mod tests {
21 use super::*;
22 use futures_util::future::Aborted;
23
24 #[test]
25 fn test_drop_abort() {
26 futures::executor::block_on(async {
27 let (fut, handle) = abortable(async {});
28 drop(handle);
29 let r = fut.await;
30 assert_eq!(r, Err(Aborted))
31 });
32 }
33}