sqlxmq/
utils.rs

1use std::any::Any;
2use std::fmt::{self, Debug};
3use std::ops::{Deref, DerefMut};
4
5use tokio::task::JoinHandle;
6
7#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
8pub struct Opaque<T: Any>(pub T);
9
10impl<T: Any> Debug for Opaque<T> {
11    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
12        <dyn Any>::fmt(&self.0, f)
13    }
14}
15
16impl<T: Any> Deref for Opaque<T> {
17    type Target = T;
18
19    fn deref(&self) -> &Self::Target {
20        &self.0
21    }
22}
23
24impl<T: Any> DerefMut for Opaque<T> {
25    fn deref_mut(&mut self) -> &mut Self::Target {
26        &mut self.0
27    }
28}
29
30/// A handle to a background task which will be automatically cancelled if
31/// the handle is dropped. Extract the inner join handle to prevent this
32/// behaviour.
33#[derive(Debug)]
34pub struct OwnedHandle(Option<JoinHandle<()>>);
35
36impl OwnedHandle {
37    /// Construct a new `OwnedHandle` from the provided `JoinHandle`
38    pub fn new(inner: JoinHandle<()>) -> Self {
39        Self(Some(inner))
40    }
41    /// Get back the original `JoinHandle`
42    pub fn into_inner(mut self) -> JoinHandle<()> {
43        self.0.take().expect("Only consumed once")
44    }
45    /// Stop the task and wait for it to finish.
46    pub async fn stop(self) {
47        let handle = self.into_inner();
48        handle.abort();
49        let _ = handle.await;
50    }
51}
52
53impl Drop for OwnedHandle {
54    fn drop(&mut self) {
55        if let Some(handle) = self.0.take() {
56            handle.abort();
57        }
58    }
59}