Skip to content
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Remove impl of `RngCore` for `BlockRng`, making the latter more generic ([#34])
- Add trait `le::Word` ([#34])
- Add fn `BlockRng::reconstruct` and fn `BlockRng::remaining_results` ([#36])
- Move `le::{Word, next_u64_via_u32}` to new `utils` module; remove `le` ([#38])
- Replace `le::fill_bytes_via_next` with `utils::fill_bytes_via_next_word` ([#38])
- Replace `le::next_u32_via_fill` and `le::next_u64_via_fill` with `utils::next_word_via_fill` ([#38])
- Replace `le::read_u32_into` and `le::read_u64_into` with `utils::read_words` ([#38])

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

[rust-random/rand]: https://github.com/rust-random/rand
[rust-random/core]: https://github.com/rust-random/core
Expand Down
41 changes: 27 additions & 14 deletions src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,7 @@
//! fn from_seed(seed: Self::Seed) -> Self {
//! let core = MyRngCore {
//! // ...
//! # state: {
//! # let mut buf = [0u32; 8];
//! # rand_core::le::read_u32_into(&seed, &mut buf);
//! # buf
//! # }
//! # state: rand_core::utils::read_words(&seed),
//! };
//! MyRng(BlockRng::new(core))
//! }
Expand Down Expand Up @@ -85,7 +81,7 @@
//! [`SeedableRng`]: crate::SeedableRng
//! [`rand::rngs::ReseedingRng`]: https://docs.rs/rand/latest/rand/rngs/struct.ReseedingRng.html

use crate::le::{Word, fill_via_chunks};
use crate::utils::Word;
use core::fmt;

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

// Custom Debug implementation that does not expose the contents of `results`.
impl<W: Word, const N: usize, G> fmt::Debug for BlockRng<G>
impl<G> fmt::Debug for BlockRng<G>
where
G: Generator<Output = [W; N]> + fmt::Debug,
G: Generator + fmt::Debug,
{
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("BlockRng")
.field("core", &self.core)
.field("index", &self.index())
.finish()
.finish_non_exhaustive()
}
}

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

self.set_index(index + consumed_u32);
read_len += filled_u8;
let size = core::mem::size_of::<W>();
let mut chunks = dest[read_len..].chunks_exact_mut(size);
let mut src = self.results[index..].iter();

let zipped = chunks.by_ref().zip(src.by_ref());
let num_chunks = zipped.len();
zipped.for_each(|(chunk, src)| chunk.copy_from_slice(src.to_le_bytes().as_ref()));
index += num_chunks;
read_len += num_chunks * size;

if let Some(src) = src.next() {
// We have consumed all full chunks of dest, but not src.
let dest = chunks.into_remainder();
let n = dest.len();
if n > 0 {
dest.copy_from_slice(&src.to_le_bytes().as_ref()[..n]);
index += 1;
read_len += n;
}
}

self.set_index(index);
}
}
}
Expand Down
263 changes: 0 additions & 263 deletions src/le.rs

This file was deleted.

8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
use core::{fmt, ops::DerefMut};

pub mod block;
pub mod le;
pub mod utils;
mod word;

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

fn from_seed(seed: Self::Seed) -> Self {
let mut x = [0u64; 1];
le::read_u64_into(&seed, &mut x);
let x: [u64; 1] = utils::read_words(&seed);
SeedableNum(x[0])
}
}
Expand Down
Loading