Skip to content

Commit 2197f7b

Browse files
committed
Rename le::Observable -> Word and make a sealed pub trait
1 parent 432b672 commit 2197f7b

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

src/block.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
//! [`SeedableRng`]: crate::SeedableRng
6464
//! [`rand::rngs::ReseedingRng`]: https://docs.rs/rand/latest/rand/rngs/struct.ReseedingRng.html
6565
66-
use crate::le::{Observable, fill_via_chunks};
66+
use crate::le::{Word, fill_via_chunks};
6767
use core::fmt;
6868

6969
/// A random (block) generator
@@ -199,7 +199,7 @@ impl<const N: usize, G: Generator<Output = [u32; N]>> BlockRng<G> {
199199
}
200200
}
201201

202-
impl<W: Observable, const N: usize, G: Generator<Output = [W; N]>> BlockRng<G> {
202+
impl<W: Word, const N: usize, G: Generator<Output = [W; N]>> BlockRng<G> {
203203
/// Fill `dest`
204204
#[inline]
205205
pub fn fill_bytes(&mut self, dest: &mut [u8]) {

src/le.rs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,31 +71,39 @@ pub fn fill_bytes_via_next<R: RngCore + ?Sized>(rng: &mut R, dest: &mut [u8]) {
7171
}
7272
}
7373

74-
pub(crate) trait Observable: Copy {
75-
type Bytes: Sized + AsRef<[u8]>;
76-
fn to_le_bytes(self) -> Self::Bytes;
77-
}
78-
impl Observable for u32 {
79-
type Bytes = [u8; 4];
74+
mod sealed {
75+
pub trait Word: Copy {
76+
type Bytes: Sized + AsRef<[u8]>;
77+
fn to_le_bytes(self) -> Self::Bytes;
78+
}
79+
impl Word for u32 {
80+
type Bytes = [u8; 4];
8081

81-
fn to_le_bytes(self) -> Self::Bytes {
82-
Self::to_le_bytes(self)
82+
fn to_le_bytes(self) -> Self::Bytes {
83+
Self::to_le_bytes(self)
84+
}
8385
}
84-
}
85-
impl Observable for u64 {
86-
type Bytes = [u8; 8];
86+
impl Word for u64 {
87+
type Bytes = [u8; 8];
8788

88-
fn to_le_bytes(self) -> Self::Bytes {
89-
Self::to_le_bytes(self)
89+
fn to_le_bytes(self) -> Self::Bytes {
90+
Self::to_le_bytes(self)
91+
}
9092
}
9193
}
9294

95+
/// A marker trait for supported word types
96+
///
97+
/// This is implemented for: `u32`, `u64`.
98+
pub trait Word: sealed::Word {}
99+
impl<W: sealed::Word> Word for W {}
100+
93101
/// Fill dest from src
94102
///
95103
/// Returns `(n, byte_len)`. `src[..n]` is consumed,
96104
/// `dest[..byte_len]` is filled. `src[n..]` and `dest[byte_len..]` are left
97105
/// unaltered.
98-
pub(crate) fn fill_via_chunks<T: Observable>(src: &[T], dest: &mut [u8]) -> (usize, usize) {
106+
pub(crate) fn fill_via_chunks<T: Word>(src: &[T], dest: &mut [u8]) -> (usize, usize) {
99107
let size = core::mem::size_of::<T>();
100108

101109
// Always use little endian for portability of results.

0 commit comments

Comments
 (0)