Skip to content

Commit 29b1630

Browse files
authored
Merge pull request #38 from rust-random/utils
Replace mod `le` with `utils`, replacing some fns
2 parents 8101761 + d89a82f commit 29b1630

File tree

6 files changed

+270
-281
lines changed

6 files changed

+270
-281
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2121
- Remove impl of `RngCore` for `BlockRng`, making the latter more generic ([#34])
2222
- Add trait `le::Word` ([#34])
2323
- Add fn `BlockRng::reconstruct` and fn `BlockRng::remaining_results` ([#36])
24+
- Move `le::{Word, next_u64_via_u32}` to new `utils` module; remove `le` ([#38])
25+
- Replace `le::fill_bytes_via_next` with `utils::fill_bytes_via_next_word` ([#38])
26+
- Replace `le::next_u32_via_fill` and `le::next_u64_via_fill` with `utils::next_word_via_fill` ([#38])
27+
- Replace `le::read_u32_into` and `le::read_u64_into` with `utils::read_words` ([#38])
2428

2529
### Other
2630
- Changed repository from [rust-random/rand] to [rust-random/core].
@@ -38,6 +42,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3842
[#34]: https://github.com/rust-random/rand-core/pull/34
3943
[#35]: https://github.com/rust-random/rand-core/pull/35
4044
[#36]: https://github.com/rust-random/rand-core/pull/36
45+
[#38]: https://github.com/rust-random/rand-core/pull/38
4146

4247
[rust-random/rand]: https://github.com/rust-random/rand
4348
[rust-random/core]: https://github.com/rust-random/core

src/block.rs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,7 @@
3939
//! fn from_seed(seed: Self::Seed) -> Self {
4040
//! let core = MyRngCore {
4141
//! // ...
42-
//! # state: {
43-
//! # let mut buf = [0u32; 8];
44-
//! # rand_core::le::read_u32_into(&seed, &mut buf);
45-
//! # buf
46-
//! # }
42+
//! # state: rand_core::utils::read_words(&seed),
4743
//! };
4844
//! MyRng(BlockRng::new(core))
4945
//! }
@@ -85,7 +81,7 @@
8581
//! [`SeedableRng`]: crate::SeedableRng
8682
//! [`rand::rngs::ReseedingRng`]: https://docs.rs/rand/latest/rand/rngs/struct.ReseedingRng.html
8783
88-
use crate::le::{Word, fill_via_chunks};
84+
use crate::utils::Word;
8985
use core::fmt;
9086

9187
/// A random (block) generator
@@ -141,15 +137,14 @@ pub struct BlockRng<G: Generator> {
141137
}
142138

143139
// Custom Debug implementation that does not expose the contents of `results`.
144-
impl<W: Word, const N: usize, G> fmt::Debug for BlockRng<G>
140+
impl<G> fmt::Debug for BlockRng<G>
145141
where
146-
G: Generator<Output = [W; N]> + fmt::Debug,
142+
G: Generator + fmt::Debug,
147143
{
148144
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
149145
fmt.debug_struct("BlockRng")
150146
.field("core", &self.core)
151-
.field("index", &self.index())
152-
.finish()
147+
.finish_non_exhaustive()
153148
}
154149
}
155150

@@ -296,11 +291,29 @@ impl<W: Word, const N: usize, G: Generator<Output = [W; N]>> BlockRng<G> {
296291
self.core.generate(&mut self.results);
297292
index = 0;
298293
}
299-
let (consumed_u32, filled_u8) =
300-
fill_via_chunks(&self.results[index..], &mut dest[read_len..]);
301294

302-
self.set_index(index + consumed_u32);
303-
read_len += filled_u8;
295+
let size = core::mem::size_of::<W>();
296+
let mut chunks = dest[read_len..].chunks_exact_mut(size);
297+
let mut src = self.results[index..].iter();
298+
299+
let zipped = chunks.by_ref().zip(src.by_ref());
300+
let num_chunks = zipped.len();
301+
zipped.for_each(|(chunk, src)| chunk.copy_from_slice(src.to_le_bytes().as_ref()));
302+
index += num_chunks;
303+
read_len += num_chunks * size;
304+
305+
if let Some(src) = src.next() {
306+
// We have consumed all full chunks of dest, but not src.
307+
let dest = chunks.into_remainder();
308+
let n = dest.len();
309+
if n > 0 {
310+
dest.copy_from_slice(&src.to_le_bytes().as_ref()[..n]);
311+
index += 1;
312+
read_len += n;
313+
}
314+
}
315+
316+
self.set_index(index);
304317
}
305318
}
306319
}

src/le.rs

Lines changed: 0 additions & 263 deletions
This file was deleted.

src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
use core::{fmt, ops::DerefMut};
1717

1818
pub mod block;
19-
pub mod le;
19+
pub mod utils;
20+
mod word;
2021

2122
/// Implementation-level interface for RNGs
2223
///
@@ -50,7 +51,7 @@ pub mod le;
5051
///
5152
/// Typically an RNG will implement only one of the methods available
5253
/// in this trait directly, then use the helper functions from the
53-
/// [`le` module](crate::le) to implement the other methods.
54+
/// [`utils`] module to implement the other methods.
5455
///
5556
/// Note that implementors of [`RngCore`] also automatically implement
5657
/// the [`TryRngCore`] trait with the `Error` associated type being
@@ -506,8 +507,7 @@ mod test {
506507
type Seed = [u8; 8];
507508

508509
fn from_seed(seed: Self::Seed) -> Self {
509-
let mut x = [0u64; 1];
510-
le::read_u64_into(&seed, &mut x);
510+
let x: [u64; 1] = utils::read_words(&seed);
511511
SeedableNum(x[0])
512512
}
513513
}

0 commit comments

Comments
 (0)