Support using std without getrandom or rand_chacha (#1354)
Support using std without getrandom or rand_chacha Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
This commit is contained in:
parent
870c6791bb
commit
ef89cbefaf
@ -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 }
|
||||
|
@ -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
|
||||
|
||||
|
@ -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() }
|
||||
}
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
@ -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
|
||||
|
10
src/lib.rs
10
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>() -> T
|
||||
where Standard: Distribution<T> {
|
||||
@ -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();
|
||||
|
@ -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};
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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 }
|
||||
|
Loading…
x
Reference in New Issue
Block a user