From c7924afac7a060c888ead4f8929ea45cb100f906 Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Sun, 28 Dec 2025 16:53:26 +0000 Subject: [PATCH 1/4] Make RngCore just an alias for TryRngCore --- src/block.rs | 23 +++-- src/lib.rs | 264 ++++++++------------------------------------------- src/utils.rs | 14 ++- 3 files changed, 64 insertions(+), 237 deletions(-) diff --git a/src/block.rs b/src/block.rs index d8b6e12a..db28bbcc 100644 --- a/src/block.rs +++ b/src/block.rs @@ -14,7 +14,8 @@ //! # Example //! //! ``` -//! use rand_core::{RngCore, SeedableRng}; +//! use core::convert::Infallible; +//! use rand_core::{TryRngCore, SeedableRng}; //! use rand_core::block::{Generator, BlockRng}; //! //! struct MyRngCore { @@ -45,28 +46,30 @@ //! } //! } //! -//! impl RngCore for MyRng { +//! impl TryRngCore for MyRng { +//! type Error = Infallible; +//! //! #[inline] -//! fn next_u32(&mut self) -> u32 { -//! self.0.next_word() +//! fn try_next_u32(&mut self) -> Result { +//! Ok(self.0.next_word()) //! } //! //! #[inline] -//! fn next_u64(&mut self) -> u64 { -//! self.0.next_u64_from_u32() +//! fn try_next_u64(&mut self) -> Result { +//! Ok(self.0.next_u64_from_u32()) //! } //! //! #[inline] -//! fn fill_bytes(&mut self, bytes: &mut [u8]) { -//! self.0.fill_bytes(bytes) +//! fn try_fill_bytes(&mut self, bytes: &mut [u8]) -> Result<(), Infallible> { +//! Ok(self.0.fill_bytes(bytes)) //! } //! } //! //! // And if applicable: impl CryptoRng for MyRng {} //! //! let mut rng = MyRng::seed_from_u64(0); -//! println!("First value: {}", rng.next_u32()); -//! # assert_eq!(rng.next_u32(), 1171109249); +//! println!("First value: {}", rng.try_next_u32().unwrap()); +//! # assert_eq!(rng.try_next_u32(), Ok(1171109249)); //! ``` //! //! # ReseedingRng diff --git a/src/lib.rs b/src/lib.rs index a7323bc7..bba9df0c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,137 +13,12 @@ clippy::undocumented_unsafe_blocks )] -use core::{fmt, ops::DerefMut}; +use core::{convert::Infallible, fmt, ops::DerefMut}; pub mod block; pub mod utils; mod word; -/// Implementation-level interface for RNGs -/// -/// This trait encapsulates the low-level functionality common to all -/// generators, and is the "back end", to be implemented by generators. -/// End users should normally use the [`rand::Rng`] trait -/// which is automatically implemented for every type implementing `RngCore`. -/// -/// Three different methods for generating random data are provided since the -/// optimal implementation of each is dependent on the type of generator. There -/// is no required relationship between the output of each; e.g. many -/// implementations of [`fill_bytes`] consume a whole number of `u32` or `u64` -/// values and drop any remaining unused bytes. The same can happen with the -/// [`next_u32`] and [`next_u64`] methods, implementations may discard some -/// random bits for efficiency. -/// -/// # Properties of a generator -/// -/// Implementers should produce bits uniformly. Pathological RNGs (e.g. constant -/// or counting generators which rarely change some bits) may cause issues in -/// consumers of random data, for example dead-locks in rejection samplers and -/// obviously non-random output (e.g. a counting generator may result in -/// apparently-constant output from a uniform-ranged distribution). -/// -/// Algorithmic generators implementing [`SeedableRng`] should normally have -/// *portable, reproducible* output, i.e. fix Endianness when converting values -/// to avoid platform differences, and avoid making any changes which affect -/// output (except by communicating that the release has breaking changes). -/// -/// # Implementing `RngCore` -/// -/// Typically an RNG will implement only one of the methods available -/// in this trait directly, then use the helper functions from the -/// [`utils`] module to implement the other methods. -/// -/// Note that implementors of [`RngCore`] also automatically implement -/// the [`TryRngCore`] trait with the `Error` associated type being -/// equal to [`Infallible`]. -/// -/// It is recommended that implementations also implement: -/// -/// - `Debug` with a custom implementation which *does not* print any internal -/// state (at least, [`CryptoRng`]s should not risk leaking state through -/// `Debug`). -/// - `Serialize` and `Deserialize` (from Serde), preferably making Serde -/// support optional at the crate level in PRNG libs. -/// - `Clone`, if possible. -/// - *never* implement `Copy` (accidental copies may cause repeated values). -/// - *do not* implement `Default` for pseudorandom generators, but instead -/// implement [`SeedableRng`], to guide users towards proper seeding. -/// External / hardware RNGs can choose to implement `Default`. -/// - `Eq` and `PartialEq` could be implemented, but are probably not useful. -/// -/// [`rand::Rng`]: https://docs.rs/rand/latest/rand/trait.Rng.html -/// [`fill_bytes`]: RngCore::fill_bytes -/// [`next_u32`]: RngCore::next_u32 -/// [`next_u64`]: RngCore::next_u64 -/// [`Infallible`]: core::convert::Infallible -pub trait RngCore { - /// Return the next random `u32`. - fn next_u32(&mut self) -> u32; - - /// Return the next random `u64`. - fn next_u64(&mut self) -> u64; - - /// Fill `dest` with random data. - /// - /// This method should guarantee that `dest` is entirely filled - /// with new data, and may panic if this is impossible - /// (e.g. reading past the end of a file that is being used as the - /// source of randomness). - fn fill_bytes(&mut self, dst: &mut [u8]); -} - -impl RngCore for T -where - T::Target: RngCore, -{ - #[inline] - fn next_u32(&mut self) -> u32 { - self.deref_mut().next_u32() - } - - #[inline] - fn next_u64(&mut self) -> u64 { - self.deref_mut().next_u64() - } - - #[inline] - fn fill_bytes(&mut self, dst: &mut [u8]) { - self.deref_mut().fill_bytes(dst); - } -} - -/// A marker trait over [`RngCore`] for securely unpredictable RNGs -/// -/// This marker trait indicates that the implementing generator is intended, -/// when correctly seeded and protected from side-channel attacks such as a -/// leaking of state, to be a cryptographically secure generator. This trait is -/// provided as a tool to aid review of cryptographic code, but does not by -/// itself guarantee suitability for cryptographic applications. -/// -/// Implementors of `CryptoRng` automatically implement the [`TryCryptoRng`] -/// trait. -/// -/// Implementors of `CryptoRng` should only implement [`Default`] if the -/// `default()` instances are themselves secure generators: for example if the -/// implementing type is a stateless interface over a secure external generator -/// (like [`OsRng`]) or if the `default()` instance uses a strong, fresh seed. -/// -/// Formally, a CSPRNG (Cryptographically Secure Pseudo-Random Number Generator) -/// should satisfy an additional property over other generators: assuming that -/// the generator has been appropriately seeded and has unknown state, then -/// given the first *k* bits of an algorithm's output -/// sequence, it should not be possible using polynomial-time algorithms to -/// predict the next bit with probability significantly greater than 50%. -/// -/// An optional property of CSPRNGs is backtracking resistance: if the CSPRNG's -/// state is revealed, it will not be computationally-feasible to reconstruct -/// prior output values. This property is not required by `CryptoRng`. -/// -/// [`OsRng`]: https://docs.rs/rand/latest/rand/rngs/struct.OsRng.html -pub trait CryptoRng: RngCore {} - -impl CryptoRng for T where T::Target: CryptoRng {} - /// A potentially fallible variant of [`RngCore`] /// /// This trait is a generalization of [`RngCore`] to support potentially- @@ -176,36 +51,34 @@ pub trait TryRngCore { { UnwrapErr(self) } - - /// Wrap RNG with the [`UnwrapMut`] wrapper. - fn unwrap_mut(&mut self) -> UnwrapMut<'_, Self> { - UnwrapMut(self) - } } -// Note that, unfortunately, this blanket impl prevents us from implementing -// `TryRngCore` for types which can be dereferenced to `TryRngCore`, i.e. `TryRngCore` -// will not be automatically implemented for `&mut R`, `Box`, etc. -impl TryRngCore for R { - type Error = core::convert::Infallible; +impl TryRngCore for T +where + T::Target: TryRngCore, +{ + type Error = ::Error; #[inline] fn try_next_u32(&mut self) -> Result { - Ok(self.next_u32()) + self.deref_mut().try_next_u32() } #[inline] fn try_next_u64(&mut self) -> Result { - Ok(self.next_u64()) + self.deref_mut().try_next_u64() } #[inline] fn try_fill_bytes(&mut self, dst: &mut [u8]) -> Result<(), Self::Error> { - self.fill_bytes(dst); - Ok(()) + self.deref_mut().try_fill_bytes(dst) } } +/// An infallible [`TryRngCore`] +pub trait RngCore: TryRngCore {} +impl> RngCore for R {} + /// A marker trait over [`TryRngCore`] for securely unpredictable RNGs /// /// This trait is like [`CryptoRng`] but for the trait [`TryRngCore`]. @@ -224,71 +97,47 @@ impl TryRngCore for R { /// [`OsRng`]: https://docs.rs/rand/latest/rand/rngs/struct.OsRng.html pub trait TryCryptoRng: TryRngCore {} -impl TryCryptoRng for R {} - /// Wrapper around [`TryRngCore`] implementation which implements [`RngCore`] /// by panicking on potential errors. #[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Hash)] pub struct UnwrapErr(pub R); -impl RngCore for UnwrapErr { +impl TryRngCore for UnwrapErr { + type Error = Infallible; + #[inline] - fn next_u32(&mut self) -> u32 { - self.0.try_next_u32().unwrap() + fn try_next_u32(&mut self) -> Result { + Ok(self.0.try_next_u32().unwrap()) } #[inline] - fn next_u64(&mut self) -> u64 { - self.0.try_next_u64().unwrap() + fn try_next_u64(&mut self) -> Result { + Ok(self.0.try_next_u64().unwrap()) } #[inline] - fn fill_bytes(&mut self, dst: &mut [u8]) { - self.0.try_fill_bytes(dst).unwrap() + fn try_fill_bytes(&mut self, dst: &mut [u8]) -> Result<(), Infallible> { + Ok(self.0.try_fill_bytes(dst).unwrap()) } } -impl CryptoRng for UnwrapErr {} +impl TryCryptoRng for UnwrapErr {} -/// Wrapper around [`TryRngCore`] implementation which implements [`RngCore`] -/// by panicking on potential errors. -#[derive(Debug, Eq, PartialEq, Hash)] -pub struct UnwrapMut<'r, R: TryRngCore + ?Sized>(pub &'r mut R); - -impl<'r, R: TryRngCore + ?Sized> UnwrapMut<'r, R> { +impl<'r, R: TryRngCore + ?Sized> UnwrapErr<&'r mut R> { /// Reborrow with a new lifetime /// /// Rust allows references like `&T` or `&mut T` to be "reborrowed" through /// coercion: essentially, the pointer is copied under a new, shorter, lifetime. /// Until rfcs#1403 lands, reborrows on user types require a method call. #[inline(always)] - pub fn re<'b>(&'b mut self) -> UnwrapMut<'b, R> + pub fn re<'b>(&'b mut self) -> UnwrapErr<&'b mut R> where 'r: 'b, { - UnwrapMut(self.0) - } -} - -impl RngCore for UnwrapMut<'_, R> { - #[inline] - fn next_u32(&mut self) -> u32 { - self.0.try_next_u32().unwrap() - } - - #[inline] - fn next_u64(&mut self) -> u64 { - self.0.try_next_u64().unwrap() - } - - #[inline] - fn fill_bytes(&mut self, dst: &mut [u8]) { - self.0.try_fill_bytes(dst).unwrap() + UnwrapErr(&mut self.0) } } -impl CryptoRng for UnwrapMut<'_, R> {} - /// A random number generator that can be explicitly seeded. /// /// This trait encapsulates the low-level functionality common to all @@ -454,7 +303,9 @@ pub trait SeedableRng: Sized { /// [`rand`]: https://docs.rs/rand fn from_rng(rng: &mut R) -> Self { let mut seed = Self::Seed::default(); - rng.fill_bytes(seed.as_mut()); + match rng.try_fill_bytes(seed.as_mut()) { + Ok(()) => {} + } Self::from_seed(seed) } @@ -543,19 +394,21 @@ mod test { // A stub RNG. struct SomeRng; - impl RngCore for SomeRng { - fn next_u32(&mut self) -> u32 { + impl TryRngCore for SomeRng { + type Error = Infallible; + + fn try_next_u32(&mut self) -> Result { unimplemented!() } - fn next_u64(&mut self) -> u64 { + fn try_next_u64(&mut self) -> Result { unimplemented!() } - fn fill_bytes(&mut self, _: &mut [u8]) { + fn try_fill_bytes(&mut self, _dst: &mut [u8]) -> Result<(), Self::Error> { unimplemented!() } } - impl CryptoRng for SomeRng {} + impl TryCryptoRng for SomeRng {} #[test] fn dyn_rngcore_to_tryrngcore() { @@ -575,24 +428,6 @@ mod test { assert!(my_api(&mut SomeRng)); } - #[test] - fn dyn_cryptorng_to_trycryptorng() { - // Illustrates the need for `+ ?Sized` bound in `impl TryCryptoRng for R`. - - // A method in another crate taking a fallible RNG - fn third_party_api(_rng: &mut (impl TryCryptoRng + ?Sized)) -> bool { - true - } - - // A method in our crate requiring an infallible RNG - fn my_api(rng: &mut dyn CryptoRng) -> bool { - // We want to call the method above - third_party_api(rng) - } - - assert!(my_api(&mut SomeRng)); - } - #[test] fn dyn_unwrap_mut_tryrngcore() { // Illustrates the need for `+ ?Sized` bound in @@ -603,24 +438,7 @@ mod test { } fn my_api(rng: &mut (impl TryRngCore + ?Sized)) -> bool { - let mut infallible_rng = rng.unwrap_mut(); - third_party_api(&mut infallible_rng) - } - - assert!(my_api(&mut SomeRng)); - } - - #[test] - fn dyn_unwrap_mut_trycryptorng() { - // Illustrates the need for `+ ?Sized` bound in - // `impl CryptoRng for UnwrapMut<'_, R>`. - - fn third_party_api(_rng: &mut impl CryptoRng) -> bool { - true - } - - fn my_api(rng: &mut (impl TryCryptoRng + ?Sized)) -> bool { - let mut infallible_rng = rng.unwrap_mut(); + let mut infallible_rng = rng.unwrap_err(); third_party_api(&mut infallible_rng) } @@ -645,14 +463,14 @@ mod test { } let mut rng = FourRng; - let mut rng = rng.unwrap_mut(); + let mut rng = (&mut rng).unwrap_err(); - assert_eq!(rng.next_u32(), 4); + assert_eq!(rng.try_next_u32(), Ok(4)); { let mut rng2 = rng.re(); - assert_eq!(rng2.next_u32(), 4); + assert_eq!(rng2.try_next_u32(), Ok(4)); // Make sure rng2 is dropped. } - assert_eq!(rng.next_u32(), 4); + assert_eq!(rng.try_next_u32(), Ok(4)); } } diff --git a/src/utils.rs b/src/utils.rs index 4fcd294c..d7f287bf 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -39,7 +39,7 @@ //! We demonstrate a simple multiplicative congruential generator (MCG), taken //! from M.E. O'Neill's blog post //! [Does It Beat the Minimal Standard?](https://www.pcg-random.org/posts/does-it-beat-the-minimal-standard.html). -//! ``` +//! ```ignore //! use rand_core::{RngCore, SeedableRng, utils}; //! //! pub struct Mcg128(u128); @@ -89,8 +89,12 @@ pub use crate::word::Word; #[inline] pub fn next_u64_via_u32(rng: &mut R) -> u64 { // Use LE; we explicitly generate one value before the next. - let x = u64::from(rng.next_u32()); - let y = u64::from(rng.next_u32()); + let x: u64 = match rng.try_next_u32() { + Ok(x) => x.into(), + }; + let y: u64 = match rng.try_next_u32() { + Ok(y) => y.into(), + }; (y << 32) | x } @@ -118,7 +122,9 @@ pub fn fill_bytes_via_next_word(dst: &mut [u8], mut next_word: impl FnM /// This may be used to implement `next_u32` or `next_u64`. pub fn next_word_via_fill(rng: &mut R) -> W { let mut buf: W::Bytes = Default::default(); - rng.fill_bytes(buf.as_mut()); + match rng.try_fill_bytes(buf.as_mut()) { + Ok(()) => {} + } W::from_le_bytes(buf) } From d659af0f0d4cc65c204fc624e6c3c82a014e50bd Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Sun, 28 Dec 2025 17:01:00 +0000 Subject: [PATCH 2/4] Rename TryCryptoRng -> CryptoRng --- src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index bba9df0c..d9f7de8e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -89,13 +89,13 @@ impl> RngCore for R {} /// provided as a tool to aid review of cryptographic code, but does not by /// itself guarantee suitability for cryptographic applications. /// -/// Implementors of `TryCryptoRng` should only implement [`Default`] if the +/// Implementors of `CryptoRng` should only implement [`Default`] if the /// `default()` instances are themselves secure generators: for example if the /// implementing type is a stateless interface over a secure external generator /// (like [`OsRng`]) or if the `default()` instance uses a strong, fresh seed. /// /// [`OsRng`]: https://docs.rs/rand/latest/rand/rngs/struct.OsRng.html -pub trait TryCryptoRng: TryRngCore {} +pub trait CryptoRng: TryRngCore {} /// Wrapper around [`TryRngCore`] implementation which implements [`RngCore`] /// by panicking on potential errors. @@ -121,7 +121,7 @@ impl TryRngCore for UnwrapErr { } } -impl TryCryptoRng for UnwrapErr {} +impl CryptoRng for UnwrapErr {} impl<'r, R: TryRngCore + ?Sized> UnwrapErr<&'r mut R> { /// Reborrow with a new lifetime @@ -408,7 +408,7 @@ mod test { } } - impl TryCryptoRng for SomeRng {} + impl CryptoRng for SomeRng {} #[test] fn dyn_rngcore_to_tryrngcore() { From 9c8a7a8ad0dca2fe689605eb3ee7dfc108f2249b Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Sun, 28 Dec 2025 17:04:11 +0000 Subject: [PATCH 3/4] Rename RngCore -> InfallibleRng --- src/lib.rs | 16 ++++++++-------- src/utils.rs | 10 +++++----- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d9f7de8e..0fe96784 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -76,8 +76,8 @@ where } /// An infallible [`TryRngCore`] -pub trait RngCore: TryRngCore {} -impl> RngCore for R {} +pub trait InfallibleRng: TryRngCore {} +impl> InfallibleRng for R {} /// A marker trait over [`TryRngCore`] for securely unpredictable RNGs /// @@ -301,7 +301,7 @@ pub trait SeedableRng: Sized { /// (in prior versions this was not required). /// /// [`rand`]: https://docs.rs/rand - fn from_rng(rng: &mut R) -> Self { + fn from_rng(rng: &mut R) -> Self { let mut seed = Self::Seed::default(); match rng.try_fill_bytes(seed.as_mut()) { Ok(()) => {} @@ -326,7 +326,7 @@ pub trait SeedableRng: Sized { /// This is useful when initializing a PRNG for a thread fn fork(&mut self) -> Self where - Self: RngCore, + Self: InfallibleRng, { Self::from_rng(self) } @@ -412,7 +412,7 @@ mod test { #[test] fn dyn_rngcore_to_tryrngcore() { - // Illustrates the need for `+ ?Sized` bound in `impl TryRngCore for R`. + // Illustrates the need for `+ ?Sized` bound in `impl TryRngCore for R`. // A method in another crate taking a fallible RNG fn third_party_api(_rng: &mut (impl TryRngCore + ?Sized)) -> bool { @@ -420,7 +420,7 @@ mod test { } // A method in our crate requiring an infallible RNG - fn my_api(rng: &mut dyn RngCore) -> bool { + fn my_api(rng: &mut dyn InfallibleRng) -> bool { // We want to call the method above third_party_api(rng) } @@ -431,9 +431,9 @@ mod test { #[test] fn dyn_unwrap_mut_tryrngcore() { // Illustrates the need for `+ ?Sized` bound in - // `impl RngCore for UnwrapMut<'_, R>`. + // `impl InfallibleRng for UnwrapMut<'_, R>`. - fn third_party_api(_rng: &mut impl RngCore) -> bool { + fn third_party_api(_rng: &mut impl InfallibleRng) -> bool { true } diff --git a/src/utils.rs b/src/utils.rs index d7f287bf..30ba097c 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -80,14 +80,14 @@ //! # assert_eq!(buf, [154, 23, 43, 68, 75]); //! ``` -use crate::RngCore; +use crate::InfallibleRng; #[allow(unused)] use crate::SeedableRng; pub use crate::word::Word; /// Implement `next_u64` via `next_u32`, little-endian order. #[inline] -pub fn next_u64_via_u32(rng: &mut R) -> u64 { +pub fn next_u64_via_u32(rng: &mut R) -> u64 { // Use LE; we explicitly generate one value before the next. let x: u64 = match rng.try_next_u32() { Ok(x) => x.into(), @@ -100,7 +100,7 @@ pub fn next_u64_via_u32(rng: &mut R) -> u64 { /// Fill `dst` with bytes using `next_word` /// -/// This may be used to implement [`RngCore::fill_bytes`] over `next_u32` or +/// This may be used to implement [`InfallibleRng::fill_bytes`] over `next_u32` or /// `next_u64`. Words are used in order of generation. The last word may be /// partially discarded. #[inline] @@ -117,10 +117,10 @@ pub fn fill_bytes_via_next_word(dst: &mut [u8], mut next_word: impl FnM } } -/// Yield a word using [`RngCore::fill_bytes`] +/// Yield a word using [`InfallibleRng::fill_bytes`] /// /// This may be used to implement `next_u32` or `next_u64`. -pub fn next_word_via_fill(rng: &mut R) -> W { +pub fn next_word_via_fill(rng: &mut R) -> W { let mut buf: W::Bytes = Default::default(); match rng.try_fill_bytes(buf.as_mut()) { Ok(()) => {} From 85f7d9ba0a13f69e16248166abf72bff29b00de0 Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Sun, 28 Dec 2025 17:05:32 +0000 Subject: [PATCH 4/4] Rename TryRngCore -> TryRng --- src/block.rs | 4 ++-- src/lib.rs | 44 +++++++++++++++++++++----------------------- 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/src/block.rs b/src/block.rs index db28bbcc..34335a06 100644 --- a/src/block.rs +++ b/src/block.rs @@ -15,7 +15,7 @@ //! //! ``` //! use core::convert::Infallible; -//! use rand_core::{TryRngCore, SeedableRng}; +//! use rand_core::{TryRng, SeedableRng}; //! use rand_core::block::{Generator, BlockRng}; //! //! struct MyRngCore { @@ -46,7 +46,7 @@ //! } //! } //! -//! impl TryRngCore for MyRng { +//! impl TryRng for MyRng { //! type Error = Infallible; //! //! #[inline] diff --git a/src/lib.rs b/src/lib.rs index 0fe96784..d3927ec4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,7 +33,7 @@ mod word; /// panic in case the underlying fallible RNG yields an error. /// /// [`OsRng`]: https://docs.rs/rand/latest/rand/rngs/struct.OsRng.html -pub trait TryRngCore { +pub trait TryRng { /// The type returned in the event of a RNG error. type Error: fmt::Debug + fmt::Display; @@ -53,11 +53,11 @@ pub trait TryRngCore { } } -impl TryRngCore for T +impl TryRng for T where - T::Target: TryRngCore, + T::Target: TryRng, { - type Error = ::Error; + type Error = ::Error; #[inline] fn try_next_u32(&mut self) -> Result { @@ -75,13 +75,11 @@ where } } -/// An infallible [`TryRngCore`] -pub trait InfallibleRng: TryRngCore {} -impl> InfallibleRng for R {} +/// An infallible [`TryRng`] +pub trait InfallibleRng: TryRng {} +impl> InfallibleRng for R {} -/// A marker trait over [`TryRngCore`] for securely unpredictable RNGs -/// -/// This trait is like [`CryptoRng`] but for the trait [`TryRngCore`]. +/// A marker trait over [`TryRng`] for securely unpredictable RNGs /// /// This marker trait indicates that the implementing generator is intended, /// when correctly seeded and protected from side-channel attacks such as a @@ -95,14 +93,14 @@ impl> InfallibleRng for R {} /// (like [`OsRng`]) or if the `default()` instance uses a strong, fresh seed. /// /// [`OsRng`]: https://docs.rs/rand/latest/rand/rngs/struct.OsRng.html -pub trait CryptoRng: TryRngCore {} +pub trait CryptoRng: TryRng {} -/// Wrapper around [`TryRngCore`] implementation which implements [`RngCore`] +/// Wrapper around [`TryRng`] implementation which implements [`RngCore`] /// by panicking on potential errors. #[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Hash)] -pub struct UnwrapErr(pub R); +pub struct UnwrapErr(pub R); -impl TryRngCore for UnwrapErr { +impl TryRng for UnwrapErr { type Error = Infallible; #[inline] @@ -123,7 +121,7 @@ impl TryRngCore for UnwrapErr { impl CryptoRng for UnwrapErr {} -impl<'r, R: TryRngCore + ?Sized> UnwrapErr<&'r mut R> { +impl<'r, R: TryRng + ?Sized> UnwrapErr<&'r mut R> { /// Reborrow with a new lifetime /// /// Rust allows references like `&T` or `&mut T` to be "reborrowed" through @@ -312,7 +310,7 @@ pub trait SeedableRng: Sized { /// Create a new PRNG seeded from a potentially fallible `Rng`. /// /// See [`from_rng`][SeedableRng::from_rng] docs for more information. - fn try_from_rng(rng: &mut R) -> Result { + fn try_from_rng(rng: &mut R) -> Result { let mut seed = Self::Seed::default(); rng.try_fill_bytes(seed.as_mut())?; Ok(Self::from_seed(seed)) @@ -341,7 +339,7 @@ pub trait SeedableRng: Sized { /// This is the failable equivalent to [`SeedableRng::fork`] fn try_fork(&mut self) -> Result where - Self: TryRngCore, + Self: TryRng, { Self::try_from_rng(self) } @@ -394,7 +392,7 @@ mod test { // A stub RNG. struct SomeRng; - impl TryRngCore for SomeRng { + impl TryRng for SomeRng { type Error = Infallible; fn try_next_u32(&mut self) -> Result { @@ -412,10 +410,10 @@ mod test { #[test] fn dyn_rngcore_to_tryrngcore() { - // Illustrates the need for `+ ?Sized` bound in `impl TryRngCore for R`. + // Illustrates the need for `+ ?Sized` bound in `impl TryRng for R`. // A method in another crate taking a fallible RNG - fn third_party_api(_rng: &mut (impl TryRngCore + ?Sized)) -> bool { + fn third_party_api(_rng: &mut (impl TryRng + ?Sized)) -> bool { true } @@ -431,13 +429,13 @@ mod test { #[test] fn dyn_unwrap_mut_tryrngcore() { // Illustrates the need for `+ ?Sized` bound in - // `impl InfallibleRng for UnwrapMut<'_, R>`. + // `impl InfallibleRng for UnwrapMut<'_, R>`. fn third_party_api(_rng: &mut impl InfallibleRng) -> bool { true } - fn my_api(rng: &mut (impl TryRngCore + ?Sized)) -> bool { + fn my_api(rng: &mut (impl TryRng + ?Sized)) -> bool { let mut infallible_rng = rng.unwrap_err(); third_party_api(&mut infallible_rng) } @@ -449,7 +447,7 @@ mod test { fn reborrow_unwrap_mut() { struct FourRng; - impl TryRngCore for FourRng { + impl TryRng for FourRng { type Error = core::convert::Infallible; fn try_next_u32(&mut self) -> Result { Ok(4)