Skip to content

Commit 908c584

Browse files
committed
Move OsRng definition to rand::rngs
Also removes std, os_rng features from rand_core
1 parent 895c2c9 commit 908c584

File tree

7 files changed

+17
-21
lines changed

7 files changed

+17
-21
lines changed

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ serde = ["dep:serde", "rand_core/serde"]
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", "alloc"]
37+
std = ["alloc", "getrandom?/std"]
3838

3939
# Option: "alloc" enables support for Vec and Box when not using "std"
4040
alloc = []
4141

4242
# Option: enable OsRng
43-
os_rng = ["rand_core/os_rng"]
43+
os_rng = ["dep:getrandom"]
4444

4545
# Option (requires nightly Rust): experimental SIMD support
4646
simd_support = []
@@ -83,6 +83,7 @@ rand_core = { path = "rand_core", version = "0.9.0", default-features = false }
8383
log = { version = "0.4.4", optional = true }
8484
serde = { version = "1.0.103", features = ["derive"], optional = true }
8585
chacha20 = { version = "=0.10.0-rc.2", default-features = false, features = ["rng"], optional = true }
86+
getrandom = { version = "0.3.0", optional = true }
8687

8788
[dev-dependencies]
8889
rand_pcg = { path = "rand_pcg", version = "0.9.0" }

rand_chacha/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ rand = { path = "..", version = "0.10.0-rc.0" }
3232

3333
[features]
3434
default = ["std"]
35-
std = ["ppv-lite86/std", "rand_core/std"]
35+
std = ["ppv-lite86/std"]
3636
serde = ["dep:serde"]

rand_core/Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ rustdoc-args = ["--generate-link-to-definition"]
2525
all-features = true
2626

2727
[features]
28-
std = ["getrandom?/std"]
29-
os_rng = ["dep:getrandom"]
3028
serde = ["dep:serde"] # enables serde for BlockRng wrapper
3129

3230
[dependencies]
3331
serde = { version = "1", features = ["derive"], optional = true }
34-
getrandom = { version = "0.3.0", optional = true }

rand_core/src/lib.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,10 @@
3636
#![cfg_attr(docsrs, feature(doc_cfg))]
3737
#![no_std]
3838

39-
#[cfg(feature = "std")]
40-
extern crate std;
41-
4239
use core::{fmt, ops::DerefMut};
4340

4441
pub mod block;
4542
pub mod le;
46-
#[cfg(feature = "os_rng")]
47-
mod os;
48-
49-
#[cfg(feature = "os_rng")]
50-
pub use os::{OsError, OsRng};
5143

5244
/// Implementation-level interface for RNGs
5345
///
@@ -168,6 +160,8 @@ where
168160
/// An optional property of CSPRNGs is backtracking resistance: if the CSPRNG's
169161
/// state is revealed, it will not be computationally-feasible to reconstruct
170162
/// prior output values. This property is not required by `CryptoRng`.
163+
///
164+
/// [`OsRng`]: https://docs.rs/rand/latest/rand/rngs/struct.OsRng.html
171165
pub trait CryptoRng: RngCore {}
172166

173167
impl<T: DerefMut> CryptoRng for T where T::Target: CryptoRng {}
@@ -184,6 +178,8 @@ impl<T: DerefMut> CryptoRng for T where T::Target: CryptoRng {}
184178
/// An implementation of this trait may be made compatible with code requiring
185179
/// an [`RngCore`] through [`TryRngCore::unwrap_err`]. The resulting RNG will
186180
/// panic in case the underlying fallible RNG yields an error.
181+
///
182+
/// [`OsRng`]: https://docs.rs/rand/latest/rand/rngs/struct.OsRng.html
187183
pub trait TryRngCore {
188184
/// The type returned in the event of a RNG error.
189185
type Error: fmt::Debug + fmt::Display;
@@ -246,6 +242,8 @@ impl<R: RngCore + ?Sized> TryRngCore for R {
246242
/// `default()` instances are themselves secure generators: for example if the
247243
/// implementing type is a stateless interface over a secure external generator
248244
/// (like [`OsRng`]) or if the `default()` instance uses a strong, fresh seed.
245+
///
246+
/// [`OsRng`]: https://docs.rs/rand/latest/rand/rngs/struct.OsRng.html
249247
pub trait TryCryptoRng: TryRngCore {}
250248

251249
impl<R: CryptoRng + ?Sized> TryCryptoRng for R {}

src/rngs/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ mod std;
110110
#[cfg(feature = "thread_rng")]
111111
pub(crate) mod thread;
112112

113+
#[cfg(feature = "os_rng")]
114+
mod os;
115+
113116
#[cfg(feature = "small_rng")]
114117
pub use self::small::SmallRng;
115118
#[cfg(feature = "small_rng")]
@@ -126,4 +129,4 @@ pub use self::thread::ThreadRng;
126129
pub use chacha20::{ChaCha8Rng, ChaCha12Rng, ChaCha20Rng};
127130

128131
#[cfg(feature = "os_rng")]
129-
pub use rand_core::OsRng;
132+
pub use os::{OsError, OsRng};

rand_core/src/os.rs renamed to src/rngs/os.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use crate::{TryCryptoRng, TryRngCore};
3535
///
3636
/// # Usage example
3737
/// ```
38-
/// use rand_core::{TryRngCore, OsRng};
38+
/// use rand::{TryRngCore, rngs::OsRng};
3939
///
4040
/// let mut key = [0u8; 16];
4141
/// OsRng.try_fill_bytes(&mut key).unwrap();

src/rngs/thread.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,9 @@ use std::fmt;
1313
use std::rc::Rc;
1414
use std::thread_local;
1515

16+
use super::{OsError, OsRng, ReseedingRng, std::Core};
1617
use rand_core::{CryptoRng, RngCore};
1718

18-
use super::std::Core;
19-
use crate::rngs::OsRng;
20-
use crate::rngs::ReseedingRng;
21-
2219
// Rationale for using `UnsafeCell` in `ThreadRng`:
2320
//
2421
// Previously we used a `RefCell`, with an overhead of ~15%. There will only
@@ -100,7 +97,7 @@ impl ThreadRng {
10097
/// Immediately reseed the generator
10198
///
10299
/// This discards any remaining random data in the cache.
103-
pub fn reseed(&mut self) -> Result<(), rand_core::OsError> {
100+
pub fn reseed(&mut self) -> Result<(), OsError> {
104101
// SAFETY: We must make sure to stop using `rng` before anyone else
105102
// creates another mutable reference
106103
let rng = unsafe { &mut *self.rng.get() };

0 commit comments

Comments
 (0)