Debian Codegraph @

debian/rust-vendor/time/src/rand08.rs

raw
1 //! Implementation of [`Distribution`] for various structs.
2
3 use rand08::Rng;
4 use rand08::distributions::{Distribution, Standard};
5
6 use crate::{
7 Date, Month, OffsetDateTime, PlainDateTime, SignedDuration, Time, Timestamp, UtcDateTime,
8 UtcOffset, Weekday,
9 };
10
11 impl Distribution<Time> for Standard {
12 #[inline]
13 fn sample<R>(&self, rng: &mut R) -> Time
14 where
15 R: Rng + ?Sized,
16 {
17 Time::from_hms_nanos_ranged(rng.r#gen(), rng.r#gen(), rng.r#gen(), rng.r#gen())
18 }
19 }
20
21 impl Distribution<Date> for Standard {
22 #[inline]
23 fn sample<R>(&self, rng: &mut R) -> Date
24 where
25 R: Rng + ?Sized,
26 {
27 // Safety: The Julian day number is in range.
28 unsafe {
29 Date::from_julian_day_unchecked(
30 rng.gen_range(Date::MIN.to_julian_day()..=Date::MAX.to_julian_day()),
31 )
32 }
33 }
34 }
35
36 impl Distribution<UtcOffset> for Standard {
37 #[inline]
38 fn sample<R>(&self, rng: &mut R) -> UtcOffset
39 where
40 R: Rng + ?Sized,
41 {
42 UtcOffset::from_hms_ranged(rng.r#gen(), rng.r#gen(), rng.r#gen())
43 }
44 }
45
46 impl Distribution<PlainDateTime> for Standard {
47 #[inline]
48 fn sample<R>(&self, rng: &mut R) -> PlainDateTime
49 where
50 R: Rng + ?Sized,
51 {
52 PlainDateTime::new(Self.sample(rng), Self.sample(rng))
53 }
54 }
55
56 impl Distribution<UtcDateTime> for Standard {
57 #[inline]
58 fn sample<R>(&self, rng: &mut R) -> UtcDateTime
59 where
60 R: Rng + ?Sized,
61 {
62 UtcDateTime::new(Self.sample(rng), Self.sample(rng))
63 }
64 }
65
66 impl Distribution<OffsetDateTime> for Standard {
67 #[inline]
68 fn sample<R>(&self, rng: &mut R) -> OffsetDateTime
69 where
70 R: Rng + ?Sized,
71 {
72 let date_time: PlainDateTime = Self.sample(rng);
73 date_time.assume_offset(Self.sample(rng))
74 }
75 }
76
77 impl Distribution<Timestamp> for Standard {
78 #[inline]
79 fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Timestamp {
80 Timestamp::new_ranged(rng.r#gen(), rng.r#gen())
81 }
82 }
83
84 impl Distribution<SignedDuration> for Standard {
85 #[inline]
86 fn sample<R>(&self, rng: &mut R) -> SignedDuration
87 where
88 R: Rng + ?Sized,
89 {
90 SignedDuration::new_ranged(rng.r#gen(), rng.r#gen())
91 }
92 }
93
94 impl Distribution<Weekday> for Standard {
95 #[inline]
96 fn sample<R>(&self, rng: &mut R) -> Weekday
97 where
98 R: Rng + ?Sized,
99 {
100 use Weekday::*;
101
102 match rng.gen_range(0u8..7) {
103 0 => Monday,
104 1 => Tuesday,
105 2 => Wednesday,
106 3 => Thursday,
107 4 => Friday,
108 5 => Saturday,
109 val => {
110 debug_assert!(val == 6);
111 Sunday
112 }
113 }
114 }
115 }
116
117 impl Distribution<Month> for Standard {
118 #[inline]
119 fn sample<R>(&self, rng: &mut R) -> Month
120 where
121 R: Rng + ?Sized,
122 {
123 use Month::*;
124 match rng.gen_range(1u8..=12) {
125 1 => January,
126 2 => February,
127 3 => March,
128 4 => April,
129 5 => May,
130 6 => June,
131 7 => July,
132 8 => August,
133 9 => September,
134 10 => October,
135 11 => November,
136 val => {
137 debug_assert!(val == 12);
138 December
139 }
140 }
141 }
142 }