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

Support using std without getrandom or rand_chacha #1354

Merged
merged 2 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion rand_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 3 additions & 5 deletions rand_core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ impl Error {
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
#[inline]
pub fn new<E>(err: E) -> Self
where
E: Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
{
where E: Into<Box<dyn std::error::Error + Send + Sync + 'static>> {
Error { inner: err.into() }
}

Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion rand_distr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
SUPERCILEX marked this conversation as resolved.
Show resolved Hide resolved

/// Generates a random value using the thread-local random number generator.
Expand Down Expand Up @@ -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>() -> T
where Standard: Distribution<T> {
Expand All @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
4 changes: 2 additions & 2 deletions src/rngs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
1 change: 1 addition & 0 deletions src/rngs/std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use crate::{CryptoRng, Error, RngCore, SeedableRng};

#[cfg(feature = "getrandom")]
pub(crate) use rand_chacha::ChaCha12Core as Core;
SUPERCILEX marked this conversation as resolved.
Show resolved Hide resolved

use rand_chacha::ChaCha12Rng as Rng;
Expand Down
4 changes: 2 additions & 2 deletions src/rngs/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }
Expand Down