aboutsummaryrefslogtreecommitdiff
path: root/src/sync/cancellation_token/guard.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/sync/cancellation_token/guard.rs')
-rw-r--r--src/sync/cancellation_token/guard.rs27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/sync/cancellation_token/guard.rs b/src/sync/cancellation_token/guard.rs
new file mode 100644
index 0000000..54ed7ea
--- /dev/null
+++ b/src/sync/cancellation_token/guard.rs
@@ -0,0 +1,27 @@
+use crate::sync::CancellationToken;
+
+/// A wrapper for cancellation token which automatically cancels
+/// it on drop. It is created using `drop_guard` method on the `CancellationToken`.
+#[derive(Debug)]
+pub struct DropGuard {
+ pub(super) inner: Option<CancellationToken>,
+}
+
+impl DropGuard {
+ /// Returns stored cancellation token and removes this drop guard instance
+ /// (i.e. it will no longer cancel token). Other guards for this token
+ /// are not affected.
+ pub fn disarm(mut self) -> CancellationToken {
+ self.inner
+ .take()
+ .expect("`inner` can be only None in a destructor")
+ }
+}
+
+impl Drop for DropGuard {
+ fn drop(&mut self) {
+ if let Some(inner) = &self.inner {
+ inner.cancel();
+ }
+ }
+}