Rand: add std_rng feature flag

This commit is contained in:
Diggory Hardy 2020-03-09 15:22:13 +00:00
parent 0aa461715b
commit abcd587bd8
7 changed files with 40 additions and 28 deletions

View File

@ -22,18 +22,28 @@ appveyor = { repository = "rust-random/rand" }
[features]
# Meta-features:
default = ["std"] # without "std" rand uses libcore
default = ["std", "std_rng"]
nightly = ["simd_support"] # enables all features requiring nightly rust
serde1 = [] # does nothing, deprecated
# Optional dependencies:
# Option: 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"]
alloc = ["rand_core/alloc"] # enables Vec and Box support (without std)
# Option: "alloc" enables support for Vec and Box when not using "std"
alloc = ["rand_core/alloc"]
# Option: use getrandom package for seeding
getrandom = ["rand_core/getrandom"]
# Configuration:
simd_support = ["packed_simd"] # enables SIMD support
small_rng = ["rand_pcg"] # enables SmallRng
# Option: experimental SIMD support
simd_support = ["packed_simd"]
# Option: enable StdRng (enabled by default)
std_rng = ["rand_chacha", "rand_hc"]
# Option: enable SmallRng
small_rng = ["rand_pcg"]
[workspace]
members = [
@ -63,9 +73,9 @@ libc = { version = "0.2.22", optional = true, 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" }

View File

@ -90,19 +90,17 @@ Rand release if required, but the change must be noted in the changelog.
Rand is built with these features enabled by default:
- `std` enables functionality dependent on the `std` lib
- `alloc` (implied by `std`) enables functionality requiring an allocator (when using this feature in `no_std`, Rand requires Rustc version 1.36 or greater)
- `alloc` (implied by `std`) enables functionality requiring an allocator
(when using this feature in `no_std`, Rand requires Rustc version 1.36 or
greater)
- `getrandom` (implied by `std`) is an optional dependency providing the code
behind `rngs::OsRng`
- `std_rng` enables inclusion of `StdRng`, `thread_rng` and `random`
(the latter two *also* require that `std` be enabled)
Optionally, the following dependencies can be enabled:
- `log` enables logging via the `log` crate
- `stdweb` implies `getrandom/stdweb` to enable
`getrandom` support on `wasm32-unknown-unknown`
(will be removed in rand 0.8; activate via `getrandom` crate instead)
- `wasm-bindgen` implies `getrandom/wasm-bindgen` to enable
`getrandom` support on `wasm32-unknown-unknown`
(will be removed in rand 0.8; activate via `getrandom` crate instead)
- `log` enables logging via the `log` crate` crate
Additionally, these features configure Rand:

View File

@ -100,10 +100,12 @@ pub mod rngs;
pub mod seq;
// Public exports
#[cfg(feature = "std")] pub use crate::rngs::thread::thread_rng;
#[cfg(all(feature = "std", feature = "std_rng"))]
pub use crate::rngs::thread::thread_rng;
pub use rng::{Fill, Rng};
#[cfg(feature = "std")] use crate::distributions::{Distribution, Standard};
#[cfg(all(feature = "std", feature = "std_rng"))]
use crate::distributions::{Distribution, Standard};
/// Generates a random value using the thread-local random number generator.
///
@ -147,7 +149,7 @@ pub use rng::{Fill, Rng};
/// ```
///
/// [`Standard`]: distributions::Standard
#[cfg(feature = "std")]
#[cfg(all(feature = "std", feature = "std_rng"))]
#[inline]
pub fn random<T>() -> T
where Standard: Distribution<T> {
@ -167,7 +169,7 @@ mod test {
}
#[test]
#[cfg(feature = "std")]
#[cfg(all(feature = "std", feature = "std_rng"))]
fn test_random() {
// not sure how to test this aside from just getting some values
let _n: usize = random();

View File

@ -22,12 +22,13 @@
#[cfg(feature = "small_rng")]
#[doc(no_inline)]
pub use crate::rngs::SmallRng;
#[cfg(feature = "std_rng")]
#[doc(no_inline)] pub use crate::rngs::StdRng;
#[doc(no_inline)]
#[cfg(feature = "std")]
#[cfg(all(feature = "std", feature = "std_rng"))]
pub use crate::rngs::ThreadRng;
#[doc(no_inline)] pub use crate::seq::{IteratorRandom, SliceRandom};
#[doc(no_inline)]
#[cfg(feature = "std")]
#[cfg(all(feature = "std", feature = "std_rng"))]
pub use crate::{random, thread_rng};
#[doc(no_inline)] pub use crate::{CryptoRng, Rng, RngCore, SeedableRng};

View File

@ -325,6 +325,7 @@ mod fork {
}
#[cfg(feature = "std_rng")]
#[cfg(test)]
mod test {
use super::ReseedingRng;

View File

@ -102,15 +102,15 @@ pub mod adapter;
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;
mod std;
#[cfg(feature = "std")] pub(crate) mod thread;
#[cfg(feature = "std_rng")] mod std;
#[cfg(all(feature = "std", feature = "std_rng"))] pub(crate) mod thread;
#[allow(deprecated)]
#[cfg(feature = "std")]
pub use self::entropy::EntropyRng;
#[cfg(feature = "small_rng")] pub use self::small::SmallRng;
pub use self::std::StdRng;
#[cfg(feature = "std")] pub use self::thread::ThreadRng;
#[cfg(feature = "std_rng")] pub use self::std::StdRng;
#[cfg(all(feature = "std", feature = "std_rng"))] pub use self::thread::ThreadRng;
#[cfg(feature = "getrandom")] pub use rand_core::OsRng;

View File

@ -8,8 +8,8 @@
//! Thread-local random number generator
use std::cell::UnsafeCell;
use std::ptr::NonNull;
use core::cell::UnsafeCell;
use core::ptr::NonNull;
use super::std::Core;
use crate::rngs::adapter::ReseedingRng;