tokio_util/sync/cancellation_token/guard.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`] method on the [`CancellationToken`].
5///
6/// [`drop_guard`]: CancellationToken::drop_guard
7#[derive(Debug)]
8pub struct DropGuard {
9 pub(super) inner: Option<CancellationToken>,
10}
11
12impl DropGuard {
13 /// Returns stored cancellation token and removes this drop guard instance
14 /// (i.e. it will no longer cancel token). Other guards for this token
15 /// are not affected.
16 pub fn disarm(mut self) -> CancellationToken {
17 self.inner
18 .take()
19 .expect("`inner` can be only None in a destructor")
20 }
21}
22
23impl Drop for DropGuard {
24 fn drop(&mut self) {
25 if let Some(inner) = &self.inner {
26 inner.cancel();
27 }
28 }
29}