From e567a95da2d60c265f07ec22000cd941bab475a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Fri, 22 Mar 2024 17:28:57 +0300 Subject: [PATCH] Add `rand_core::InfallibleRng` marker trait --- rand_chacha/src/chacha.rs | 4 +++- rand_core/src/lib.rs | 8 ++++++++ rand_pcg/src/pcg128.rs | 5 ++++- rand_pcg/src/pcg128cm.rs | 4 +++- rand_pcg/src/pcg64.rs | 4 +++- src/lib.rs | 2 +- src/rngs/mock.rs | 4 +++- src/rngs/small.rs | 4 +++- src/rngs/std.rs | 3 ++- src/rngs/xoshiro128plusplus.rs | 4 +++- src/rngs/xoshiro256plusplus.rs | 4 +++- 11 files changed, 36 insertions(+), 10 deletions(-) diff --git a/rand_chacha/src/chacha.rs b/rand_chacha/src/chacha.rs index ebc28a8ab04..ccae19f8c9a 100644 --- a/rand_chacha/src/chacha.rs +++ b/rand_chacha/src/chacha.rs @@ -14,7 +14,7 @@ use self::core::fmt; use crate::guts::ChaCha; use rand_core::block::{BlockRng, BlockRngCore, CryptoBlockRng}; -use rand_core::{CryptoRng, Error, RngCore, SeedableRng}; +use rand_core::{CryptoRng, Error, InfallibleRng, RngCore, SeedableRng}; #[cfg(feature = "serde1")] use serde::{Serialize, Deserialize, Serializer, Deserializer}; @@ -101,6 +101,8 @@ macro_rules! chacha_impl { impl CryptoBlockRng for $ChaChaXCore {} + impl InfallibleRng for $ChaChaXRng {} + /// A cryptographically secure random number generator that uses the ChaCha algorithm. /// /// ChaCha is a stream cipher designed by Daniel J. Bernstein[^1], that we use as an RNG. It is diff --git a/rand_core/src/lib.rs b/rand_core/src/lib.rs index 70424e6c9c0..25c310cbad9 100644 --- a/rand_core/src/lib.rs +++ b/rand_core/src/lib.rs @@ -212,6 +212,14 @@ pub trait RngCore { /// [`BlockRngCore`]: block::BlockRngCore pub trait CryptoRng: RngCore {} +/// A marker trait used to indicate that an [`RngCore`] implementation is +/// supposed to never return errors from the [`RngCore::try_fill_bytes`] method +/// and never panic on unwrapping [`Error`] while calling other methods. +/// +/// This trait is usually implemented by PRNGs, as opposed to OS, hardware, +/// and periodically-reseeded RNGs, which may fail with IO errors. +pub trait InfallibleRng: RngCore {} + /// A random number generator that can be explicitly seeded. /// /// This trait encapsulates the low-level functionality common to all diff --git a/rand_pcg/src/pcg128.rs b/rand_pcg/src/pcg128.rs index df2025dc444..3d65dc0c771 100644 --- a/rand_pcg/src/pcg128.rs +++ b/rand_pcg/src/pcg128.rs @@ -14,7 +14,7 @@ const MULTIPLIER: u128 = 0x2360_ED05_1FC6_5DA4_4385_DF64_9FCC_F645; use core::fmt; -use rand_core::{impls, le, Error, RngCore, SeedableRng}; +use rand_core::{impls, le, Error, InfallibleRng, RngCore, SeedableRng}; #[cfg(feature = "serde1")] use serde::{Deserialize, Serialize}; /// A PCG random number generator (XSL RR 128/64 (LCG) variant). @@ -159,6 +159,7 @@ impl RngCore for Lcg128Xsl64 { } } +impl InfallibleRng for Lcg128Xsl64 {} /// A PCG random number generator (XSL 128/64 (MCG) variant). /// @@ -269,6 +270,8 @@ impl RngCore for Mcg128Xsl64 { } } +impl InfallibleRng for Mcg128Xsl64 {} + #[inline(always)] fn output_xsl_rr(state: u128) -> u64 { // Output function XSL RR ("xorshift low (bits), random rotation") diff --git a/rand_pcg/src/pcg128cm.rs b/rand_pcg/src/pcg128cm.rs index 7ac5187e4e0..113d1bcb2f4 100644 --- a/rand_pcg/src/pcg128cm.rs +++ b/rand_pcg/src/pcg128cm.rs @@ -14,7 +14,7 @@ const MULTIPLIER: u64 = 15750249268501108917; use core::fmt; -use rand_core::{impls, le, Error, RngCore, SeedableRng}; +use rand_core::{impls, le, Error, InfallibleRng, RngCore, SeedableRng}; #[cfg(feature = "serde1")] use serde::{Deserialize, Serialize}; /// A PCG random number generator (CM DXSM 128/64 (LCG) variant). @@ -165,6 +165,8 @@ impl RngCore for Lcg128CmDxsm64 { } } +impl InfallibleRng for Lcg128CmDxsm64 {} + #[inline(always)] fn output_dxsm(state: u128) -> u64 { // See https://github.com/imneme/pcg-cpp/blob/ffd522e7188bef30a00c74dc7eb9de5faff90092/include/pcg_random.hpp#L1016 diff --git a/rand_pcg/src/pcg64.rs b/rand_pcg/src/pcg64.rs index 365f1c0b117..a622afc0b25 100644 --- a/rand_pcg/src/pcg64.rs +++ b/rand_pcg/src/pcg64.rs @@ -11,7 +11,7 @@ //! PCG random number generators use core::fmt; -use rand_core::{impls, le, Error, RngCore, SeedableRng}; +use rand_core::{impls, le, Error, InfallibleRng, RngCore, SeedableRng}; #[cfg(feature = "serde1")] use serde::{Deserialize, Serialize}; // This is the default multiplier used by PCG for 64-bit state. @@ -167,3 +167,5 @@ impl RngCore for Lcg64Xsh32 { Ok(()) } } + +impl InfallibleRng for Lcg64Xsh32 {} diff --git a/src/lib.rs b/src/lib.rs index dc9e29d6277..f90108a2f45 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -92,7 +92,7 @@ macro_rules! error { ($($x:tt)*) => ( ) } // Re-exports from rand_core -pub use rand_core::{CryptoRng, Error, RngCore, SeedableRng}; +pub use rand_core::{CryptoRng, Error, InfallibleRng, RngCore, SeedableRng}; // Public modules pub mod distributions; diff --git a/src/rngs/mock.rs b/src/rngs/mock.rs index 0d9e0f905c9..c7355bf8cdc 100644 --- a/src/rngs/mock.rs +++ b/src/rngs/mock.rs @@ -8,7 +8,7 @@ //! Mock random number generator -use rand_core::{impls, Error, RngCore}; +use rand_core::{impls, Error, InfallibleRng, RngCore}; #[cfg(feature = "serde1")] use serde::{Serialize, Deserialize}; @@ -81,6 +81,8 @@ impl RngCore for StepRng { } } +impl InfallibleRng for StepRng {} + #[cfg(test)] mod tests { #[cfg(any(feature = "alloc", feature = "serde1"))] diff --git a/src/rngs/small.rs b/src/rngs/small.rs index 2841b0b5dd8..8bc2c4cd065 100644 --- a/src/rngs/small.rs +++ b/src/rngs/small.rs @@ -8,7 +8,7 @@ //! A small fast RNG -use rand_core::{Error, RngCore, SeedableRng}; +use rand_core::{Error, InfallibleRng, RngCore, SeedableRng}; #[cfg(target_pointer_width = "64")] type Rng = super::xoshiro256plusplus::Xoshiro256PlusPlus; @@ -64,6 +64,8 @@ impl RngCore for SmallRng { } } +impl InfallibleRng for SmallRng {} + impl SmallRng { /// Construct an instance seeded from another `Rng` /// diff --git a/src/rngs/std.rs b/src/rngs/std.rs index 3de125f3e5c..f99a551d2e5 100644 --- a/src/rngs/std.rs +++ b/src/rngs/std.rs @@ -8,7 +8,7 @@ //! The standard RNG -use crate::{CryptoRng, Error, RngCore, SeedableRng}; +use rand_core::{CryptoRng, Error, InfallibleRng, RngCore, SeedableRng}; #[cfg(any(test, feature = "getrandom"))] pub(crate) use rand_chacha::ChaCha12Core as Core; @@ -73,6 +73,7 @@ impl SeedableRng for StdRng { impl CryptoRng for StdRng {} +impl InfallibleRng for StdRng {} #[cfg(test)] mod test { diff --git a/src/rngs/xoshiro128plusplus.rs b/src/rngs/xoshiro128plusplus.rs index ece98fafd6a..fd92800b943 100644 --- a/src/rngs/xoshiro128plusplus.rs +++ b/src/rngs/xoshiro128plusplus.rs @@ -9,7 +9,7 @@ #[cfg(feature="serde1")] use serde::{Serialize, Deserialize}; use rand_core::impls::{next_u64_via_u32, fill_bytes_via_next}; use rand_core::le::read_u32_into; -use rand_core::{SeedableRng, RngCore, Error}; +use rand_core::{Error, InfallibleRng, RngCore, SeedableRng}; /// A xoshiro128++ random number generator. /// @@ -97,6 +97,8 @@ impl RngCore for Xoshiro128PlusPlus { } } +impl InfallibleRng for Xoshiro128PlusPlus {} + #[cfg(test)] mod tests { use super::*; diff --git a/src/rngs/xoshiro256plusplus.rs b/src/rngs/xoshiro256plusplus.rs index 8ffb18b8033..f0522054534 100644 --- a/src/rngs/xoshiro256plusplus.rs +++ b/src/rngs/xoshiro256plusplus.rs @@ -9,7 +9,7 @@ #[cfg(feature="serde1")] use serde::{Serialize, Deserialize}; use rand_core::impls::fill_bytes_via_next; use rand_core::le::read_u64_into; -use rand_core::{SeedableRng, RngCore, Error}; +use rand_core::{Error, InfallibleRng, RngCore, SeedableRng}; /// A xoshiro256++ random number generator. /// @@ -99,6 +99,8 @@ impl RngCore for Xoshiro256PlusPlus { } } +impl InfallibleRng for Xoshiro256PlusPlus {} + #[cfg(test)] mod tests { use super::*;