sync: add CancellationToken::run_until_cancelled_owned (#7081)

This commit is contained in:
Ty Larrabee
2025-03-10 14:24:25 +01:00
committed by GitHub
parent afd3678f89
commit e4a39d2ef6
2 changed files with 71 additions and 0 deletions
+16
View File
@@ -287,6 +287,22 @@ impl CancellationToken {
}
.await
}
/// 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
/// `None` and the future gets dropped.
///
/// The function takes self by value and returns a future that owns the token.
///
/// # Cancel safety
///
/// 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>
where
F: Future,
{
self.run_until_cancelled(fut).await
}
}
// ===== impl WaitForCancellationFuture =====