Give how (seemingly) often a tokio_util::sync::CancellationToken gets used as follows:
tokio::select! {
_ = token.cancelled() => { / something / }
other => other_future => { / something else / }
}
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, with None indicating cancellation.
Solution
I added the described function to tokio_util::sync::CancellationToken and added a test case to the loom tests.
I also had to add the macros feature of Tokio to get access to the select! macro. I thought about using select! provided by futures_util but that would require WaitForCancellationFuture to implement FusedFuture so I had to stick with the tokio one. But maybe this is a not worthy trade off just for a convenience function?
Motivation
Citing from #6566:
Solution
I added the described function to
tokio_util::sync::CancellationToken
and added a test case to the loom tests. I also had to add themacros
feature of Tokio to get access to theselect!
macro. I thought about usingselect!
provided byfutures_util
but that would requireWaitForCancellationFuture
to implementFusedFuture
so I had to stick with the tokio one. But maybe this is a not worthy trade off just for a convenience function?This would close #6566