1 //! Utility to abort an async task if its logical owner is dropped 2 3 use tokio::task::JoinHandle; 4 5 /// When this struct is dropped, the contained task will be aborted 6 #[derive(Debug)] 7 pub struct OwnedHandle<T> { 8 handle: JoinHandle<T>, 9 } 10 11 impl<T> From<JoinHandle<T>> for OwnedHandle<T> { from(handle: JoinHandle<T>) -> Self12 fn from(handle: JoinHandle<T>) -> Self { 13 Self { handle } 14 } 15 } 16 17 impl<T> Drop for OwnedHandle<T> { drop(&mut self)18 fn drop(&mut self) { 19 self.handle.abort(); 20 } 21 } 22