File: rand08.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 (134 lines) | stat: -rw-r--r-- 3,130 bytes parent folder | download | duplicates (3)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//! Implementation of [`Distribution`] for various structs.

use rand08::Rng;
use rand08::distributions::{Distribution, Standard};

use crate::{
    Date, Duration, Month, OffsetDateTime, PrimitiveDateTime, Time, UtcDateTime, UtcOffset, Weekday,
};

impl Distribution<Time> for Standard {
    #[inline]
    fn sample<R>(&self, rng: &mut R) -> Time
    where
        R: Rng + ?Sized,
    {
        Time::from_hms_nanos_ranged(rng.r#gen(), rng.r#gen(), rng.r#gen(), rng.r#gen())
    }
}

impl Distribution<Date> for Standard {
    #[inline]
    fn sample<R>(&self, rng: &mut R) -> Date
    where
        R: Rng + ?Sized,
    {
        // Safety: The Julian day number is in range.
        unsafe {
            Date::from_julian_day_unchecked(
                rng.gen_range(Date::MIN.to_julian_day()..=Date::MAX.to_julian_day()),
            )
        }
    }
}

impl Distribution<UtcOffset> for Standard {
    #[inline]
    fn sample<R>(&self, rng: &mut R) -> UtcOffset
    where
        R: Rng + ?Sized,
    {
        UtcOffset::from_hms_ranged(rng.r#gen(), rng.r#gen(), rng.r#gen())
    }
}

impl Distribution<PrimitiveDateTime> for Standard {
    #[inline]
    fn sample<R>(&self, rng: &mut R) -> PrimitiveDateTime
    where
        R: Rng + ?Sized,
    {
        PrimitiveDateTime::new(Self.sample(rng), Self.sample(rng))
    }
}

impl Distribution<UtcDateTime> for Standard {
    #[inline]
    fn sample<R>(&self, rng: &mut R) -> UtcDateTime
    where
        R: Rng + ?Sized,
    {
        UtcDateTime::new(Self.sample(rng), Self.sample(rng))
    }
}

impl Distribution<OffsetDateTime> for Standard {
    #[inline]
    fn sample<R>(&self, rng: &mut R) -> OffsetDateTime
    where
        R: Rng + ?Sized,
    {
        let date_time: PrimitiveDateTime = Self.sample(rng);
        date_time.assume_offset(Self.sample(rng))
    }
}

impl Distribution<Duration> for Standard {
    #[inline]
    fn sample<R>(&self, rng: &mut R) -> Duration
    where
        R: Rng + ?Sized,
    {
        Duration::new_ranged(rng.r#gen(), rng.r#gen())
    }
}

impl Distribution<Weekday> for Standard {
    #[inline]
    fn sample<R>(&self, rng: &mut R) -> Weekday
    where
        R: Rng + ?Sized,
    {
        use Weekday::*;

        match rng.gen_range(0u8..7) {
            0 => Monday,
            1 => Tuesday,
            2 => Wednesday,
            3 => Thursday,
            4 => Friday,
            5 => Saturday,
            val => {
                debug_assert!(val == 6);
                Sunday
            }
        }
    }
}

impl Distribution<Month> for Standard {
    #[inline]
    fn sample<R>(&self, rng: &mut R) -> Month
    where
        R: Rng + ?Sized,
    {
        use Month::*;
        match rng.gen_range(1u8..=12) {
            1 => January,
            2 => February,
            3 => March,
            4 => April,
            5 => May,
            6 => June,
            7 => July,
            8 => August,
            9 => September,
            10 => October,
            11 => November,
            val => {
                debug_assert!(val == 12);
                December
            }
        }
    }
}