Skip to content

Consider hiding StdRng behind a feature flag #938

@matklad

Description

@matklad

Hi!

So I've noticed that even if I use rand with what I think is the minimal profile,

rand = { version = "0.7.1", features = [ "small_rng" ], default_features = false }

I still get both chacha (with its deps) and hc.

However, I am specifically interested in a single fast non-cryptographic manually seeded rng (and small Cargo.lock :) ).

I've tried to apply the following patch to rand, and it seems like it did the trick?

diff --git a/Cargo.toml b/Cargo.toml
index c1a0fe3..b72d3b2 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -39,6 +39,7 @@ getrandom = ["getrandom_package", "rand_core/getrandom"]
 # Configuration:
 simd_support = ["packed_simd"] # enables SIMD support
 small_rng = ["rand_pcg"]    # enables SmallRng
+std_rng = ["rand_chacha", "rand_hc" ]
 
 [workspace]
 members = [
@@ -78,9 +79,9 @@ libc = { version = "0.2.22", default-features = false }
 # Emscripten does not support 128-bit integers, which are used by ChaCha code.
 # We work around this by using a different RNG.
 [target.'cfg(not(target_os = "emscripten"))'.dependencies]
-rand_chacha = { path = "rand_chacha", version = "0.2.1", default-features = false }
+rand_chacha = { path = "rand_chacha", version = "0.2.1", default-features = false, optional = true }
 [target.'cfg(target_os = "emscripten")'.dependencies]
-rand_hc = { path = "rand_hc", version = "0.2" }
+rand_hc = { path = "rand_hc", version = "0.2", optional = true }
 
 [dev-dependencies]
 rand_pcg = { path = "rand_pcg", version = "0.2" }
diff --git a/src/prelude.rs b/src/prelude.rs
index 3c386e8..451faad 100644
--- a/src/prelude.rs
+++ b/src/prelude.rs
@@ -19,6 +19,7 @@
 //! ```
 
 #[doc(no_inline)] pub use crate::distributions::Distribution;
+#[cfg(feature="std_rng")]
 #[doc(no_inline)] pub use crate::rngs::StdRng;
 #[cfg(feature="small_rng")]
 #[doc(no_inline)] pub use crate::rngs::SmallRng;
diff --git a/src/rngs/mod.rs b/src/rngs/mod.rs
index abf3243..aa8f5cf 100644
--- a/src/rngs/mod.rs
+++ b/src/rngs/mod.rs
@@ -105,6 +105,7 @@ pub mod mock;   // Public so we don't export `StepRng` directly, making it a bit
                 // more clear it is intended for testing.
 #[cfg(feature="small_rng")]
 mod small;
+#[cfg(feature="std_rng")]
 mod std;
 #[cfg(feature="std")] pub(crate) mod thread;
 
@@ -113,6 +114,7 @@ mod std;
 
 #[cfg(feature="small_rng")]
 pub use self::small::SmallRng;
+#[cfg(feature="std_rng")]
 pub use self::std::StdRng;
 #[cfg(feature="std")] pub use self::thread::ThreadRng;
 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions