Skip to content

Commit 5da66c3

Browse files
committed
AsMutByteSlice: support arrays up to length 32
Usage of recursive macros appears to have some compile time hit, but with a single macro and generic impl it's not too much (directly recursing for each type is far worse).
1 parent cbd2cc7 commit 5da66c3

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

src/lib.rs

+24-6
Original file line numberDiff line numberDiff line change
@@ -508,9 +508,9 @@ pub trait RngCore {
508508
///
509509
/// [`RngCore`]: trait.RngCore.html
510510
pub trait Rng: RngCore + Sized {
511-
/// Fill `dest` entirely with random bytes, where `dest` is any type
512-
/// supporting [`AsByteSliceMut`], namely slices over primitive integer
513-
/// types (`i8`, `i16`, `u32`, etc.).
511+
/// Fill `dest` entirely with random bytes (uniform value distribution),
512+
/// where `dest` is any type supporting [`AsByteSliceMut`], namely slices
513+
/// and arrays over primitive integer types (`i8`, `i16`, `u32`, etc.).
514514
///
515515
/// On big-endian platforms this performs byte-swapping to ensure
516516
/// portability of results from reproducible generators.
@@ -536,9 +536,9 @@ pub trait Rng: RngCore + Sized {
536536
dest.to_le();
537537
}
538538

539-
/// Fill `dest` entirely with random bytes, where `dest` is any type
540-
/// supporting [`AsByteSliceMut`], namely slices over primitive integer
541-
/// types (`i8`, `i16`, `u32`, etc.).
539+
/// Fill `dest` entirely with random bytes (uniform value distribution),
540+
/// where `dest` is any type supporting [`AsByteSliceMut`], namely slices
541+
/// and arrays over primitive integer types (`i8`, `i16`, `u32`, etc.).
542542
///
543543
/// On big-endian platforms this performs byte-swapping to ensure
544544
/// portability of results from reproducible generators.
@@ -867,6 +867,24 @@ impl_as_byte_slice!(i64);
867867
#[cfg(feature="i128_support")] impl_as_byte_slice!(i128);
868868
impl_as_byte_slice!(isize);
869869

870+
macro_rules! impl_as_byte_slice_arrays {
871+
($n:expr,) => {};
872+
($n:expr, $N:ident, $($NN:ident,)*) => {
873+
impl_as_byte_slice_arrays!($n - 1, $($NN,)*);
874+
875+
impl<T> AsByteSliceMut for [T; $n] where [T]: AsByteSliceMut {
876+
fn as_byte_slice_mut<'a>(&'a mut self) -> &'a mut [u8] {
877+
self[..].as_byte_slice_mut()
878+
}
879+
880+
fn to_le(&mut self) {
881+
self[..].to_le()
882+
}
883+
}
884+
};
885+
}
886+
impl_as_byte_slice_arrays!(32, N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,);
887+
870888
/// Iterator which will generate a stream of random items.
871889
///
872890
/// This iterator is created via the [`gen_iter`] method on [`Rng`].

src/seq.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ fn sample_indices_cache<R>(
227227
#[cfg(test)]
228228
mod test {
229229
use super::*;
230-
use {XorShiftRng, RngCore, SeedableRng};
230+
use {XorShiftRng, Rng, SeedableRng};
231231
#[cfg(not(feature="std"))]
232232
use alloc::Vec;
233233

@@ -304,7 +304,7 @@ mod test {
304304
for length in 1usize..max_range {
305305
let amount = r.gen_range(0, length);
306306
let mut seed = [0u8; 16];
307-
r.fill_bytes(&mut seed);
307+
r.fill(&mut seed);
308308

309309
// assert that the two index methods give exactly the same result
310310
let inplace = sample_indices_inplace(

0 commit comments

Comments
 (0)