Skip to content

Commit

Permalink
review update
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed Apr 19, 2024
1 parent 5a47df0 commit 5734b55
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 13 deletions.
1 change: 1 addition & 0 deletions rand_core/src/blanket_impls.rs
Expand Up @@ -43,6 +43,7 @@ impl<'a, R: TryRngCore + ?Sized> TryRngCore for &'a mut R {
impl<'a, R: TryCryptoRng + ?Sized> TryCryptoRng for &'a mut R {}

#[cfg(feature = "alloc")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
impl<R: RngCore + ?Sized> RngCore for Box<R> {
#[inline(always)]
fn next_u32(&mut self) -> u32 {
Expand Down
15 changes: 11 additions & 4 deletions rand_core/src/impls.rs
Expand Up @@ -173,24 +173,24 @@ macro_rules! impl_try_rng_from_rng_core {

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

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

#[inline]
fn try_fill_bytes(&mut self, dst: &mut [u8]) -> Result<(), Self::Error> {
self.fill_bytes(dst);
$crate::RngCore::fill_bytes(self, dst);
Ok(())
}
}
};
}

/// Implement [`TryCryptoRng`] for a type implementing [`CryptoRng`].
/// Implement [`TryCryptoRng`] and [`TryRngCore`] for a type implementing [`CryptoRng`].
///
/// Ideally, `rand_core` would define blanket impls for this, but they conflict with blanket impls
/// for `&mut R` and `Box<R>`, so until specialziation is stabilized, implementer crates
Expand All @@ -199,7 +199,14 @@ macro_rules! impl_try_rng_from_rng_core {
macro_rules! impl_try_crypto_rng_from_crypto_rng {
($t:ty) => {
$crate::impl_try_rng_from_rng_core!($t);

impl $crate::TryCryptoRng for $t {}

/// Check at compile time that `$t` implements `CryptoRng`
const _: () = {
const fn check_crypto_rng_impl<T: $crate::CryptoRng>() {}
check_crypto_rng_impl::<$t>();
};
};
}

Expand Down
12 changes: 6 additions & 6 deletions rand_core/src/lib.rs
Expand Up @@ -400,7 +400,7 @@ pub trait SeedableRng: Sized {
/// since it is convenient and secure.
///
/// Note that this method may panic on (extremely unlikely) [`getrandom`] errors.
/// If it's not desirable, use the [`try_from_entropy`] method instead.
/// If it's not desirable, use the [`try_from_os_rng`] method instead.
///
/// In case the overhead of using [`getrandom`] to seed *many* PRNGs is an
/// issue, one may prefer to seed from a local PRNG, e.g.
Expand All @@ -411,13 +411,13 @@ pub trait SeedableRng: Sized {
/// If [`getrandom`] is unable to provide secure entropy this method will panic.
///
/// [`getrandom`]: https://docs.rs/getrandom
/// [`try_from_entropy`]: SeedableRng::try_from_entropy
/// [`try_from_os_rng`]: SeedableRng::try_from_os_rng
#[cfg(feature = "getrandom")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "getrandom")))]
fn from_entropy() -> Self {
match Self::try_from_entropy() {
fn from_os_rng() -> Self {
match Self::try_from_os_rng() {
Ok(res) => res,
Err(err) => panic!("from_entropy failed: {}", err),
Err(err) => panic!("from_os_rng failed: {}", err),
}
}

Expand All @@ -431,7 +431,7 @@ pub trait SeedableRng: Sized {
/// [`getrandom`]: https://docs.rs/getrandom
#[cfg(feature = "getrandom")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "getrandom")))]
fn try_from_entropy() -> Result<Self, getrandom::Error> {
fn try_from_os_rng() -> Result<Self, getrandom::Error> {
let mut seed = Self::Seed::default();
getrandom::getrandom(seed.as_mut())?;
let res = Self::from_seed(seed);
Expand Down
2 changes: 1 addition & 1 deletion src/distributions/mod.rs
Expand Up @@ -191,7 +191,7 @@ use crate::Rng;
/// use rand::prelude::*;
/// use rand::distributions::Standard;
///
/// let val: f32 = StdRng::from_entropy().sample(Standard);
/// let val: f32 = StdRng::from_os_rng().sample(Standard);
/// println!("f32 from [0, 1): {}", val);
/// ```
///
Expand Down
2 changes: 1 addition & 1 deletion src/rngs/reseeding.rs
Expand Up @@ -59,7 +59,7 @@ use rand_core::{CryptoRng, RngCore, SeedableRng, TryCryptoRng, TryRngCore};
/// use rand::rngs::OsRng;
/// use rand::rngs::ReseedingRng;
///
/// let prng = ChaCha20Core::from_entropy();
/// let prng = ChaCha20Core::from_os_rng();
/// let mut reseeding_rng = ReseedingRng::new(prng, 0, OsRng);
///
/// println!("{}", reseeding_rng.gen::<u64>());
Expand Down
2 changes: 1 addition & 1 deletion src/rngs/thread.rs
Expand Up @@ -110,7 +110,7 @@ thread_local!(
// We require Rc<..> to avoid premature freeing when thread_rng is used
// within thread-local destructors. See #968.
static THREAD_RNG_KEY: Rc<UnsafeCell<ReseedingRng<Core, OsRng>>> = {
let r = Core::try_from_rng(&mut OsRng).unwrap_or_else(|err|
let r = Core::try_from_os_rng().unwrap_or_else(|err|
panic!("could not initialize thread_rng: {}", err));
let rng = ReseedingRng::new(r,
THREAD_RNG_RESEED_THRESHOLD,
Expand Down

0 comments on commit 5734b55

Please sign in to comment.