Skip to content

Commit e567a95

Browse files
committed
Add rand_core::InfallibleRng marker trait
1 parent 2eaf854 commit e567a95

File tree

11 files changed

+36
-10
lines changed

11 files changed

+36
-10
lines changed

rand_chacha/src/chacha.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use self::core::fmt;
1515
use crate::guts::ChaCha;
1616
use rand_core::block::{BlockRng, BlockRngCore, CryptoBlockRng};
17-
use rand_core::{CryptoRng, Error, RngCore, SeedableRng};
17+
use rand_core::{CryptoRng, Error, InfallibleRng, RngCore, SeedableRng};
1818

1919
#[cfg(feature = "serde1")] use serde::{Serialize, Deserialize, Serializer, Deserializer};
2020

@@ -101,6 +101,8 @@ macro_rules! chacha_impl {
101101

102102
impl CryptoBlockRng for $ChaChaXCore {}
103103

104+
impl InfallibleRng for $ChaChaXRng {}
105+
104106
/// A cryptographically secure random number generator that uses the ChaCha algorithm.
105107
///
106108
/// ChaCha is a stream cipher designed by Daniel J. Bernstein[^1], that we use as an RNG. It is

rand_core/src/lib.rs

+8
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,14 @@ pub trait RngCore {
212212
/// [`BlockRngCore`]: block::BlockRngCore
213213
pub trait CryptoRng: RngCore {}
214214

215+
/// A marker trait used to indicate that an [`RngCore`] implementation is
216+
/// supposed to never return errors from the [`RngCore::try_fill_bytes`] method
217+
/// and never panic on unwrapping [`Error`] while calling other methods.
218+
///
219+
/// This trait is usually implemented by PRNGs, as opposed to OS, hardware,
220+
/// and periodically-reseeded RNGs, which may fail with IO errors.
221+
pub trait InfallibleRng: RngCore {}
222+
215223
/// A random number generator that can be explicitly seeded.
216224
///
217225
/// This trait encapsulates the low-level functionality common to all

rand_pcg/src/pcg128.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
const MULTIPLIER: u128 = 0x2360_ED05_1FC6_5DA4_4385_DF64_9FCC_F645;
1515

1616
use core::fmt;
17-
use rand_core::{impls, le, Error, RngCore, SeedableRng};
17+
use rand_core::{impls, le, Error, InfallibleRng, RngCore, SeedableRng};
1818
#[cfg(feature = "serde1")] use serde::{Deserialize, Serialize};
1919

2020
/// A PCG random number generator (XSL RR 128/64 (LCG) variant).
@@ -159,6 +159,7 @@ impl RngCore for Lcg128Xsl64 {
159159
}
160160
}
161161

162+
impl InfallibleRng for Lcg128Xsl64 {}
162163

163164
/// A PCG random number generator (XSL 128/64 (MCG) variant).
164165
///
@@ -269,6 +270,8 @@ impl RngCore for Mcg128Xsl64 {
269270
}
270271
}
271272

273+
impl InfallibleRng for Mcg128Xsl64 {}
274+
272275
#[inline(always)]
273276
fn output_xsl_rr(state: u128) -> u64 {
274277
// Output function XSL RR ("xorshift low (bits), random rotation")

rand_pcg/src/pcg128cm.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
const MULTIPLIER: u64 = 15750249268501108917;
1515

1616
use core::fmt;
17-
use rand_core::{impls, le, Error, RngCore, SeedableRng};
17+
use rand_core::{impls, le, Error, InfallibleRng, RngCore, SeedableRng};
1818
#[cfg(feature = "serde1")] use serde::{Deserialize, Serialize};
1919

2020
/// A PCG random number generator (CM DXSM 128/64 (LCG) variant).
@@ -165,6 +165,8 @@ impl RngCore for Lcg128CmDxsm64 {
165165
}
166166
}
167167

168+
impl InfallibleRng for Lcg128CmDxsm64 {}
169+
168170
#[inline(always)]
169171
fn output_dxsm(state: u128) -> u64 {
170172
// See https://github.com/imneme/pcg-cpp/blob/ffd522e7188bef30a00c74dc7eb9de5faff90092/include/pcg_random.hpp#L1016

rand_pcg/src/pcg64.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//! PCG random number generators
1212
1313
use core::fmt;
14-
use rand_core::{impls, le, Error, RngCore, SeedableRng};
14+
use rand_core::{impls, le, Error, InfallibleRng, RngCore, SeedableRng};
1515
#[cfg(feature = "serde1")] use serde::{Deserialize, Serialize};
1616

1717
// This is the default multiplier used by PCG for 64-bit state.
@@ -167,3 +167,5 @@ impl RngCore for Lcg64Xsh32 {
167167
Ok(())
168168
}
169169
}
170+
171+
impl InfallibleRng for Lcg64Xsh32 {}

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ macro_rules! error { ($($x:tt)*) => (
9292
) }
9393

9494
// Re-exports from rand_core
95-
pub use rand_core::{CryptoRng, Error, RngCore, SeedableRng};
95+
pub use rand_core::{CryptoRng, Error, InfallibleRng, RngCore, SeedableRng};
9696

9797
// Public modules
9898
pub mod distributions;

src/rngs/mock.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
//! Mock random number generator
1010
11-
use rand_core::{impls, Error, RngCore};
11+
use rand_core::{impls, Error, InfallibleRng, RngCore};
1212

1313
#[cfg(feature = "serde1")]
1414
use serde::{Serialize, Deserialize};
@@ -81,6 +81,8 @@ impl RngCore for StepRng {
8181
}
8282
}
8383

84+
impl InfallibleRng for StepRng {}
85+
8486
#[cfg(test)]
8587
mod tests {
8688
#[cfg(any(feature = "alloc", feature = "serde1"))]

src/rngs/small.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
//! A small fast RNG
1010
11-
use rand_core::{Error, RngCore, SeedableRng};
11+
use rand_core::{Error, InfallibleRng, RngCore, SeedableRng};
1212

1313
#[cfg(target_pointer_width = "64")]
1414
type Rng = super::xoshiro256plusplus::Xoshiro256PlusPlus;
@@ -64,6 +64,8 @@ impl RngCore for SmallRng {
6464
}
6565
}
6666

67+
impl InfallibleRng for SmallRng {}
68+
6769
impl SmallRng {
6870
/// Construct an instance seeded from another `Rng`
6971
///

src/rngs/std.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
//! The standard RNG
1010
11-
use crate::{CryptoRng, Error, RngCore, SeedableRng};
11+
use rand_core::{CryptoRng, Error, InfallibleRng, RngCore, SeedableRng};
1212

1313
#[cfg(any(test, feature = "getrandom"))]
1414
pub(crate) use rand_chacha::ChaCha12Core as Core;
@@ -73,6 +73,7 @@ impl SeedableRng for StdRng {
7373

7474
impl CryptoRng for StdRng {}
7575

76+
impl InfallibleRng for StdRng {}
7677

7778
#[cfg(test)]
7879
mod test {

src/rngs/xoshiro128plusplus.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#[cfg(feature="serde1")] use serde::{Serialize, Deserialize};
1010
use rand_core::impls::{next_u64_via_u32, fill_bytes_via_next};
1111
use rand_core::le::read_u32_into;
12-
use rand_core::{SeedableRng, RngCore, Error};
12+
use rand_core::{Error, InfallibleRng, RngCore, SeedableRng};
1313

1414
/// A xoshiro128++ random number generator.
1515
///
@@ -97,6 +97,8 @@ impl RngCore for Xoshiro128PlusPlus {
9797
}
9898
}
9999

100+
impl InfallibleRng for Xoshiro128PlusPlus {}
101+
100102
#[cfg(test)]
101103
mod tests {
102104
use super::*;

src/rngs/xoshiro256plusplus.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#[cfg(feature="serde1")] use serde::{Serialize, Deserialize};
1010
use rand_core::impls::fill_bytes_via_next;
1111
use rand_core::le::read_u64_into;
12-
use rand_core::{SeedableRng, RngCore, Error};
12+
use rand_core::{Error, InfallibleRng, RngCore, SeedableRng};
1313

1414
/// A xoshiro256++ random number generator.
1515
///
@@ -99,6 +99,8 @@ impl RngCore for Xoshiro256PlusPlus {
9999
}
100100
}
101101

102+
impl InfallibleRng for Xoshiro256PlusPlus {}
103+
102104
#[cfg(test)]
103105
mod tests {
104106
use super::*;

0 commit comments

Comments
 (0)