@@ -14,6 +14,8 @@ extern crate rand;
1414const RAND_BENCH_N : u64 = 1000 ;
1515
1616use std:: mem:: size_of;
17+ #[ cfg( rustc_1_28) ]
18+ use std:: num:: { NonZeroU8 , NonZeroU16 , NonZeroU32 , NonZeroU64 , NonZeroU128 } ;
1719use test:: Bencher ;
1820use 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+
4466macro_rules! distr_float {
4567 ( $fnn: ident, $ty: ty, $distr: expr) => {
4668 #[ bench]
@@ -156,6 +178,11 @@ distr_int!(distr_standard_i16, i16, Standard);
156178distr_int ! ( distr_standard_i32, i32 , Standard ) ;
157179distr_int ! ( distr_standard_i64, i64 , Standard ) ;
158180distr_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
160187distr ! ( distr_standard_bool, bool , Standard ) ;
161188distr ! ( distr_standard_alphanumeric, char , Alphanumeric ) ;
0 commit comments