Skip to content

Commit 5734b55

Browse files
committed
review update
1 parent 5a47df0 commit 5734b55

File tree

6 files changed

+21
-13
lines changed

6 files changed

+21
-13
lines changed

rand_core/src/blanket_impls.rs

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ impl<'a, R: TryRngCore + ?Sized> TryRngCore for &'a mut R {
4343
impl<'a, R: TryCryptoRng + ?Sized> TryCryptoRng for &'a mut R {}
4444

4545
#[cfg(feature = "alloc")]
46+
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
4647
impl<R: RngCore + ?Sized> RngCore for Box<R> {
4748
#[inline(always)]
4849
fn next_u32(&mut self) -> u32 {

rand_core/src/impls.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -173,24 +173,24 @@ macro_rules! impl_try_rng_from_rng_core {
173173

174174
#[inline]
175175
fn try_next_u32(&mut self) -> Result<u32, Self::Error> {
176-
Ok(self.next_u32())
176+
Ok($crate::RngCore::next_u32(self))
177177
}
178178

179179
#[inline]
180180
fn try_next_u64(&mut self) -> Result<u64, Self::Error> {
181-
Ok(self.next_u64())
181+
Ok($crate::RngCore::next_u64(self))
182182
}
183183

184184
#[inline]
185185
fn try_fill_bytes(&mut self, dst: &mut [u8]) -> Result<(), Self::Error> {
186-
self.fill_bytes(dst);
186+
$crate::RngCore::fill_bytes(self, dst);
187187
Ok(())
188188
}
189189
}
190190
};
191191
}
192192

193-
/// Implement [`TryCryptoRng`] for a type implementing [`CryptoRng`].
193+
/// Implement [`TryCryptoRng`] and [`TryRngCore`] for a type implementing [`CryptoRng`].
194194
///
195195
/// Ideally, `rand_core` would define blanket impls for this, but they conflict with blanket impls
196196
/// for `&mut R` and `Box<R>`, so until specialziation is stabilized, implementer crates
@@ -199,7 +199,14 @@ macro_rules! impl_try_rng_from_rng_core {
199199
macro_rules! impl_try_crypto_rng_from_crypto_rng {
200200
($t:ty) => {
201201
$crate::impl_try_rng_from_rng_core!($t);
202+
202203
impl $crate::TryCryptoRng for $t {}
204+
205+
/// Check at compile time that `$t` implements `CryptoRng`
206+
const _: () = {
207+
const fn check_crypto_rng_impl<T: $crate::CryptoRng>() {}
208+
check_crypto_rng_impl::<$t>();
209+
};
203210
};
204211
}
205212

rand_core/src/lib.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ pub trait SeedableRng: Sized {
400400
/// since it is convenient and secure.
401401
///
402402
/// Note that this method may panic on (extremely unlikely) [`getrandom`] errors.
403-
/// If it's not desirable, use the [`try_from_entropy`] method instead.
403+
/// If it's not desirable, use the [`try_from_os_rng`] method instead.
404404
///
405405
/// In case the overhead of using [`getrandom`] to seed *many* PRNGs is an
406406
/// issue, one may prefer to seed from a local PRNG, e.g.
@@ -411,13 +411,13 @@ pub trait SeedableRng: Sized {
411411
/// If [`getrandom`] is unable to provide secure entropy this method will panic.
412412
///
413413
/// [`getrandom`]: https://docs.rs/getrandom
414-
/// [`try_from_entropy`]: SeedableRng::try_from_entropy
414+
/// [`try_from_os_rng`]: SeedableRng::try_from_os_rng
415415
#[cfg(feature = "getrandom")]
416416
#[cfg_attr(doc_cfg, doc(cfg(feature = "getrandom")))]
417-
fn from_entropy() -> Self {
418-
match Self::try_from_entropy() {
417+
fn from_os_rng() -> Self {
418+
match Self::try_from_os_rng() {
419419
Ok(res) => res,
420-
Err(err) => panic!("from_entropy failed: {}", err),
420+
Err(err) => panic!("from_os_rng failed: {}", err),
421421
}
422422
}
423423

@@ -431,7 +431,7 @@ pub trait SeedableRng: Sized {
431431
/// [`getrandom`]: https://docs.rs/getrandom
432432
#[cfg(feature = "getrandom")]
433433
#[cfg_attr(doc_cfg, doc(cfg(feature = "getrandom")))]
434-
fn try_from_entropy() -> Result<Self, getrandom::Error> {
434+
fn try_from_os_rng() -> Result<Self, getrandom::Error> {
435435
let mut seed = Self::Seed::default();
436436
getrandom::getrandom(seed.as_mut())?;
437437
let res = Self::from_seed(seed);

src/distributions/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ use crate::Rng;
191191
/// use rand::prelude::*;
192192
/// use rand::distributions::Standard;
193193
///
194-
/// let val: f32 = StdRng::from_entropy().sample(Standard);
194+
/// let val: f32 = StdRng::from_os_rng().sample(Standard);
195195
/// println!("f32 from [0, 1): {}", val);
196196
/// ```
197197
///

src/rngs/reseeding.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ use rand_core::{CryptoRng, RngCore, SeedableRng, TryCryptoRng, TryRngCore};
5959
/// use rand::rngs::OsRng;
6060
/// use rand::rngs::ReseedingRng;
6161
///
62-
/// let prng = ChaCha20Core::from_entropy();
62+
/// let prng = ChaCha20Core::from_os_rng();
6363
/// let mut reseeding_rng = ReseedingRng::new(prng, 0, OsRng);
6464
///
6565
/// println!("{}", reseeding_rng.gen::<u64>());

src/rngs/thread.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ thread_local!(
110110
// We require Rc<..> to avoid premature freeing when thread_rng is used
111111
// within thread-local destructors. See #968.
112112
static THREAD_RNG_KEY: Rc<UnsafeCell<ReseedingRng<Core, OsRng>>> = {
113-
let r = Core::try_from_rng(&mut OsRng).unwrap_or_else(|err|
113+
let r = Core::try_from_os_rng().unwrap_or_else(|err|
114114
panic!("could not initialize thread_rng: {}", err));
115115
let rng = ReseedingRng::new(r,
116116
THREAD_RNG_RESEED_THRESHOLD,

0 commit comments

Comments
 (0)