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