Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tysen
Copy link

@tysen tysen commented Jan 9, 2025

Pretty natural addition to match the cancelled_owned fn.

Motivation

let token = CancellationToken::new();
tokio::spawn(token.run_until_cancelled(my_async_fn())); // doesn't compile
tokio::spawn(async move { // current workaround
    token.run_until_cancelled(my_async_fn())).await
});
tokio::spawn(token.run_until_cancelled_owned(my_async_fn())); // compiles, looks nicer

Solution

Just a carbon copy of the existing fn switching to the appropriate cancellation future.

@tysen tysen force-pushed the add-run-until-cancelled-owned branch from 03bf4a7 to a99e2bc Compare January 9, 2025 18:49
@Darksonn Darksonn added A-tokio-util Area: The tokio-util crate M-sync Module: tokio/sync labels Jan 10, 2025
@tysen tysen force-pushed the add-run-until-cancelled-owned branch 2 times, most recently from e66f923 to d561cd1 Compare February 19, 2025 23:40
@tysen
Copy link
Author

tysen commented Feb 20, 2025

Thanks for the review @tglane! I have modified the commit as you requested.

@tysen tysen requested a review from tglane February 28, 2025 03:58
/// 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> {
Copy link
Contributor

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-utils tokio dependency. @Darksonn do you have a optionion on that?

Copy link
Contributor

@Darksonn Darksonn left a 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.

@tglane
Copy link
Contributor

tglane commented Mar 3, 2025

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.

@tysen tysen force-pushed the add-run-until-cancelled-owned branch from f62cb03 to 3f55bf6 Compare March 4, 2025 20:05
@tysen
Copy link
Author

tysen commented Mar 4, 2025

Oh wow yeah I definitely overcomplicated this 🤦

@tysen tysen requested a review from Darksonn March 4, 2025 20:52
@tysen tysen force-pushed the add-run-until-cancelled-owned branch from 3f55bf6 to 33dd526 Compare March 4, 2025 23:14
Copy link
Contributor

@tglane tglane left a 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.

Comment on lines +291 to +299
/// 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.
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio-util Area: The tokio-util crate M-sync Module: tokio/sync
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants