Skip to content

Commit b7abfb6

Browse files
committed
Feature-gate the SmallRng generator
1 parent 1ea01ae commit b7abfb6

File tree

7 files changed

+31
-20
lines changed

7 files changed

+31
-20
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ getrandom = ["getrandom_package", "rand_core/getrandom"]
3535

3636
# Configuration:
3737
simd_support = ["packed_simd"] # enables SIMD support
38+
small_rng = ["rand_pcg"] # enables SmallRng
3839

3940
[workspace]
4041
members = [
@@ -53,8 +54,8 @@ members = [
5354

5455
[dependencies]
5556
rand_core = { path = "rand_core", version = "0.4" }
56-
rand_pcg = { path = "rand_pcg", version = "0.1" }
5757
rand_hc = { path = "rand_hc", version = "0.1" }
58+
rand_pcg = { path = "rand_pcg", version = "0.1", optional = true }
5859
# Do not depend on 'getrandom_package' directly; use the 'getrandom' feature!
5960
getrandom_package = { version = "0.1.1", package = "getrandom", optional = true }
6061
log = { version = "0.4", optional = true }

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ Optionally, the following dependencies can be enabled:
102102

103103
Additionally, these features configure Rand:
104104

105+
- `small_rng` enables inclusion of the `SmallRng` PRNG
105106
- `nightly` enables all experimental features
106107
- `simd_support` (experimental) enables sampling of SIMD values
107108
(uniformly random SIMD integers and floats)

src/distributions/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ impl<D, R, T> iter::TrustedLen for DistIter<D, R, T>
324324
/// use rand::prelude::*;
325325
/// use rand::distributions::Standard;
326326
///
327-
/// let val: f32 = SmallRng::from_entropy().sample(Standard);
327+
/// let val: f32 = StdRng::from_entropy().sample(Standard);
328328
/// println!("f32 from [0, 1): {}", val);
329329
/// ```
330330
///

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ extern crate getrandom_package as getrandom;
6363

6464
extern crate rand_core;
6565
extern crate rand_hc;
66-
extern crate rand_pcg;
66+
#[cfg(feature="small_rng")] extern crate rand_pcg;
6767

6868
#[cfg(feature = "log")] #[macro_use] extern crate log;
6969
#[allow(unused)]

src/prelude.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@
1414
//!
1515
//! ```
1616
//! use rand::prelude::*;
17-
//! # let _ = StdRng::from_entropy();
18-
//! # let mut r = SmallRng::from_rng(thread_rng()).unwrap();
17+
//! # let mut r = StdRng::from_rng(thread_rng()).unwrap();
1918
//! # let _: f32 = r.gen();
2019
//! ```
2120
2221
#[doc(no_inline)] pub use distributions::Distribution;
23-
#[doc(no_inline)] pub use rngs::{SmallRng, StdRng};
22+
#[doc(no_inline)] pub use rngs::StdRng;
23+
#[cfg(feature="small_rng")]
24+
#[doc(no_inline)] pub use rngs::SmallRng;
2425
#[doc(no_inline)] #[cfg(feature="std")] pub use rngs::ThreadRng;
2526
#[doc(no_inline)] pub use {Rng, RngCore, CryptoRng, SeedableRng};
2627
#[doc(no_inline)] #[cfg(feature="std")] pub use {random, thread_rng};

src/rngs/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,15 @@ pub mod adapter;
104104
#[cfg(feature="std")] mod entropy;
105105
pub mod mock; // Public so we don't export `StepRng` directly, making it a bit
106106
// more clear it is intended for testing.
107+
#[cfg(feature="small_rng")]
107108
mod small;
108109
mod std;
109110
#[cfg(feature="std")] pub(crate) mod thread;
110111

111112
#[allow(deprecated)]
112113
#[cfg(feature="std")] pub use self::entropy::EntropyRng;
113114

115+
#[cfg(feature="small_rng")]
114116
pub use self::small::SmallRng;
115117
pub use self::std::StdRng;
116118
#[cfg(feature="std")] pub use self::thread::ThreadRng;

src/rngs/small.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,28 @@ type Rng = ::rand_pcg::Pcg64Mcg;
1515
#[cfg(not(all(all(rustc_1_26, not(target_os = "emscripten")), target_pointer_width = "64")))]
1616
type Rng = ::rand_pcg::Pcg32;
1717

18-
/// An RNG recommended when small state, cheap initialization and good
19-
/// performance are required. The PRNG algorithm in `SmallRng` is chosen to be
20-
/// efficient on the current platform, **without consideration for cryptography
21-
/// or security**. The size of its state is much smaller than for [`StdRng`].
18+
/// A small-state, fast non-crypto PRNG
2219
///
23-
/// Reproducibility of output from this generator is however not required, thus
24-
/// future library versions may use a different internal generator with
25-
/// different output. Further, this generator may not be portable and can
26-
/// produce different output depending on the architecture. If you require
27-
/// reproducible output, use a named RNG.
28-
/// Refer to [The Book](https://rust-random.github.io/book/guide-rngs.html).
29-
///
20+
/// `SmallRng` may be a good choice when a PRNG with small state, cheap
21+
/// initialization, good statistical quality and good performance are required.
22+
/// It is **not** a good choice when security against prediction or
23+
/// reproducibility are important.
3024
///
31-
/// The current algorithm is [`Pcg64Mcg`][rand_pcg::Pcg64Mcg] on 64-bit platforms with Rust version
32-
/// 1.26 and later, or [`Pcg32`][rand_pcg::Pcg32] otherwise. Both are found in
33-
/// the [rand_pcg] crate.
25+
/// This PRNG is **feature-gated**: to use, you must enable the crate feature
26+
/// `small_rng`.
27+
///
28+
/// The algorithm is deterministic but should not be considered reproducible
29+
/// due to dependence on platform and possible replacement in future
30+
/// library versions. For a reproducible generator, use a named PRNG from an
31+
/// external crate, e.g. [rand_pcg] or [rand_chacha].
32+
/// Refer also to [The Book](https://rust-random.github.io/book/guide-rngs.html).
33+
///
34+
/// The PRNG algorithm in `SmallRng` is chosen to be
35+
/// efficient on the current platform, without consideration for cryptography
36+
/// or security. The size of its state is much smaller than [`StdRng`].
37+
/// The current algorithm is [`Pcg64Mcg`][rand_pcg::Pcg64Mcg] on 64-bit
38+
/// platforms and [`Pcg32`][rand_pcg::Pcg32] on 32-bit platforms. Both are
39+
/// implemented by the [rand_pcg] crate.
3440
///
3541
/// # Examples
3642
///

0 commit comments

Comments
 (0)