diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/time/src/rand.rs | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/time/src/rand.rs')
-rw-r--r-- | third_party/rust/time/src/rand.rs | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/third_party/rust/time/src/rand.rs b/third_party/rust/time/src/rand.rs new file mode 100644 index 0000000000..8afefe5075 --- /dev/null +++ b/third_party/rust/time/src/rand.rs @@ -0,0 +1,99 @@ +//! Implementation of [`Distribution`] for various structs. + +use rand::distributions::{Distribution, Standard}; +use rand::Rng; + +use crate::{Date, Duration, Month, OffsetDateTime, PrimitiveDateTime, Time, UtcOffset, Weekday}; + +impl Distribution<Time> for Standard { + fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Time { + Time::__from_hms_nanos_unchecked( + rng.gen_range(0..24), + rng.gen_range(0..60), + rng.gen_range(0..60), + rng.gen_range(0..1_000_000_000), + ) + } +} + +impl Distribution<Date> for Standard { + fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Date { + Date::from_julian_day_unchecked( + rng.gen_range(Date::MIN.to_julian_day()..=Date::MAX.to_julian_day()), + ) + } +} + +impl Distribution<UtcOffset> for Standard { + fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> UtcOffset { + let seconds = rng.gen_range(-86399..=86399); + UtcOffset::__from_hms_unchecked( + (seconds / 3600) as _, + ((seconds % 3600) / 60) as _, + (seconds % 60) as _, + ) + } +} + +impl Distribution<PrimitiveDateTime> for Standard { + fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> PrimitiveDateTime { + PrimitiveDateTime::new(Self.sample(rng), Self.sample(rng)) + } +} + +impl Distribution<OffsetDateTime> for Standard { + fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> OffsetDateTime { + let date_time: PrimitiveDateTime = Self.sample(rng); + date_time.assume_offset(Self.sample(rng)) + } +} + +impl Distribution<Duration> for Standard { + fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Duration { + Duration::nanoseconds_i128( + rng.gen_range(Duration::MIN.whole_nanoseconds()..=Duration::MAX.whole_nanoseconds()), + ) + } +} + +impl Distribution<Weekday> for Standard { + fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Weekday { + 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 { + fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Month { + 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 + } + } + } +} |