Skip to content

Commit

Permalink
AsMutByteSlice: support arrays up to length 32
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
dhardy committed Feb 20, 2018
1 parent cbd2cc7 commit 5da66c3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
30 changes: 24 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,9 +508,9 @@ pub trait RngCore {
///
/// [`RngCore`]: trait.RngCore.html
pub trait Rng: RngCore + Sized {
/// Fill `dest` entirely with random bytes, where `dest` is any type
/// supporting [`AsByteSliceMut`], namely slices over primitive integer
/// types (`i8`, `i16`, `u32`, etc.).
/// Fill `dest` entirely with random bytes (uniform value distribution),
/// where `dest` is any type supporting [`AsByteSliceMut`], namely slices
/// and arrays over primitive integer types (`i8`, `i16`, `u32`, etc.).
///
/// On big-endian platforms this performs byte-swapping to ensure
/// portability of results from reproducible generators.
Expand All @@ -536,9 +536,9 @@ pub trait Rng: RngCore + Sized {
dest.to_le();
}

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

macro_rules! impl_as_byte_slice_arrays {
($n:expr,) => {};
($n:expr, $N:ident, $($NN:ident,)*) => {
impl_as_byte_slice_arrays!($n - 1, $($NN,)*);

impl<T> AsByteSliceMut for [T; $n] where [T]: AsByteSliceMut {
fn as_byte_slice_mut<'a>(&'a mut self) -> &'a mut [u8] {
self[..].as_byte_slice_mut()
}

fn to_le(&mut self) {
self[..].to_le()
}
}
};
}
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,);

/// Iterator which will generate a stream of random items.
///
/// This iterator is created via the [`gen_iter`] method on [`Rng`].
Expand Down
4 changes: 2 additions & 2 deletions src/seq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ fn sample_indices_cache<R>(
#[cfg(test)]
mod test {
use super::*;
use {XorShiftRng, RngCore, SeedableRng};
use {XorShiftRng, Rng, SeedableRng};
#[cfg(not(feature="std"))]
use alloc::Vec;

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

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

0 comments on commit 5da66c3

Please sign in to comment.