Skip to content

Commit 3c168c1

Browse files
committed
Replace fns read_u{32,64}_into with read_words
1 parent 22797a6 commit 3c168c1

File tree

3 files changed

+21
-44
lines changed

3 files changed

+21
-44
lines changed

src/block.rs

Lines changed: 1 addition & 5 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::le::read_words(&seed),
4743
//! };
4844
//! MyRng(BlockRng::new(core))
4945
//! }

src/le.rs

Lines changed: 19 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,8 @@
2929
//!
3030
//! ### Implementing [`SeedableRng`]
3131
//!
32-
//! In many cases, [`SeedableRng::Seed`] must be converted to `[u32]` or
33-
//! `[u64]`. The following helpers are provided:
34-
//!
35-
//! - [`read_u32_into`]
36-
//! - [`read_u64_into`]
32+
//! In many cases, [`SeedableRng::Seed`] must be converted to `[u32; _]` or
33+
//! `[u64; _]`. [`read_words`] may be used for this.
3734
3835
use crate::RngCore;
3936
#[allow(unused)]
@@ -107,35 +104,24 @@ pub fn next_word_via_fill<W: Word, R: RngCore + ?Sized>(rng: &mut R) -> W {
107104
W::from_le_bytes(buf)
108105
}
109106

110-
/// Fills `dst: &mut [u32]` from `src`
107+
/// Reads an array of words from a byte slice
111108
///
112-
/// Reads use Little-Endian byte order, allowing portable reproduction of `dst`
113-
/// from a byte slice.
109+
/// Words are read from `src` in order, using LE conversion from bytes.
114110
///
115111
/// # Panics
116112
///
117-
/// If `src` has insufficient length (if `src.len() < 4*dst.len()`).
118-
#[inline]
119-
#[track_caller]
120-
pub fn read_u32_into(src: &[u8], dst: &mut [u32]) {
121-
assert!(src.len() >= 4 * dst.len());
122-
for (out, chunk) in dst.iter_mut().zip(src.chunks_exact(4)) {
123-
*out = u32::from_le_bytes(chunk.try_into().unwrap());
124-
}
125-
}
126-
127-
/// Fills `dst: &mut [u64]` from `src`
128-
///
129-
/// # Panics
130-
///
131-
/// If `src` has insufficient length (if `src.len() < 8*dst.len()`).
132-
#[inline]
133-
#[track_caller]
134-
pub fn read_u64_into(src: &[u8], dst: &mut [u64]) {
135-
assert!(src.len() >= 8 * dst.len());
136-
for (out, chunk) in dst.iter_mut().zip(src.chunks_exact(8)) {
137-
*out = u64::from_le_bytes(chunk.try_into().unwrap());
113+
/// Panics if `size_of_val(src) != size_of::<[W; N]>()`.
114+
#[inline(always)]
115+
pub fn read_words<W: Word, const N: usize>(src: &[u8]) -> [W; N] {
116+
assert_eq!(size_of_val(src), size_of::<[W; N]>());
117+
let mut dst = [W::from_usize(0); N];
118+
let chunks = src.chunks_exact(size_of::<W>());
119+
for (out, chunk) in dst.iter_mut().zip(chunks) {
120+
let mut buf: W::Bytes = Default::default();
121+
buf.as_mut().copy_from_slice(chunk);
122+
*out = W::from_le_bytes(buf);
138123
}
124+
dst
139125
}
140126

141127
#[cfg(test)]
@@ -186,23 +172,19 @@ mod test {
186172
fn test_read() {
187173
let bytes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
188174

189-
let mut buf = [0u32; 4];
190-
read_u32_into(&bytes, &mut buf);
175+
let buf: [u32; 4] = read_words(&bytes);
191176
assert_eq!(buf[0], 0x04030201);
192177
assert_eq!(buf[3], 0x100F0E0D);
193178

194-
let mut buf = [0u32; 3];
195-
read_u32_into(&bytes[1..13], &mut buf); // unaligned
179+
let buf: [u32; 3] = read_words(&bytes[1..13]); // unaligned
196180
assert_eq!(buf[0], 0x05040302);
197181
assert_eq!(buf[2], 0x0D0C0B0A);
198182

199-
let mut buf = [0u64; 2];
200-
read_u64_into(&bytes, &mut buf);
183+
let buf: [u64; 2] = read_words(&bytes);
201184
assert_eq!(buf[0], 0x0807060504030201);
202185
assert_eq!(buf[1], 0x100F0E0D0C0B0A09);
203186

204-
let mut buf = [0u64; 1];
205-
read_u64_into(&bytes[7..15], &mut buf); // unaligned
187+
let buf: [u64; 1] = read_words(&bytes[7..15]); // unaligned
206188
assert_eq!(buf[0], 0x0F0E0D0C0B0A0908);
207189
}
208190
}

src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,8 +507,7 @@ mod test {
507507
type Seed = [u8; 8];
508508

509509
fn from_seed(seed: Self::Seed) -> Self {
510-
let mut x = [0u64; 1];
511-
le::read_u64_into(&seed, &mut x);
510+
let x: [u64; 1] = le::read_words(&seed);
512511
SeedableNum(x[0])
513512
}
514513
}

0 commit comments

Comments
 (0)