Skip to content

Commit 731f3d7

Browse files
authored
Merge pull request #728 from dhardy/nonzero
Implement Standard support for NonZero* types (Rustc ≥ 1.28)
2 parents f3da1ba + 23f0a51 commit 731f3d7

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

benches/distributions.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ extern crate rand;
1414
const RAND_BENCH_N: u64 = 1000;
1515

1616
use std::mem::size_of;
17+
#[cfg(rustc_1_28)]
18+
use std::num::{NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128};
1719
use test::Bencher;
1820
use std::time::Duration;
1921

@@ -41,6 +43,26 @@ macro_rules! distr_int {
4143
}
4244
}
4345

46+
macro_rules! distr_nz_int {
47+
($fnn:ident, $tynz:ty, $ty:ty, $distr:expr) => {
48+
#[bench]
49+
fn $fnn(b: &mut Bencher) {
50+
let mut rng = SmallRng::from_entropy();
51+
let distr = $distr;
52+
53+
b.iter(|| {
54+
let mut accum = 0 as $ty;
55+
for _ in 0..::RAND_BENCH_N {
56+
let x: $tynz = distr.sample(&mut rng);
57+
accum = accum.wrapping_add(x.get());
58+
}
59+
accum
60+
});
61+
b.bytes = size_of::<$ty>() as u64 * ::RAND_BENCH_N;
62+
}
63+
}
64+
}
65+
4466
macro_rules! distr_float {
4567
($fnn:ident, $ty:ty, $distr:expr) => {
4668
#[bench]
@@ -156,6 +178,11 @@ distr_int!(distr_standard_i16, i16, Standard);
156178
distr_int!(distr_standard_i32, i32, Standard);
157179
distr_int!(distr_standard_i64, i64, Standard);
158180
distr_int!(distr_standard_i128, i128, Standard);
181+
#[cfg(rustc_1_28)] distr_nz_int!(distr_standard_nz8, NonZeroU8, u8, Standard);
182+
#[cfg(rustc_1_28)] distr_nz_int!(distr_standard_nz16, NonZeroU16, u16, Standard);
183+
#[cfg(rustc_1_28)] distr_nz_int!(distr_standard_nz32, NonZeroU32, u32, Standard);
184+
#[cfg(rustc_1_28)] distr_nz_int!(distr_standard_nz64, NonZeroU64, u64, Standard);
185+
#[cfg(rustc_1_28)] distr_nz_int!(distr_standard_nz128, NonZeroU128, u128, Standard);
159186

160187
distr!(distr_standard_bool, bool, Standard);
161188
distr!(distr_standard_alphanumeric, char, Alphanumeric);

build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ fn main() {
77
ac.emit_rustc_version(1, 25);
88
ac.emit_rustc_version(1, 26);
99
ac.emit_rustc_version(1, 27);
10+
ac.emit_rustc_version(1, 28);
1011
}

src/distributions/integer.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
1111
use {Rng};
1212
use distributions::{Distribution, Standard};
13+
#[cfg(rustc_1_28)]
14+
use core::num::{NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128, NonZeroUsize};
1315
#[cfg(feature="simd_support")]
1416
use packed_simd::*;
1517
#[cfg(all(target_arch = "x86", feature="nightly"))]
@@ -88,6 +90,27 @@ impl_int_from_uint! { i64, u64 }
8890
#[cfg(all(rustc_1_26, not(target_os = "emscripten")))] impl_int_from_uint! { i128, u128 }
8991
impl_int_from_uint! { isize, usize }
9092

93+
macro_rules! impl_nzint {
94+
($ty:ty, $new:path) => {
95+
impl Distribution<$ty> for Standard {
96+
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> $ty {
97+
loop {
98+
if let Some(nz) = $new(rng.gen()) {
99+
break nz;
100+
}
101+
}
102+
}
103+
}
104+
}
105+
}
106+
107+
#[cfg(rustc_1_28)] impl_nzint!(NonZeroU8, NonZeroU8::new);
108+
#[cfg(rustc_1_28)] impl_nzint!(NonZeroU16, NonZeroU16::new);
109+
#[cfg(rustc_1_28)] impl_nzint!(NonZeroU32, NonZeroU32::new);
110+
#[cfg(rustc_1_28)] impl_nzint!(NonZeroU64, NonZeroU64::new);
111+
#[cfg(all(rustc_1_28, not(target_os = "emscripten")))] impl_nzint!(NonZeroU128, NonZeroU128::new);
112+
#[cfg(rustc_1_28)] impl_nzint!(NonZeroUsize, NonZeroUsize::new);
113+
91114
#[cfg(feature="simd_support")]
92115
macro_rules! simd_impl {
93116
($(($intrinsic:ident, $vec:ty),)+) => {$(

0 commit comments

Comments
 (0)