Skip to content

Commit 8f95967

Browse files
committed
Inline fill_via_chunks
1 parent 3c168c1 commit 8f95967

File tree

2 files changed

+23
-75
lines changed

2 files changed

+23
-75
lines changed

src/block.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
//! [`SeedableRng`]: crate::SeedableRng
8282
//! [`rand::rngs::ReseedingRng`]: https://docs.rs/rand/latest/rand/rngs/struct.ReseedingRng.html
8383
84-
use crate::le::{Word, fill_via_chunks};
84+
use crate::le::Word;
8585
use core::fmt;
8686

8787
/// A random (block) generator
@@ -292,11 +292,29 @@ impl<W: Word, const N: usize, G: Generator<Output = [W; N]>> BlockRng<G> {
292292
self.core.generate(&mut self.results);
293293
index = 0;
294294
}
295-
let (consumed_u32, filled_u8) =
296-
fill_via_chunks(&self.results[index..], &mut dest[read_len..]);
297295

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

src/le.rs

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -65,36 +65,6 @@ pub fn fill_bytes_via_next_word<W: Word>(dst: &mut [u8], mut next_word: impl FnM
6565
}
6666
}
6767

68-
/// Fill dest from src
69-
///
70-
/// Returns `(n, byte_len)`. `src[..n]` is consumed,
71-
/// `dest[..byte_len]` is filled. `src[n..]` and `dest[byte_len..]` are left
72-
/// unaltered.
73-
pub(crate) fn fill_via_chunks<T: Word>(src: &[T], dest: &mut [u8]) -> (usize, usize) {
74-
let size = core::mem::size_of::<T>();
75-
76-
// Always use little endian for portability of results.
77-
78-
let mut dest = dest.chunks_exact_mut(size);
79-
let mut src = src.iter();
80-
81-
let zipped = dest.by_ref().zip(src.by_ref());
82-
let num_chunks = zipped.len();
83-
zipped.for_each(|(dest, src)| dest.copy_from_slice(src.to_le_bytes().as_ref()));
84-
85-
let byte_len = num_chunks * size;
86-
if let Some(src) = src.next() {
87-
// We have consumed all full chunks of dest, but not src.
88-
let dest = dest.into_remainder();
89-
let n = dest.len();
90-
if n > 0 {
91-
dest.copy_from_slice(&src.to_le_bytes().as_ref()[..n]);
92-
return (num_chunks + 1, byte_len + n);
93-
}
94-
}
95-
(num_chunks, byte_len)
96-
}
97-
9868
/// Yield a word using [`RngCore::fill_bytes`]
9969
///
10070
/// This may be used to implement `next_u32` or `next_u64`.
@@ -128,46 +98,6 @@ pub fn read_words<W: Word, const N: usize>(src: &[u8]) -> [W; N] {
12898
mod test {
12999
use super::*;
130100

131-
#[test]
132-
fn test_fill_via_u32_chunks() {
133-
let src_orig = [1u32, 2, 3];
134-
135-
let src = src_orig;
136-
let mut dst = [0u8; 11];
137-
assert_eq!(fill_via_chunks(&src, &mut dst), (3, 11));
138-
assert_eq!(dst, [1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0]);
139-
140-
let src = src_orig;
141-
let mut dst = [0u8; 13];
142-
assert_eq!(fill_via_chunks(&src, &mut dst), (3, 12));
143-
assert_eq!(dst, [1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 0]);
144-
145-
let src = src_orig;
146-
let mut dst = [0u8; 5];
147-
assert_eq!(fill_via_chunks(&src, &mut dst), (2, 5));
148-
assert_eq!(dst, [1, 0, 0, 0, 2]);
149-
}
150-
151-
#[test]
152-
fn test_fill_via_u64_chunks() {
153-
let src_orig = [1u64, 2];
154-
155-
let src = src_orig;
156-
let mut dst = [0u8; 11];
157-
assert_eq!(fill_via_chunks(&src, &mut dst), (2, 11));
158-
assert_eq!(dst, [1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0]);
159-
160-
let src = src_orig;
161-
let mut dst = [0u8; 17];
162-
assert_eq!(fill_via_chunks(&src, &mut dst), (2, 16));
163-
assert_eq!(dst, [1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0]);
164-
165-
let src = src_orig;
166-
let mut dst = [0u8; 5];
167-
assert_eq!(fill_via_chunks(&src, &mut dst), (1, 5));
168-
assert_eq!(dst, [1, 0, 0, 0, 0]);
169-
}
170-
171101
#[test]
172102
fn test_read() {
173103
let bytes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];

0 commit comments

Comments
 (0)