-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add run_until_cancelled_owned to CancellationToken #7081
base: master
Are you sure you want to change the base?
Conversation
03bf4a7
to
a99e2bc
Compare
e66f923
to
d561cd1
Compare
Thanks for the review @tglane! I have modified the commit as you requested. |
/// is cancelled or a given Future gets resolved. It is biased towards the | ||
/// Future completion. | ||
#[must_use = "futures do nothing unless polled"] | ||
struct RunUntilCancelledFuture<C, F: Future> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Know this type basically is a biased select future I think because C
technically is not required to be some sort of CancellationToken
. I think we could replace the usage of this type with a tokio::select!
if we add the macros feature to tokio-util
s tokio
dependency. @Darksonn do you have a optionion on that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason that this PR is not just this?
pub async fn run_until_cancelled_owned<F>(self, fut: F) -> Option<F::Output>
where
F: Future,
{
self.run_until_cancelled(fut).await
}
I believe it should be sufficient - the async/await syntax takes care of making it owned.
Oh yeah, you are right that works! So that would be an even better approach here. |
f62cb03
to
3f55bf6
Compare
Oh wow yeah I definitely overcomplicated this 🤦 |
3f55bf6
to
33dd526
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apart from this nit it looks good to me know.
/// 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. | ||
/// | ||
/// # Cancel safety | ||
/// | ||
/// This method is only cancel safe if `fut` is cancel safe. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The docs should mention the difference to run_until_cancelled
in some way.
Maybe add something like this to the docs:
///
/// The function takes self by value and returns a future that owns the
/// token.
Pretty natural addition to match the
cancelled_owned
fn.Motivation
Solution
Just a carbon copy of the existing fn switching to the appropriate cancellation future.