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

feat: Add no_std mode #50

Merged
merged 2 commits into from
Feb 19, 2023
Merged

feat: Add no_std mode #50

merged 2 commits into from
Feb 19, 2023

Conversation

notgull
Copy link
Member

@notgull notgull commented Feb 13, 2023

This PR adds a feature named std. When disabled, the thread-local generator and Rng::new() are disabled. This allows fastrand to be used in no_std environments.

src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated
@@ -598,6 +611,7 @@ impl Rng {
}

/// Run an operation with the current thread-local generator.
#[cfg(feature = "std")]
#[inline]
fn with_rng<R>(f: impl FnOnce(&mut Rng) -> R) -> R {
Copy link
Member

Choose a reason for hiding this comment

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

wouldn't it make more sense to put this into $crate scope and get rid of the dozens of functions which just wrap with_rng?

anyways, it might be a good idea to move all the thread-local+Cell dependent stuff into a separate module, and include it here with

#[cfg(feature = "std")]
mod glrng;
#[cfg(feature = "std")]
pub use glrng::*;

that would imo be much cleaner.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm hesitant to expose (try_)with_rng, since I feel like the API is much more idiomatic without giving direct access to the thread-local RNG. Anyways, I moved the thread-local things into a global_rng module.

@notgull notgull merged commit 12e49ca into master Feb 19, 2023
@notgull notgull deleted the notgull/no_std branch February 19, 2023 00:20
@notgull notgull mentioned this pull request Apr 7, 2023
notgull added a commit that referenced this pull request Jun 9, 2023
- **Breaking:** Remove interior mutability from `Rng`. (#47)
- Add a `fork()` method. (#49)
- Add a `no_std` mode. (#50)
- Add an iterator selection function. (#51)
- Add a `choose_multiple()` function for sampling several elements from an iterator. (#55)
- Use the `getrandom` crate for seeding on WebAssembly targets if the `js` feature is enabled. (#60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants