From bb0a356ec75210f91350e5a7147cb6676d6fb5d3 Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Fri, 28 Feb 2020 16:44:51 +0000 Subject: [PATCH] Use const_generics to impl Fill for array types (feature flag) Uses the experimental nightly feature to produce a more generic impl. Note: we cannot yet do the same for Standard (see #939). --- Cargo.toml | 3 ++- src/lib.rs | 1 + src/rng.rs | 52 ++++++++++++++++++++++++++++++++-------------------- 3 files changed, 35 insertions(+), 21 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ac64187cf69..7d8f2a55d7c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,7 +23,7 @@ appveyor = { repository = "rust-random/rand" } [features] # Meta-features: default = ["std"] # without "std" rand uses libcore -nightly = ["simd_support"] # enables all features requiring nightly rust +nightly = ["const_generics", "simd_support"] # enables all features requiring nightly rust serde1 = [] # does nothing, deprecated # Optional dependencies: @@ -37,6 +37,7 @@ stdweb = ["getrandom_package/stdweb"] getrandom = ["getrandom_package", "rand_core/getrandom"] # Configuration: +const_generics = [] # nightly feature simd_support = ["packed_simd"] # enables SIMD support small_rng = ["rand_pcg"] # enables SmallRng diff --git a/src/lib.rs b/src/lib.rs index f0f9f148aa8..4bc2222d6f6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -50,6 +50,7 @@ #![doc(test(attr(allow(unused_variables), deny(warnings))))] #![cfg_attr(not(feature = "std"), no_std)] #![cfg_attr(all(feature = "simd_support", feature = "nightly"), feature(stdsimd))] +#![cfg_attr(feature = "const_generics", feature(const_generics))] #![allow( clippy::excessive_precision, clippy::unreadable_literal, diff --git a/src/rng.rs b/src/rng.rs index 391b6dd0e83..3fc01ae63a8 100644 --- a/src/rng.rs +++ b/src/rng.rs @@ -383,28 +383,40 @@ impl_fill!(i8, i16, i32, i64, isize,); #[cfg(not(target_os = "emscripten"))] impl_fill!(i128); -macro_rules! impl_fill_arrays { - ($n:expr,) => {}; - ($n:expr, $N:ident) => { - impl Fill for [T; $n] where [T]: Fill { - fn try_fill(&mut self, rng: &mut R) -> Result<(), Error> { - self[..].try_fill(rng) - } - } - }; - ($n:expr, $N:ident, $($NN:ident,)*) => { - impl_fill_arrays!($n, $N); - impl_fill_arrays!($n - 1, $($NN,)*); - }; - (!div $n:expr,) => {}; - (!div $n:expr, $N:ident, $($NN:ident,)*) => { - impl_fill_arrays!($n, $N); - impl_fill_arrays!(!div $n / 2, $($NN,)*); - }; +#[cfg(feature = "const_generics")] +impl Fill for [T; N] where [T]: Fill { + fn try_fill(&mut self, rng: &mut R) -> Result<(), Error> { + self[..].try_fill(rng) + } } + +#[cfg(not(feature = "const_generics"))] #[rustfmt::skip] -impl_fill_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,N,); -impl_fill_arrays!(!div 4096, N,N,N,N,N,N,N,); +mod impls { + use super::*; + + macro_rules! impl_fill_arrays { + ($n:expr,) => {}; + ($n:expr, $N:ident) => { + impl Fill for [T; $n] where [T]: Fill { + fn try_fill(&mut self, rng: &mut R) -> Result<(), Error> { + self[..].try_fill(rng) + } + } + }; + ($n:expr, $N:ident, $($NN:ident,)*) => { + impl_fill_arrays!($n, $N); + impl_fill_arrays!($n - 1, $($NN,)*); + }; + (!div $n:expr,) => {}; + (!div $n:expr, $N:ident, $($NN:ident,)*) => { + impl_fill_arrays!($n, $N); + impl_fill_arrays!(!div $n / 2, $($NN,)*); + }; + } + impl_fill_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,N,); + impl_fill_arrays!(!div 4096, N,N,N,N,N,N,N,); +} #[cfg(test)] mod test {