Skip to content

Commit bb0a356

Browse files
committed
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).
1 parent d66d4d1 commit bb0a356

File tree

3 files changed

+35
-21
lines changed

3 files changed

+35
-21
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ appveyor = { repository = "rust-random/rand" }
2323
[features]
2424
# Meta-features:
2525
default = ["std"] # without "std" rand uses libcore
26-
nightly = ["simd_support"] # enables all features requiring nightly rust
26+
nightly = ["const_generics", "simd_support"] # enables all features requiring nightly rust
2727
serde1 = [] # does nothing, deprecated
2828

2929
# Optional dependencies:
@@ -37,6 +37,7 @@ stdweb = ["getrandom_package/stdweb"]
3737
getrandom = ["getrandom_package", "rand_core/getrandom"]
3838

3939
# Configuration:
40+
const_generics = [] # nightly feature
4041
simd_support = ["packed_simd"] # enables SIMD support
4142
small_rng = ["rand_pcg"] # enables SmallRng
4243

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#![doc(test(attr(allow(unused_variables), deny(warnings))))]
5151
#![cfg_attr(not(feature = "std"), no_std)]
5252
#![cfg_attr(all(feature = "simd_support", feature = "nightly"), feature(stdsimd))]
53+
#![cfg_attr(feature = "const_generics", feature(const_generics))]
5354
#![allow(
5455
clippy::excessive_precision,
5556
clippy::unreadable_literal,

src/rng.rs

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -383,28 +383,40 @@ impl_fill!(i8, i16, i32, i64, isize,);
383383
#[cfg(not(target_os = "emscripten"))]
384384
impl_fill!(i128);
385385

386-
macro_rules! impl_fill_arrays {
387-
($n:expr,) => {};
388-
($n:expr, $N:ident) => {
389-
impl<T> Fill for [T; $n] where [T]: Fill {
390-
fn try_fill<R: Rng + ?Sized>(&mut self, rng: &mut R) -> Result<(), Error> {
391-
self[..].try_fill(rng)
392-
}
393-
}
394-
};
395-
($n:expr, $N:ident, $($NN:ident,)*) => {
396-
impl_fill_arrays!($n, $N);
397-
impl_fill_arrays!($n - 1, $($NN,)*);
398-
};
399-
(!div $n:expr,) => {};
400-
(!div $n:expr, $N:ident, $($NN:ident,)*) => {
401-
impl_fill_arrays!($n, $N);
402-
impl_fill_arrays!(!div $n / 2, $($NN,)*);
403-
};
386+
#[cfg(feature = "const_generics")]
387+
impl<T, const N: usize> Fill for [T; N] where [T]: Fill {
388+
fn try_fill<R: Rng + ?Sized>(&mut self, rng: &mut R) -> Result<(), Error> {
389+
self[..].try_fill(rng)
390+
}
404391
}
392+
393+
#[cfg(not(feature = "const_generics"))]
405394
#[rustfmt::skip]
406-
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,);
407-
impl_fill_arrays!(!div 4096, N,N,N,N,N,N,N,);
395+
mod impls {
396+
use super::*;
397+
398+
macro_rules! impl_fill_arrays {
399+
($n:expr,) => {};
400+
($n:expr, $N:ident) => {
401+
impl<T> Fill for [T; $n] where [T]: Fill {
402+
fn try_fill<R: Rng + ?Sized>(&mut self, rng: &mut R) -> Result<(), Error> {
403+
self[..].try_fill(rng)
404+
}
405+
}
406+
};
407+
($n:expr, $N:ident, $($NN:ident,)*) => {
408+
impl_fill_arrays!($n, $N);
409+
impl_fill_arrays!($n - 1, $($NN,)*);
410+
};
411+
(!div $n:expr,) => {};
412+
(!div $n:expr, $N:ident, $($NN:ident,)*) => {
413+
impl_fill_arrays!($n, $N);
414+
impl_fill_arrays!(!div $n / 2, $($NN,)*);
415+
};
416+
}
417+
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,);
418+
impl_fill_arrays!(!div 4096, N,N,N,N,N,N,N,);
419+
}
408420

409421
#[cfg(test)]
410422
mod test {

0 commit comments

Comments
 (0)