File: rand09.rs

package info (click to toggle)
rust-time 0.3.47-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,972 kB
  • sloc: makefile: 2
file content (61 lines) | stat: -rw-r--r-- 1,380 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use criterion::Bencher;
use rand09::Rng;
use time::{Date, Duration, Month, OffsetDateTime, PrimitiveDateTime, Time, UtcOffset, Weekday};

macro_rules! bench_rand {
    ($($name:ident : $type:ty),* $(,)?) => {
        setup_benchmark! {
            "Random",
            $(fn $name(ben: &mut Bencher<'_>) {
                iter_batched_ref!(
                    ben,
                    || StepRng::new(0, 1),
                    [|rng| rng.random::<$type>()]
                );
            })*
        }
    }
}

bench_rand![
    time: Time,
    date: Date,
    utc_offset: UtcOffset,
    primitive_date_time: PrimitiveDateTime,
    offset_date_time: OffsetDateTime,
    duration: Duration,
    weekday: Weekday,
    month: Month,
];

// copy of `StepRng` from rand 0.8 to avoid deprecation warnings
#[derive(Debug, Clone)]
struct StepRng {
    v: u64,
    a: u64,
}

impl StepRng {
    const fn new(initial: u64, increment: u64) -> Self {
        Self {
            v: initial,
            a: increment,
        }
    }
}

impl rand09::RngCore for StepRng {
    fn next_u32(&mut self) -> u32 {
        self.next_u64() as u32
    }

    fn next_u64(&mut self) -> u64 {
        let res = self.v;
        self.v = self.v.wrapping_add(self.a);
        res
    }

    fn fill_bytes(&mut self, dst: &mut [u8]) {
        rand09::rand_core::impls::fill_bytes_via_next(self, dst)
    }
}