sync: improve docs of tokio_util::sync::CancellationToken (#7408)

Co-authored-by: Alice Ryhl <aliceryhl@google.com>
This commit is contained in:
Qi 2025-06-19 06:32:41 +08:00 committed by GitHub
parent 013f323def
commit b3a14483bf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 35 additions and 18 deletions

View File

@ -1,4 +1,4 @@
//! An asynchronously awaitable `CancellationToken`. //! An asynchronously awaitable [`CancellationToken`].
//! The token allows to signal a cancellation request to one or more tasks. //! The token allows to signal a cancellation request to one or more tasks.
pub(crate) mod guard; pub(crate) mod guard;
pub(crate) mod guard_ref; pub(crate) mod guard_ref;
@ -112,7 +112,7 @@ impl core::fmt::Debug for CancellationToken {
} }
impl Clone for CancellationToken { impl Clone for CancellationToken {
/// Creates a clone of the `CancellationToken` which will get cancelled /// Creates a clone of the [`CancellationToken`] which will get cancelled
/// whenever the current token gets cancelled, and vice versa. /// whenever the current token gets cancelled, and vice versa.
fn clone(&self) -> Self { fn clone(&self) -> Self {
tree_node::increase_handle_refcount(&self.inner); tree_node::increase_handle_refcount(&self.inner);
@ -135,15 +135,15 @@ impl Default for CancellationToken {
} }
impl CancellationToken { impl CancellationToken {
/// Creates a new `CancellationToken` in the non-cancelled state. /// Creates a new [`CancellationToken`] in the non-cancelled state.
pub fn new() -> CancellationToken { pub fn new() -> CancellationToken {
CancellationToken { CancellationToken {
inner: Arc::new(tree_node::TreeNode::new()), inner: Arc::new(tree_node::TreeNode::new()),
} }
} }
/// Creates a `CancellationToken` which will get cancelled whenever the /// Creates a [`CancellationToken`] which will get cancelled whenever the
/// current token gets cancelled. Unlike a cloned `CancellationToken`, /// current token gets cancelled. Unlike a cloned [`CancellationToken`],
/// cancelling a child token does not cancel the parent token. /// cancelling a child token does not cancel the parent token.
/// ///
/// If the current token is already cancelled, the child token will get /// If the current token is already cancelled, the child token will get
@ -206,12 +206,18 @@ impl CancellationToken {
tree_node::is_cancelled(&self.inner) tree_node::is_cancelled(&self.inner)
} }
/// Returns a `Future` that gets fulfilled when cancellation is requested. /// Returns a [`Future`] that gets fulfilled when cancellation is requested.
///
/// Equivalent to:
///
/// ```ignore
/// async fn cancelled(&self);
/// ```
/// ///
/// The future will complete immediately if the token is already cancelled /// The future will complete immediately if the token is already cancelled
/// when this method is called. /// when this method is called.
/// ///
/// # Cancel safety /// # Cancellation safety
/// ///
/// This method is cancel safe. /// This method is cancel safe.
pub fn cancelled(&self) -> WaitForCancellationFuture<'_> { pub fn cancelled(&self) -> WaitForCancellationFuture<'_> {
@ -221,7 +227,13 @@ impl CancellationToken {
} }
} }
/// Returns a `Future` that gets fulfilled when cancellation is requested. /// Returns a [`Future`] that gets fulfilled when cancellation is requested.
///
/// Equivalent to:
///
/// ```ignore
/// async fn cancelled_owned(self);
/// ```
/// ///
/// The future will complete immediately if the token is already cancelled /// The future will complete immediately if the token is already cancelled
/// when this method is called. /// when this method is called.
@ -229,14 +241,14 @@ impl CancellationToken {
/// The function takes self by value and returns a future that owns the /// The function takes self by value and returns a future that owns the
/// token. /// token.
/// ///
/// # Cancel safety /// # Cancellation safety
/// ///
/// This method is cancel safe. /// This method is cancel safe.
pub fn cancelled_owned(self) -> WaitForCancellationFutureOwned { pub fn cancelled_owned(self) -> WaitForCancellationFutureOwned {
WaitForCancellationFutureOwned::new(self) WaitForCancellationFutureOwned::new(self)
} }
/// Creates a `DropGuard` for this token. /// Creates a [`DropGuard`] for this token.
/// ///
/// Returned guard will cancel this token (and all its children) on drop /// Returned guard will cancel this token (and all its children) on drop
/// unless disarmed. /// unless disarmed.
@ -244,7 +256,7 @@ impl CancellationToken {
DropGuard { inner: Some(self) } DropGuard { inner: Some(self) }
} }
/// Creates a `DropGuardRef` for this token. /// Creates a [`DropGuardRef`] for this token.
/// ///
/// Returned guard will cancel this token (and all its children) on drop /// Returned guard will cancel this token (and all its children) on drop
/// unless disarmed. /// unless disarmed.
@ -253,10 +265,10 @@ impl CancellationToken {
} }
/// Runs a future to completion and returns its result wrapped inside of an `Option` /// Runs a future to completion and returns its result wrapped inside of an `Option`
/// unless the `CancellationToken` is cancelled. In that case the function returns /// unless the [`CancellationToken`] is cancelled. In that case the function returns
/// `None` and the future gets dropped. /// `None` and the future gets dropped.
/// ///
/// # Cancel safety /// # Cancellation safety
/// ///
/// This method is only cancel safe if `fut` is cancel safe. /// This method is only cancel safe if `fut` is cancel safe.
pub async fn run_until_cancelled<F>(&self, fut: F) -> Option<F::Output> pub async fn run_until_cancelled<F>(&self, fut: F) -> Option<F::Output>
@ -299,12 +311,12 @@ impl CancellationToken {
} }
/// Runs a future to completion and returns its result wrapped inside of an `Option` /// Runs a future to completion and returns its result wrapped inside of an `Option`
/// unless the `CancellationToken` is cancelled. In that case the function returns /// unless the [`CancellationToken`] is cancelled. In that case the function returns
/// `None` and the future gets dropped. /// `None` and the future gets dropped.
/// ///
/// The function takes self by value and returns a future that owns the token. /// The function takes self by value and returns a future that owns the token.
/// ///
/// # Cancel safety /// # Cancellation safety
/// ///
/// This method is only cancel safe if `fut` is cancel safe. /// This method is only cancel safe if `fut` is cancel safe.
pub async fn run_until_cancelled_owned<F>(self, fut: F) -> Option<F::Output> pub async fn run_until_cancelled_owned<F>(self, fut: F) -> Option<F::Output>

View File

@ -1,7 +1,9 @@
use crate::sync::CancellationToken; use crate::sync::CancellationToken;
/// A wrapper for cancellation token which automatically cancels /// A wrapper for cancellation token which automatically cancels
/// it on drop. It is created using `drop_guard` method on the `CancellationToken`. /// it on drop. It is created using [`drop_guard`] method on the [`CancellationToken`].
///
/// [`drop_guard`]: CancellationToken::drop_guard
#[derive(Debug)] #[derive(Debug)]
pub struct DropGuard { pub struct DropGuard {
pub(super) inner: Option<CancellationToken>, pub(super) inner: Option<CancellationToken>,

View File

@ -1,9 +1,12 @@
use crate::sync::CancellationToken; use crate::sync::CancellationToken;
/// A wrapper for cancellation token which automatically cancels /// A wrapper for cancellation token which automatically cancels
/// it on drop. It is created using `drop_guard_ref` method on the `CancellationToken`. /// it on drop. It is created using [`drop_guard_ref`] method on the [`CancellationToken`].
/// ///
/// This is a `ref` version of `DropGuard` /// This is a borrowed version of [`DropGuard`].
///
/// [`drop_guard_ref`]: CancellationToken::drop_guard_ref
/// [`DropGuard`]: super::DropGuard
#[derive(Debug)] #[derive(Debug)]
pub struct DropGuardRef<'a> { pub struct DropGuardRef<'a> {
pub(super) inner: Option<&'a CancellationToken>, pub(super) inner: Option<&'a CancellationToken>,