Skip to content

Commit 2b36486

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 e9d0b06 commit 2b36486

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
@@ -538,9 +538,9 @@ pub trait RngCore {
538538
///
539539
/// [`RngCore`]: trait.RngCore.html
540540
pub trait Rng: RngCore + Sized {
541-
/// Fill `dest` entirely with random bytes, where `dest` is any type
542-
/// supporting [`AsByteSliceMut`], namely slices over primitive integer
543-
/// types (`i8`, `i16`, `u32`, etc.).
541+
/// Fill `dest` entirely with random bytes (uniform value distribution),
542+
/// where `dest` is any type supporting [`AsByteSliceMut`], namely slices
543+
/// and arrays over primitive integer types (`i8`, `i16`, `u32`, etc.).
544544
///
545545
/// On big-endian platforms this performs byte-swapping to ensure
546546
/// portability of results from reproducible generators.
@@ -566,9 +566,9 @@ pub trait Rng: RngCore + Sized {
566566
dest.to_le();
567567
}
568568

569-
/// Fill `dest` entirely with random bytes, where `dest` is any type
570-
/// supporting [`AsByteSliceMut`], namely slices over primitive integer
571-
/// types (`i8`, `i16`, `u32`, etc.).
569+
/// Fill `dest` entirely with random bytes (uniform value distribution),
570+
/// where `dest` is any type supporting [`AsByteSliceMut`], namely slices
571+
/// and arrays over primitive integer types (`i8`, `i16`, `u32`, etc.).
572572
///
573573
/// On big-endian platforms this performs byte-swapping to ensure
574574
/// portability of results from reproducible generators.
@@ -880,6 +880,24 @@ impl_as_byte_slice!(i64);
880880
#[cfg(feature="i128_support")] impl_as_byte_slice!(i128);
881881
impl_as_byte_slice!(isize);
882882

883+
macro_rules! impl_as_byte_slice_arrays {
884+
($n:expr,) => {};
885+
($n:expr, $N:ident, $($NN:ident,)*) => {
886+
impl_as_byte_slice_arrays!($n - 1, $($NN,)*);
887+
888+
impl<T> AsByteSliceMut for [T; $n] where [T]: AsByteSliceMut {
889+
fn as_byte_slice_mut<'a>(&'a mut self) -> &'a mut [u8] {
890+
self[..].as_byte_slice_mut()
891+
}
892+
893+
fn to_le(&mut self) {
894+
self[..].to_le()
895+
}
896+
}
897+
};
898+
}
899+
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,);
900+
883901
/// Iterator which will generate a stream of random items.
884902
///
885903
/// 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)