Skip to content

Commit ef89cbe

Browse files
authored
Support using std without getrandom or rand_chacha (#1354)
Support using std without getrandom or rand_chacha Signed-off-by: Alex Saveau <[email protected]>
1 parent 870c679 commit ef89cbe

File tree

9 files changed

+20
-21
lines changed

9 files changed

+20
-21
lines changed

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ features = ["small_rng", "serde1"]
2828

2929
[features]
3030
# Meta-features:
31-
default = ["std", "std_rng"]
31+
default = ["std", "std_rng", "getrandom"]
3232
nightly = [] # some additions requiring nightly Rust
3333
serde1 = ["serde", "rand_core/serde1"]
3434

3535
# Option (enabled by default): without "std" rand uses libcore; this option
3636
# enables functionality expected to be available on a standard platform.
37-
std = ["rand_core/std", "rand_chacha/std", "alloc", "getrandom", "libc"]
37+
std = ["rand_core/std", "rand_chacha?/std", "alloc", "libc"]
3838

3939
# Option: "alloc" enables support for Vec and Box when not using "std"
4040
alloc = ["rand_core/alloc"]
@@ -65,7 +65,7 @@ members = [
6565
]
6666

6767
[dependencies]
68-
rand_core = { path = "rand_core", version = "0.7.0" }
68+
rand_core = { path = "rand_core", version = "0.7.0", default-features = false }
6969
log = { version = "0.4.4", optional = true }
7070
serde = { version = "1.0.103", features = ["derive"], optional = true }
7171
rand_chacha = { path = "rand_chacha", version = "0.4.0", default-features = false, optional = true }

rand_core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ rustdoc-args = ["--cfg", "doc_cfg", "--generate-link-to-definition"]
2525
all-features = true
2626

2727
[features]
28-
std = ["alloc", "getrandom", "getrandom/std"] # use std library; should be default but for above bug
28+
std = ["alloc", "getrandom?/std"]
2929
alloc = [] # enables Vec and Box support without std
3030
serde1 = ["serde"] # enables serde for BlockRng wrapper
3131

rand_core/src/error.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ impl Error {
5050
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
5151
#[inline]
5252
pub fn new<E>(err: E) -> Self
53-
where
54-
E: Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
55-
{
53+
where E: Into<Box<dyn std::error::Error + Send + Sync + 'static>> {
5654
Error { inner: err.into() }
5755
}
5856

@@ -125,7 +123,7 @@ impl fmt::Debug for Error {
125123
{
126124
getrandom::Error::from(self.code).fmt(f)
127125
}
128-
#[cfg(not(feature = "getrandom"))]
126+
#[cfg(not(any(feature = "getrandom", feature = "std")))]
129127
{
130128
write!(f, "Error {{ code: {} }}", self.code)
131129
}
@@ -142,7 +140,7 @@ impl fmt::Display for Error {
142140
{
143141
getrandom::Error::from(self.code).fmt(f)
144142
}
145-
#[cfg(not(feature = "getrandom"))]
143+
#[cfg(not(any(feature = "getrandom", feature = "std")))]
146144
{
147145
write!(f, "error code {}", self.code)
148146
}

rand_distr/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ serde_with = { version = "1.14.0", optional = true }
3535
[dev-dependencies]
3636
rand_pcg = { version = "0.4.0", path = "../rand_pcg" }
3737
# For inline examples
38-
rand = { path = "..", version = "0.9.0", default-features = false, features = ["std_rng", "std", "small_rng"] }
38+
rand = { path = "..", version = "0.9.0", features = ["small_rng"] }
3939
# Histogram implementation for testing uniformity
4040
average = { version = "0.13", features = [ "std" ] }
4141
# Special functions for testing distributions

src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,11 @@ pub mod rngs;
101101
pub mod seq;
102102

103103
// Public exports
104-
#[cfg(all(feature = "std", feature = "std_rng"))]
104+
#[cfg(all(feature = "std", feature = "std_rng", feature = "getrandom"))]
105105
pub use crate::rngs::thread::thread_rng;
106106
pub use rng::{Fill, Rng};
107107

108-
#[cfg(all(feature = "std", feature = "std_rng"))]
108+
#[cfg(all(feature = "std", feature = "std_rng", feature = "getrandom"))]
109109
use crate::distributions::{Distribution, Standard};
110110

111111
/// Generates a random value using the thread-local random number generator.
@@ -152,8 +152,8 @@ use crate::distributions::{Distribution, Standard};
152152
///
153153
/// [`Standard`]: distributions::Standard
154154
/// [`ThreadRng`]: rngs::ThreadRng
155-
#[cfg(all(feature = "std", feature = "std_rng"))]
156-
#[cfg_attr(doc_cfg, doc(cfg(all(feature = "std", feature = "std_rng"))))]
155+
#[cfg(all(feature = "std", feature = "std_rng", feature = "getrandom"))]
156+
#[cfg_attr(doc_cfg, doc(cfg(all(feature = "std", feature = "std_rng", feature = "getrandom"))))]
157157
#[inline]
158158
pub fn random<T>() -> T
159159
where Standard: Distribution<T> {
@@ -173,7 +173,7 @@ mod test {
173173
}
174174

175175
#[test]
176-
#[cfg(all(feature = "std", feature = "std_rng"))]
176+
#[cfg(all(feature = "std", feature = "std_rng", feature = "getrandom"))]
177177
fn test_random() {
178178
let _n: usize = random();
179179
let _f: f32 = random();

src/prelude.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ pub use crate::rngs::SmallRng;
2525
#[cfg(feature = "std_rng")]
2626
#[doc(no_inline)] pub use crate::rngs::StdRng;
2727
#[doc(no_inline)]
28-
#[cfg(all(feature = "std", feature = "std_rng"))]
28+
#[cfg(all(feature = "std", feature = "std_rng", feature = "getrandom"))]
2929
pub use crate::rngs::ThreadRng;
3030
#[doc(no_inline)] pub use crate::seq::{IteratorRandom, SliceRandom};
3131
#[doc(no_inline)]
32-
#[cfg(all(feature = "std", feature = "std_rng"))]
32+
#[cfg(all(feature = "std", feature = "std_rng", feature = "getrandom"))]
3333
pub use crate::{random, thread_rng};
3434
#[doc(no_inline)] pub use crate::{CryptoRng, Rng, RngCore, SeedableRng};

src/rngs/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,11 @@ mod xoshiro128plusplus;
109109
#[cfg(feature = "small_rng")] mod small;
110110

111111
#[cfg(feature = "std_rng")] mod std;
112-
#[cfg(all(feature = "std", feature = "std_rng"))] pub(crate) mod thread;
112+
#[cfg(all(feature = "std", feature = "std_rng", feature = "getrandom"))] pub(crate) mod thread;
113113

114114
#[cfg(feature = "small_rng")] pub use self::small::SmallRng;
115115
#[cfg(feature = "std_rng")] pub use self::std::StdRng;
116-
#[cfg(all(feature = "std", feature = "std_rng"))] pub use self::thread::ThreadRng;
116+
#[cfg(all(feature = "std", feature = "std_rng", feature = "getrandom"))] pub use self::thread::ThreadRng;
117117

118118
#[cfg_attr(doc_cfg, doc(cfg(feature = "getrandom")))]
119119
#[cfg(feature = "getrandom")] pub use rand_core::OsRng;

src/rngs/std.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
1111
use crate::{CryptoRng, Error, RngCore, SeedableRng};
1212

13+
#[cfg(feature = "getrandom")]
1314
pub(crate) use rand_chacha::ChaCha12Core as Core;
1415

1516
use rand_chacha::ChaCha12Rng as Rng;

src/rngs/thread.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ const THREAD_RNG_RESEED_THRESHOLD: u64 = 1024 * 64;
6363
///
6464
/// [`ReseedingRng`]: crate::rngs::adapter::ReseedingRng
6565
/// [`StdRng`]: crate::rngs::StdRng
66-
#[cfg_attr(doc_cfg, doc(cfg(all(feature = "std", feature = "std_rng"))))]
66+
#[cfg_attr(doc_cfg, doc(cfg(all(feature = "std", feature = "std_rng", feature = "getrandom"))))]
6767
#[derive(Clone)]
6868
pub struct ThreadRng {
6969
// Rc is explicitly !Send and !Sync
@@ -107,7 +107,7 @@ thread_local!(
107107
/// println!("A simulated die roll: {}", rng.gen_range(1..=6));
108108
/// # }
109109
/// ```
110-
#[cfg_attr(doc_cfg, doc(cfg(all(feature = "std", feature = "std_rng"))))]
110+
#[cfg_attr(doc_cfg, doc(cfg(all(feature = "std", feature = "std_rng", feature = "getrandom"))))]
111111
pub fn thread_rng() -> ThreadRng {
112112
let rng = THREAD_RNG_KEY.with(|t| t.clone());
113113
ThreadRng { rng }

0 commit comments

Comments
 (0)