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

runtime: add Handle::try_current #2118

Merged
merged 2 commits into from
Jan 20, 2020
Merged

Conversation

tomaka
Copy link
Contributor

@tomaka tomaka commented Jan 15, 2020

Motivation

There is currently no way to conditonnally determine whether tokio::spawn will succeed.
Since this is a niche situation, I went for putting a method on the lower-level component.

Makes it possible to get a Handle only if a Runtime has been started, without panicing if that isn't the case
@LucioFranco
Copy link
Member

I wonder since you just need to check if it makes sense to expose a bool over Option<Self>? like Handle::is_current() -> bool

@carllerche
Copy link
Member

is_current implies a check for a specific handle, so you would have: Handle::is_current(&self), which wouldn't help here.

I'm ok with this PR. We can always add a boolean. At the ultra bikeshed level, I wonder if try_current should return a Result... but that isn't critical. I also don't think there are error cases.

/// Returns `None` if no Runtime has been started
///
/// Contrary to `current`, this never panics
pub fn try_current() -> Option<Self> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I definitely have a slight preference for @carllerche's suggestion that this return a Result...the error type could just format with the message "not currently running on the Tokio runtime.", like the expect in current(). Not a big deal, though, IMO.

@LucioFranco
Copy link
Member

👍 on Result over Option same thing for thread_locals https://doc.rust-lang.org/stable/std/thread/struct.LocalKey.html#method.try_with

Copy link
Member

@carllerche carllerche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like there is consensus that try_current should return a Result. To follow other patterns, the error probably should be named.... TryCurrentError 🤷. It probably can be an empty struct: struct TryCurrentError(()) as there is only one failure case today.

Thanks for getting this going 👍

@tomaka
Copy link
Contributor Author

tomaka commented Jan 16, 2020

Changed to an error

@tomaka
Copy link
Contributor Author

tomaka commented Jan 20, 2020

Any update on that?

Copy link
Member

@LucioFranco LucioFranco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the deal, LGTM! Thanks.

@LucioFranco LucioFranco merged commit 1475448 into tokio-rs:master Jan 20, 2020
@tomaka tomaka deleted the try-current branch January 20, 2020 16:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants