diff --git a/Cargo.toml b/Cargo.toml index 21781ef538..da14228a54 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,13 +28,13 @@ features = ["small_rng", "serde1"] [features] # Meta-features: -default = ["std", "std_rng"] +default = ["std", "std_rng", "getrandom"] nightly = [] # some additions requiring nightly Rust serde1 = ["serde", "rand_core/serde1"] # Option (enabled by default): without "std" rand uses libcore; this option # enables functionality expected to be available on a standard platform. -std = ["rand_core/std", "rand_chacha/std", "alloc", "getrandom", "libc"] +std = ["rand_core/std", "rand_chacha?/std", "alloc", "libc"] # Option: "alloc" enables support for Vec and Box when not using "std" alloc = ["rand_core/alloc"] @@ -65,7 +65,7 @@ members = [ ] [dependencies] -rand_core = { path = "rand_core", version = "0.7.0" } +rand_core = { path = "rand_core", version = "0.7.0", default-features = false } log = { version = "0.4.4", optional = true } serde = { version = "1.0.103", features = ["derive"], optional = true } rand_chacha = { path = "rand_chacha", version = "0.4.0", default-features = false, optional = true } diff --git a/rand_core/Cargo.toml b/rand_core/Cargo.toml index 37c2a563a3..8c9d902a70 100644 --- a/rand_core/Cargo.toml +++ b/rand_core/Cargo.toml @@ -25,7 +25,7 @@ rustdoc-args = ["--cfg", "doc_cfg", "--generate-link-to-definition"] all-features = true [features] -std = ["alloc", "getrandom", "getrandom/std"] # use std library; should be default but for above bug +std = ["alloc", "getrandom?/std"] alloc = [] # enables Vec and Box support without std serde1 = ["serde"] # enables serde for BlockRng wrapper diff --git a/rand_core/src/error.rs b/rand_core/src/error.rs index 411896f2c4..1a5092fe82 100644 --- a/rand_core/src/error.rs +++ b/rand_core/src/error.rs @@ -50,9 +50,7 @@ impl Error { #[cfg_attr(doc_cfg, doc(cfg(feature = "std")))] #[inline] pub fn new(err: E) -> Self - where - E: Into>, - { + where E: Into> { Error { inner: err.into() } } @@ -125,7 +123,7 @@ impl fmt::Debug for Error { { getrandom::Error::from(self.code).fmt(f) } - #[cfg(not(feature = "getrandom"))] + #[cfg(not(any(feature = "getrandom", feature = "std")))] { write!(f, "Error {{ code: {} }}", self.code) } @@ -142,7 +140,7 @@ impl fmt::Display for Error { { getrandom::Error::from(self.code).fmt(f) } - #[cfg(not(feature = "getrandom"))] + #[cfg(not(any(feature = "getrandom", feature = "std")))] { write!(f, "error code {}", self.code) } diff --git a/rand_distr/Cargo.toml b/rand_distr/Cargo.toml index a5907eb4fd..9d3baee0a8 100644 --- a/rand_distr/Cargo.toml +++ b/rand_distr/Cargo.toml @@ -35,7 +35,7 @@ serde_with = { version = "1.14.0", optional = true } [dev-dependencies] rand_pcg = { version = "0.4.0", path = "../rand_pcg" } # For inline examples -rand = { path = "..", version = "0.9.0", default-features = false, features = ["std_rng", "std", "small_rng"] } +rand = { path = "..", version = "0.9.0", features = ["small_rng"] } # Histogram implementation for testing uniformity average = { version = "0.13", features = [ "std" ] } # Special functions for testing distributions diff --git a/src/lib.rs b/src/lib.rs index 755b5ba6e9..69b6293b8b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -101,11 +101,11 @@ pub mod rngs; pub mod seq; // Public exports -#[cfg(all(feature = "std", feature = "std_rng"))] +#[cfg(all(feature = "std", feature = "std_rng", feature = "getrandom"))] pub use crate::rngs::thread::thread_rng; pub use rng::{Fill, Rng}; -#[cfg(all(feature = "std", feature = "std_rng"))] +#[cfg(all(feature = "std", feature = "std_rng", feature = "getrandom"))] use crate::distributions::{Distribution, Standard}; /// Generates a random value using the thread-local random number generator. @@ -152,8 +152,8 @@ use crate::distributions::{Distribution, Standard}; /// /// [`Standard`]: distributions::Standard /// [`ThreadRng`]: rngs::ThreadRng -#[cfg(all(feature = "std", feature = "std_rng"))] -#[cfg_attr(doc_cfg, doc(cfg(all(feature = "std", feature = "std_rng"))))] +#[cfg(all(feature = "std", feature = "std_rng", feature = "getrandom"))] +#[cfg_attr(doc_cfg, doc(cfg(all(feature = "std", feature = "std_rng", feature = "getrandom"))))] #[inline] pub fn random() -> T where Standard: Distribution { @@ -173,7 +173,7 @@ mod test { } #[test] - #[cfg(all(feature = "std", feature = "std_rng"))] + #[cfg(all(feature = "std", feature = "std_rng", feature = "getrandom"))] fn test_random() { let _n: usize = random(); let _f: f32 = random(); diff --git a/src/prelude.rs b/src/prelude.rs index 51c457e3f9..1ce747b625 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -25,10 +25,10 @@ pub use crate::rngs::SmallRng; #[cfg(feature = "std_rng")] #[doc(no_inline)] pub use crate::rngs::StdRng; #[doc(no_inline)] -#[cfg(all(feature = "std", feature = "std_rng"))] +#[cfg(all(feature = "std", feature = "std_rng", feature = "getrandom"))] pub use crate::rngs::ThreadRng; #[doc(no_inline)] pub use crate::seq::{IteratorRandom, SliceRandom}; #[doc(no_inline)] -#[cfg(all(feature = "std", feature = "std_rng"))] +#[cfg(all(feature = "std", feature = "std_rng", feature = "getrandom"))] pub use crate::{random, thread_rng}; #[doc(no_inline)] pub use crate::{CryptoRng, Rng, RngCore, SeedableRng}; diff --git a/src/rngs/mod.rs b/src/rngs/mod.rs index ac3c2c595d..9013c57d10 100644 --- a/src/rngs/mod.rs +++ b/src/rngs/mod.rs @@ -109,11 +109,11 @@ mod xoshiro128plusplus; #[cfg(feature = "small_rng")] mod small; #[cfg(feature = "std_rng")] mod std; -#[cfg(all(feature = "std", feature = "std_rng"))] pub(crate) mod thread; +#[cfg(all(feature = "std", feature = "std_rng", feature = "getrandom"))] pub(crate) mod thread; #[cfg(feature = "small_rng")] pub use self::small::SmallRng; #[cfg(feature = "std_rng")] pub use self::std::StdRng; -#[cfg(all(feature = "std", feature = "std_rng"))] pub use self::thread::ThreadRng; +#[cfg(all(feature = "std", feature = "std_rng", feature = "getrandom"))] pub use self::thread::ThreadRng; #[cfg_attr(doc_cfg, doc(cfg(feature = "getrandom")))] #[cfg(feature = "getrandom")] pub use rand_core::OsRng; diff --git a/src/rngs/std.rs b/src/rngs/std.rs index cdae8fab01..ddc3014dfe 100644 --- a/src/rngs/std.rs +++ b/src/rngs/std.rs @@ -10,6 +10,7 @@ use crate::{CryptoRng, Error, RngCore, SeedableRng}; +#[cfg(feature = "getrandom")] pub(crate) use rand_chacha::ChaCha12Core as Core; use rand_chacha::ChaCha12Rng as Rng; diff --git a/src/rngs/thread.rs b/src/rngs/thread.rs index 78cecde575..6c8d83c02e 100644 --- a/src/rngs/thread.rs +++ b/src/rngs/thread.rs @@ -63,7 +63,7 @@ const THREAD_RNG_RESEED_THRESHOLD: u64 = 1024 * 64; /// /// [`ReseedingRng`]: crate::rngs::adapter::ReseedingRng /// [`StdRng`]: crate::rngs::StdRng -#[cfg_attr(doc_cfg, doc(cfg(all(feature = "std", feature = "std_rng"))))] +#[cfg_attr(doc_cfg, doc(cfg(all(feature = "std", feature = "std_rng", feature = "getrandom"))))] #[derive(Clone)] pub struct ThreadRng { // Rc is explicitly !Send and !Sync @@ -107,7 +107,7 @@ thread_local!( /// println!("A simulated die roll: {}", rng.gen_range(1..=6)); /// # } /// ``` -#[cfg_attr(doc_cfg, doc(cfg(all(feature = "std", feature = "std_rng"))))] +#[cfg_attr(doc_cfg, doc(cfg(all(feature = "std", feature = "std_rng", feature = "getrandom"))))] pub fn thread_rng() -> ThreadRng { let rng = THREAD_RNG_KEY.with(|t| t.clone()); ThreadRng { rng }