diff --git a/tokio-util/src/sync/cancellation_token/guard.rs b/tokio-util/src/sync/cancellation_token/guard.rs index f0b6101bd..6c626e5d2 100644 --- a/tokio-util/src/sync/cancellation_token/guard.rs +++ b/tokio-util/src/sync/cancellation_token/guard.rs @@ -10,6 +10,13 @@ pub struct DropGuard { } impl DropGuard { + /// Returns a reference to the cancellation token wrapped by this guard. + pub fn token(&self) -> &CancellationToken { + self.inner + .as_ref() + .expect("`inner` can only be None in a destructor") + } + /// 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. diff --git a/tokio-util/src/sync/cancellation_token/guard_ref.rs b/tokio-util/src/sync/cancellation_token/guard_ref.rs index 20b8fa324..be14e114e 100644 --- a/tokio-util/src/sync/cancellation_token/guard_ref.rs +++ b/tokio-util/src/sync/cancellation_token/guard_ref.rs @@ -13,6 +13,13 @@ pub struct DropGuardRef<'a> { } impl<'a> DropGuardRef<'a> { + /// Returns a reference to the cancellation token wrapped by this guard. + pub fn token(&self) -> &CancellationToken { + self.inner + .as_ref() + .expect("`inner` can only be None in a destructor") + } + /// 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.