I propose adding a method to CancellationToken that takes a Future, runs it to completion, and returns its output — unless the token is cancelled before the future completes, in which case the future is dropped. I feel the best type for the result of this composed future would be an Option<Fut::Output>, with None indicating cancellation.
Example usage of this new method (here called run_until_cancelled()):
if let Some(r) = token.run_until_cancelled(fut).await {
/* Future completed; do stuff with future's return value `r` */
} else {
/* Token & future were cancelled; do stuff for that */
}
(This is a variant of the idea proposed in #4598; I was advised to create a new issue for this.)
Give how (seemingly) often a
tokio_util::sync::CancellationToken
gets used as follows:I propose adding a method to
CancellationToken
that takes aFuture
, runs it to completion, and returns its output — unless the token is cancelled before the future completes, in which case the future is dropped. I feel the best type for the result of this composed future would be anOption<Fut::Output>
, withNone
indicating cancellation.Example usage of this new method (here called
run_until_cancelled()
):