tokio_util/sync/cancellation_token/
guard_ref.rs

1use crate::sync::CancellationToken;
2
3/// A wrapper for cancellation token which automatically cancels
4/// it on drop. It is created using [`drop_guard_ref`] method on the [`CancellationToken`].
5///
6/// This is a borrowed version of [`DropGuard`].
7///
8/// [`drop_guard_ref`]: CancellationToken::drop_guard_ref
9/// [`DropGuard`]: super::DropGuard
10#[derive(Debug)]
11pub struct DropGuardRef<'a> {
12    pub(super) inner: Option<&'a CancellationToken>,
13}
14
15impl<'a> DropGuardRef<'a> {
16    /// Returns stored cancellation token and removes this drop guard instance
17    /// (i.e. it will no longer cancel token). Other guards for this token
18    /// are not affected.
19    pub fn disarm(mut self) -> &'a CancellationToken {
20        self.inner
21            .take()
22            .expect("`inner` can be only None in a destructor")
23    }
24}
25
26impl Drop for DropGuardRef<'_> {
27    fn drop(&mut self) {
28        if let Some(inner) = self.inner {
29            inner.cancel();
30        }
31    }
32}