Skip to content

Commit 6830af0

Browse files
committed
Update rand
1 parent 6a5006b commit 6830af0

8 files changed

Lines changed: 86 additions & 62 deletions

File tree

src/lib.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ pub fn fill<T: Fill>(dest: &mut [T]) {
334334
#[cfg(test)]
335335
mod test {
336336
use super::*;
337+
use core::convert::Infallible;
337338

338339
/// Construct a deterministic RNG with the given seed
339340
pub fn rng(seed: u64) -> impl RngCore {
@@ -355,19 +356,21 @@ mod test {
355356

356357
#[derive(Clone)]
357358
pub struct StepRng(u64, u64);
358-
impl RngCore for StepRng {
359-
fn next_u32(&mut self) -> u32 {
360-
self.next_u64() as u32
359+
impl TryRngCore for StepRng {
360+
type Error = Infallible;
361+
362+
fn try_next_u32(&mut self) -> Result<u32, Infallible> {
363+
self.try_next_u64().map(|x| x as u32)
361364
}
362365

363-
fn next_u64(&mut self) -> u64 {
366+
fn try_next_u64(&mut self) -> Result<u64, Infallible> {
364367
let res = self.0;
365368
self.0 = self.0.wrapping_add(self.1);
366-
res
369+
Ok(res)
367370
}
368371

369-
fn fill_bytes(&mut self, dst: &mut [u8]) {
370-
rand_core::utils::fill_bytes_via_next_word(dst, || self.next_u64());
372+
fn try_fill_bytes(&mut self, dst: &mut [u8]) -> Result<(), Infallible> {
373+
rand_core::utils::fill_bytes_via_next_word(dst, || self.try_next_u64())
371374
}
372375
}
373376

src/rngs/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@
6868
//!
6969
//! ## Traits and functionality
7070
//!
71-
//! All generators implement [`RngCore`] and thus also [`Rng`][crate::Rng].
71+
//! All generators implement [`TryRngCore`]. Most implement [`RngCore`] (i.e.
72+
//! `TryRngCore<Error = Infallible>`) and thus also implement [`Rng`][crate::Rng].
7273
//! See also the [Random Values] chapter in the book.
7374
//!
7475
//! Secure RNGs may additionally implement the [`CryptoRng`] trait.
@@ -82,6 +83,7 @@
8283
//! [Our RNGs]: https://rust-random.github.io/book/guide-rngs.html
8384
//! [Random Values]: https://rust-random.github.io/book/guide-values.html
8485
//! [`Rng`]: crate::Rng
86+
//! [`TryRngCore`]: crate::TryRngCore
8587
//! [`RngCore`]: crate::RngCore
8688
//! [`CryptoRng`]: crate::CryptoRng
8789
//! [`SeedableRng`]: crate::SeedableRng

src/rngs/reseeding.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
//! A wrapper around another PRNG that reseeds it after it
1111
//! generates a certain number of random bytes.
1212
13+
use core::convert::Infallible;
1314
use core::mem::size_of_val;
1415

1516
use rand_core::block::{BlockRng, CryptoGenerator, Generator};
16-
use rand_core::{CryptoRng, RngCore, SeedableRng, TryCryptoRng, TryRngCore};
17+
use rand_core::{SeedableRng, TryCryptoRng, TryRngCore};
1718

1819
/// A wrapper around any PRNG that implements [`Generator`], that adds the
1920
/// ability to reseed it.
@@ -93,34 +94,37 @@ where
9394
///
9495
/// This discards any remaining random data in the cache.
9596
pub fn reseed(&mut self) -> Result<(), Rsdr::Error> {
96-
self.0.reset();
97+
self.0.reset_and_skip(0);
9798
self.0.core.reseed()
9899
}
99100
}
100101

101102
// TODO: this should be implemented for any type where the inner type
102-
// implements RngCore, but we can't specify that because ReseedingCore is private
103-
impl<const N: usize, G, Rsdr> RngCore for ReseedingRng<G, Rsdr>
103+
// implements TryRngCore, but we can't specify that because ReseedingCore is private
104+
impl<const N: usize, G, Rsdr> TryRngCore for ReseedingRng<G, Rsdr>
104105
where
105106
G: Generator<Output = [u32; N]> + SeedableRng,
106107
Rsdr: TryRngCore,
107108
{
109+
type Error = Infallible;
110+
108111
#[inline(always)]
109-
fn next_u32(&mut self) -> u32 {
110-
self.0.next_word()
112+
fn try_next_u32(&mut self) -> Result<u32, Infallible> {
113+
Ok(self.0.next_word())
111114
}
112115

113116
#[inline(always)]
114-
fn next_u64(&mut self) -> u64 {
115-
self.0.next_u64_from_u32()
117+
fn try_next_u64(&mut self) -> Result<u64, Infallible> {
118+
Ok(self.0.next_u64_from_u32())
116119
}
117120

118-
fn fill_bytes(&mut self, dest: &mut [u8]) {
119-
self.0.fill_bytes(dest)
121+
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Infallible> {
122+
self.0.fill_bytes(dest);
123+
Ok(())
120124
}
121125
}
122126

123-
impl<const N: usize, G, Rsdr> CryptoRng for ReseedingRng<G, Rsdr>
127+
impl<const N: usize, G, Rsdr> TryCryptoRng for ReseedingRng<G, Rsdr>
124128
where
125129
G: Generator<Output = [u32; N]> + SeedableRng + CryptoGenerator,
126130
Rsdr: TryCryptoRng,

src/rngs/small.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
//! A small fast RNG
1010
11-
use rand_core::{RngCore, SeedableRng};
11+
use core::convert::Infallible;
12+
use rand_core::{SeedableRng, TryRngCore};
1213

1314
#[cfg(any(target_pointer_width = "32", target_pointer_width = "16"))]
1415
type Rng = super::xoshiro128plusplus::Xoshiro128PlusPlus;
@@ -73,6 +74,7 @@ type Rng = super::xoshiro256plusplus::Xoshiro256PlusPlus;
7374
/// [rand_xoshiro]: https://crates.io/crates/rand_xoshiro
7475
/// [`rand_chacha::ChaCha8Rng`]: https://docs.rs/rand_chacha/latest/rand_chacha/struct.ChaCha8Rng.html
7576
/// [`rand_seeder`]: https://docs.rs/rand_seeder/latest/rand_seeder/
77+
/// [`RngCore`]: rand_core::RngCore
7678
#[derive(Clone, Debug, PartialEq, Eq)]
7779
pub struct SmallRng(Rng);
7880

@@ -95,19 +97,21 @@ impl SeedableRng for SmallRng {
9597
}
9698
}
9799

98-
impl RngCore for SmallRng {
100+
impl TryRngCore for SmallRng {
101+
type Error = Infallible;
102+
99103
#[inline(always)]
100-
fn next_u32(&mut self) -> u32 {
101-
self.0.next_u32()
104+
fn try_next_u32(&mut self) -> Result<u32, Infallible> {
105+
self.0.try_next_u32()
102106
}
103107

104108
#[inline(always)]
105-
fn next_u64(&mut self) -> u64 {
106-
self.0.next_u64()
109+
fn try_next_u64(&mut self) -> Result<u64, Infallible> {
110+
self.0.try_next_u64()
107111
}
108112

109113
#[inline(always)]
110-
fn fill_bytes(&mut self, dest: &mut [u8]) {
111-
self.0.fill_bytes(dest)
114+
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Infallible> {
115+
self.0.try_fill_bytes(dest)
112116
}
113117
}

src/rngs/std.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
//! The standard RNG
1010
11-
use rand_core::{CryptoRng, RngCore, SeedableRng};
11+
use core::convert::Infallible;
12+
use rand_core::{SeedableRng, TryCryptoRng, TryRngCore};
1213

1314
#[cfg(any(test, feature = "sys_rng"))]
1415
pub(crate) use chacha20::ChaCha12Core as Core;
@@ -66,23 +67,26 @@ use chacha20::ChaCha12Rng as Rng;
6667
/// [CSPRNG]: https://rust-random.github.io/book/guide-gen.html#cryptographically-secure-pseudo-random-number-generator
6768
/// [rand_chacha]: https://crates.io/crates/rand_chacha
6869
/// [rand issue]: https://github.com/rust-random/rand/issues/932
70+
/// [`RngCore`]: rand_core::RngCore
6971
#[derive(Debug, PartialEq, Eq)]
7072
pub struct StdRng(Rng);
7173

72-
impl RngCore for StdRng {
74+
impl TryRngCore for StdRng {
75+
type Error = Infallible;
76+
7377
#[inline(always)]
74-
fn next_u32(&mut self) -> u32 {
75-
self.0.next_u32()
78+
fn try_next_u32(&mut self) -> Result<u32, Infallible> {
79+
self.0.try_next_u32()
7680
}
7781

7882
#[inline(always)]
79-
fn next_u64(&mut self) -> u64 {
80-
self.0.next_u64()
83+
fn try_next_u64(&mut self) -> Result<u64, Infallible> {
84+
self.0.try_next_u64()
8185
}
8286

8387
#[inline(always)]
84-
fn fill_bytes(&mut self, dst: &mut [u8]) {
85-
self.0.fill_bytes(dst)
88+
fn try_fill_bytes(&mut self, dst: &mut [u8]) -> Result<(), Infallible> {
89+
self.0.try_fill_bytes(dst)
8690
}
8791
}
8892

@@ -96,7 +100,7 @@ impl SeedableRng for StdRng {
96100
}
97101
}
98102

99-
impl CryptoRng for StdRng {}
103+
impl TryCryptoRng for StdRng {}
100104

101105
#[cfg(test)]
102106
mod test {

src/rngs/thread.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99
//! Thread-local random number generator
1010
11-
use core::cell::UnsafeCell;
11+
use core::{cell::UnsafeCell, convert::Infallible};
1212
use std::fmt;
1313
use std::rc::Rc;
1414
use std::thread_local;
1515

1616
use super::{ReseedingRng, SysError, SysRng, std::Core};
17-
use rand_core::{CryptoRng, RngCore};
17+
use rand_core::{TryCryptoRng, TryRngCore};
1818

1919
// Rationale for using `UnsafeCell` in `ThreadRng`:
2020
//
@@ -162,33 +162,35 @@ impl Default for ThreadRng {
162162
}
163163
}
164164

165-
impl RngCore for ThreadRng {
165+
impl TryRngCore for ThreadRng {
166+
type Error = Infallible;
167+
166168
#[inline(always)]
167-
fn next_u32(&mut self) -> u32 {
169+
fn try_next_u32(&mut self) -> Result<u32, Infallible> {
168170
// SAFETY: We must make sure to stop using `rng` before anyone else
169171
// creates another mutable reference
170172
let rng = unsafe { &mut *self.rng.get() };
171-
rng.next_u32()
173+
rng.try_next_u32()
172174
}
173175

174176
#[inline(always)]
175-
fn next_u64(&mut self) -> u64 {
177+
fn try_next_u64(&mut self) -> Result<u64, Infallible> {
176178
// SAFETY: We must make sure to stop using `rng` before anyone else
177179
// creates another mutable reference
178180
let rng = unsafe { &mut *self.rng.get() };
179-
rng.next_u64()
181+
rng.try_next_u64()
180182
}
181183

182184
#[inline(always)]
183-
fn fill_bytes(&mut self, dest: &mut [u8]) {
185+
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Infallible> {
184186
// SAFETY: We must make sure to stop using `rng` before anyone else
185187
// creates another mutable reference
186188
let rng = unsafe { &mut *self.rng.get() };
187-
rng.fill_bytes(dest)
189+
rng.try_fill_bytes(dest)
188190
}
189191
}
190192

191-
impl CryptoRng for ThreadRng {}
193+
impl TryCryptoRng for ThreadRng {}
192194

193195
#[cfg(test)]
194196
mod test {

src/rngs/xoshiro128plusplus.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
use rand_core::{RngCore, SeedableRng, utils};
9+
use core::convert::Infallible;
10+
use rand_core::{SeedableRng, TryRngCore, utils};
1011
#[cfg(feature = "serde")]
1112
use serde::{Deserialize, Serialize};
1213

@@ -63,9 +64,11 @@ impl SeedableRng for Xoshiro128PlusPlus {
6364
}
6465
}
6566

66-
impl RngCore for Xoshiro128PlusPlus {
67+
impl TryRngCore for Xoshiro128PlusPlus {
68+
type Error = Infallible;
69+
6770
#[inline]
68-
fn next_u32(&mut self) -> u32 {
71+
fn try_next_u32(&mut self) -> Result<u32, Infallible> {
6972
let res = self.s[0]
7073
.wrapping_add(self.s[3])
7174
.rotate_left(7)
@@ -82,17 +85,17 @@ impl RngCore for Xoshiro128PlusPlus {
8285

8386
self.s[3] = self.s[3].rotate_left(11);
8487

85-
res
88+
Ok(res)
8689
}
8790

8891
#[inline]
89-
fn next_u64(&mut self) -> u64 {
92+
fn try_next_u64(&mut self) -> Result<u64, Infallible> {
9093
utils::next_u64_via_u32(self)
9194
}
9295

9396
#[inline]
94-
fn fill_bytes(&mut self, dst: &mut [u8]) {
95-
utils::fill_bytes_via_next_word(dst, || self.next_u32());
97+
fn try_fill_bytes(&mut self, dst: &mut [u8]) -> Result<(), Infallible> {
98+
utils::fill_bytes_via_next_word(dst, || self.try_next_u32())
9699
}
97100
}
98101

src/rngs/xoshiro256plusplus.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
use rand_core::{RngCore, SeedableRng, utils};
9+
use core::convert::Infallible;
10+
use rand_core::{SeedableRng, TryRngCore, utils};
1011
#[cfg(feature = "serde")]
1112
use serde::{Deserialize, Serialize};
1213

@@ -62,17 +63,18 @@ impl SeedableRng for Xoshiro256PlusPlus {
6263
}
6364
}
6465

65-
impl RngCore for Xoshiro256PlusPlus {
66+
impl TryRngCore for Xoshiro256PlusPlus {
67+
type Error = Infallible;
68+
6669
#[inline]
67-
fn next_u32(&mut self) -> u32 {
70+
fn try_next_u32(&mut self) -> Result<u32, Infallible> {
6871
// The lowest bits have some linear dependencies, so we use the
6972
// upper bits instead.
70-
let val = self.next_u64();
71-
(val >> 32) as u32
73+
self.try_next_u64().map(|val| (val >> 32) as u32)
7274
}
7375

7476
#[inline]
75-
fn next_u64(&mut self) -> u64 {
77+
fn try_next_u64(&mut self) -> Result<u64, Infallible> {
7678
let res = self.s[0]
7779
.wrapping_add(self.s[3])
7880
.rotate_left(23)
@@ -89,12 +91,12 @@ impl RngCore for Xoshiro256PlusPlus {
8991

9092
self.s[3] = self.s[3].rotate_left(45);
9193

92-
res
94+
Ok(res)
9395
}
9496

9597
#[inline]
96-
fn fill_bytes(&mut self, dst: &mut [u8]) {
97-
utils::fill_bytes_via_next_word(dst, || self.next_u64());
98+
fn try_fill_bytes(&mut self, dst: &mut [u8]) -> Result<(), Infallible> {
99+
utils::fill_bytes_via_next_word(dst, || self.try_next_u64())
98100
}
99101
}
100102

0 commit comments

Comments
 (0)