Skip to content

Commit 22797a6

Browse files
committed
Replace fns next_u{32,64}_via_fill with next_word_via_fill
1 parent 27004ac commit 22797a6

File tree

2 files changed

+10
-15
lines changed

2 files changed

+10
-15
lines changed

src/le.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
//! **`fn next_u32`:**
1919
//! - `self.next_u64() as u32`
2020
//! - `(self.next_u64() >> 32) as u32`
21-
//! - <code>[next_u32_via_fill][](self)</code>
21+
//! - <code>[next_word_via_fill][](self)</code>
2222
//!
2323
//! **`fn next_u64`:**
2424
//! - <code>[next_u64_via_u32][](self)</code>
25-
//! - <code>[next_u64_via_fill][](self)</code>
25+
//! - <code>[next_word_via_fill][](self)</code>
2626
//!
2727
//! **`fn fill_bytes`:**
2828
//! - <code>[fill_bytes_via_next_word][](self, dest)</code>
@@ -98,18 +98,13 @@ pub(crate) fn fill_via_chunks<T: Word>(src: &[T], dest: &mut [u8]) -> (usize, us
9898
(num_chunks, byte_len)
9999
}
100100

101-
/// Implement `next_u32` via `fill_bytes`, little-endian order.
102-
pub fn next_u32_via_fill<R: RngCore + ?Sized>(rng: &mut R) -> u32 {
103-
let mut buf = [0; 4];
104-
rng.fill_bytes(&mut buf);
105-
u32::from_le_bytes(buf)
106-
}
107-
108-
/// Implement `next_u64` via `fill_bytes`, little-endian order.
109-
pub fn next_u64_via_fill<R: RngCore + ?Sized>(rng: &mut R) -> u64 {
110-
let mut buf = [0; 8];
111-
rng.fill_bytes(&mut buf);
112-
u64::from_le_bytes(buf)
101+
/// Yield a word using [`RngCore::fill_bytes`]
102+
///
103+
/// This may be used to implement `next_u32` or `next_u64`.
104+
pub fn next_word_via_fill<W: Word, R: RngCore + ?Sized>(rng: &mut R) -> W {
105+
let mut buf: W::Bytes = Default::default();
106+
rng.fill_bytes(buf.as_mut());
107+
W::from_le_bytes(buf)
113108
}
114109

115110
/// Fills `dst: &mut [u32]` from `src`

src/word.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ impl Word for u64 {}
1111
mod sealed {
1212
/// Sealed trait implemented for `u32` and `u64`.
1313
pub trait Sealed: Default + Copy + TryFrom<usize> + Eq + core::hash::Hash {
14-
type Bytes: Sized + AsRef<[u8]>;
14+
type Bytes: Default + Sized + AsRef<[u8]> + AsMut<[u8]>;
1515

1616
fn from_le_bytes(bytes: Self::Bytes) -> Self;
1717
fn to_le_bytes(self) -> Self::Bytes;

0 commit comments

Comments
 (0)