API to check whether we are inside a simulation#258
Merged
mcches merged 1 commit intotokio-rs:mainfrom Jan 6, 2026
Merged
Conversation
mcches
approved these changes
Dec 29, 2025
Contributor
mcches
left a comment
There was a problem hiding this comment.
A couple alternatives...but the proposed is also Ok.
Option 1: Use try_current
pub fn in_simulation() -> bool {
World::try_current(|_| ()).is_ok()
}This uses the existing try_current method which already checks CURRENT.is_set() and returns a Result.
Option 2: Add a dedicated is_set() method to World
In src/world.rs:
pub(crate) fn is_set() -> bool {
CURRENT.is_set()
}Then in src/lib.rs:
pub fn in_simulation() -> bool {
World::is_set()
}
Contributor
Author
|
Changed to option 1. I initially avoided this because I didn't like the additional overhead, but it's probably silly to worry about that here. |
mcches
approved these changes
Jan 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds
turmoil::in_simulation, an API that allows checking whether the caller is currently running inside a simulation.The same information was already obtainable by
turmoil::sim_elapsed().is_some(), but it seems good to have a less obscure way to do this.For context, my reason for wanting this is that I have a
TcpStreamwrapper that supports both tokio and turmoil streams and decides which type to use at runtime. Roughly:This is for situations where I can't decide this at compile time, for example because I'm using turmoil in Rust tests and there are other tests in the same binary that want to use the tokio types instead.
Happy to discuss other ways of doing this, if folks have ideas!