diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-04 12:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-04 12:47:55 +0000 |
commit | 2aadc03ef15cb5ca5cc2af8a7c08e070742f0ac4 (patch) | |
tree | 033cc839730fda84ff08db877037977be94e5e3a /vendor/time/src/rand.rs | |
parent | Initial commit. (diff) | |
download | cargo-2aadc03ef15cb5ca5cc2af8a7c08e070742f0ac4.tar.xz cargo-2aadc03ef15cb5ca5cc2af8a7c08e070742f0ac4.zip |
Adding upstream version 0.70.1+ds1.upstream/0.70.1+ds1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/time/src/rand.rs')
-rw-r--r-- | vendor/time/src/rand.rs | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/vendor/time/src/rand.rs b/vendor/time/src/rand.rs new file mode 100644 index 0000000..eac6fe6 --- /dev/null +++ b/vendor/time/src/rand.rs @@ -0,0 +1,87 @@ +//! 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_ranged(rng.gen(), rng.gen(), rng.gen(), rng.gen()) + } +} + +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 { + UtcOffset::from_hms_ranged(rng.gen(), rng.gen(), rng.gen()) + } +} + +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::new_ranged(rng.gen(), rng.gen()) + } +} + +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 + } + } + } +} |