Skip to content

Commit f314152

Browse files
committed
migrate rand
1 parent 085c698 commit f314152

File tree

5 files changed

+29
-5
lines changed

5 files changed

+29
-5
lines changed

src/rngs/adapter/read.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ impl<R: Read> ReadRng<R> {
5656
}
5757

5858
impl<R: Read> RngCore for ReadRng<R> {
59+
fn next_bool(&mut self) -> bool {
60+
let mut buf = [0; 1];
61+
self.fill_bytes(&mut buf);
62+
(buf[0] & 1) == 1
63+
}
64+
5965
fn next_u32(&mut self) -> u32 {
6066
impls::next_u32_via_fill(self)
6167
}

src/rngs/adapter/reseeding.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,13 @@ where
106106
// implements RngCore, but we can't specify that because ReseedingCore is private
107107
impl<R, Rsdr: RngCore> RngCore for ReseedingRng<R, Rsdr>
108108
where
109-
R: BlockRngCore<Item = u32> + SeedableRng,
110-
<R as BlockRngCore>::Results: AsRef<[u32]> + AsMut<[u32]>,
109+
R: BlockRngCore + SeedableRng
111110
{
111+
#[inline(always)]
112+
fn next_bool(&mut self) -> bool {
113+
self.0.next_bool()
114+
}
115+
112116
#[inline(always)]
113117
fn next_u32(&mut self) -> u32 {
114118
self.0.next_u32()
@@ -161,7 +165,6 @@ where
161165
R: BlockRngCore + SeedableRng,
162166
Rsdr: RngCore,
163167
{
164-
type Item = <R as BlockRngCore>::Item;
165168
type Results = <R as BlockRngCore>::Results;
166169

167170
fn generate(&mut self, results: &mut Self::Results) {
@@ -172,7 +175,7 @@ where
172175
// returning from a non-inlined function.
173176
return self.reseed_and_generate(results, global_fork_counter);
174177
}
175-
let num_bytes = results.as_ref().len() * size_of::<Self::Item>();
178+
let num_bytes = results.as_ref().len();
176179
self.bytes_until_reseed -= num_bytes as i64;
177180
self.inner.generate(results);
178181
}
@@ -242,7 +245,7 @@ where
242245
trace!("Reseeding RNG (periodic reseed)");
243246
}
244247

245-
let num_bytes = results.as_ref().len() * size_of::<<R as BlockRngCore>::Item>();
248+
let num_bytes = results.as_ref().len();
246249

247250
if let Err(e) = self.reseed() {
248251
warn!("Reseeding RNG failed: {}", e);

src/rngs/mock.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ impl StepRng {
4646
}
4747

4848
impl RngCore for StepRng {
49+
#[inline]
50+
fn next_bool(&mut self) -> bool {
51+
(self.next_u64() & 1) == 1
52+
}
53+
4954
#[inline]
5055
fn next_u32(&mut self) -> u32 {
5156
self.next_u64() as u32

src/rngs/std.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ pub(crate) use rand_hc::Hc128Core as Core;
3737
pub struct StdRng(Rng);
3838

3939
impl RngCore for StdRng {
40+
#[inline(always)]
41+
fn next_bool(&mut self) -> bool {
42+
self.0.next_bool()
43+
}
44+
4045
#[inline(always)]
4146
fn next_u32(&mut self) -> u32 {
4247
self.0.next_u32()

src/rngs/thread.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ impl Default for ThreadRng {
9393
}
9494

9595
impl RngCore for ThreadRng {
96+
#[inline(always)]
97+
fn next_bool(&mut self) -> bool {
98+
unsafe { self.rng.as_mut().next_bool() }
99+
}
100+
96101
#[inline(always)]
97102
fn next_u32(&mut self) -> u32 {
98103
unsafe { self.rng.as_mut().next_u32() }

0 commit comments

Comments
 (0)