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