-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Conversation
Makes it possible to get a Handle only if a Runtime has been started, without panicing if that isn't the case
I wonder since you just need to check if it makes sense to expose a |
I'm ok with this PR. We can always add a boolean. At the ultra bikeshed level, I wonder if |
tokio/src/runtime/handle.rs
Outdated
/// Returns `None` if no Runtime has been started | ||
/// | ||
/// Contrary to `current`, this never panics | ||
pub fn try_current() -> Option<Self> { |
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.
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.
👍 on |
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.
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 👍
Changed to an error |
Any update 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.
Sorry for the deal, LGTM! Thanks.
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.