File: rand.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 (73 lines) | stat: -rw-r--r-- 1,768 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
62
63
64
65
66
67
68
69
70
71
72
73
use rand08::Rng as _;
use rand09::Rng as _;
use time::{Date, Duration, Month, OffsetDateTime, PrimitiveDateTime, Time, UtcOffset, Weekday};

#[test]
fn support08() {
    // Work around rust-random/rand#1020.
    let mut rng = rand08::rngs::mock::StepRng::new(0, 656_175_560);

    for _ in 0..7 {
        let _ = rng.r#gen::<Weekday>();
    }
    for _ in 0..12 {
        let _ = rng.r#gen::<Month>();
    }
    let _ = rng.r#gen::<Time>();
    let _ = rng.r#gen::<Date>();
    let _ = rng.r#gen::<UtcOffset>();
    let _ = rng.r#gen::<PrimitiveDateTime>();
    let _ = rng.r#gen::<OffsetDateTime>();
    let _ = rng.r#gen::<Duration>();
}

#[test]
fn support09() {
    // Work around rust-random/rand#1020.
    let mut rng = StepRng::new(0, 656_175_560);

    for _ in 0..7 {
        let _ = rng.random::<Weekday>();
    }
    for _ in 0..12 {
        let _ = rng.random::<Month>();
    }
    let _ = rng.random::<Time>();
    let _ = rng.random::<Date>();
    let _ = rng.random::<UtcOffset>();
    let _ = rng.random::<PrimitiveDateTime>();
    let _ = rng.random::<OffsetDateTime>();
    let _ = rng.random::<Duration>();
}

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

impl StepRng {
    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)
    }
}