diff options
| author | mo khan <mo@mokhan.ca> | 2025-07-02 18:36:06 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-07-02 18:36:06 -0600 |
| commit | 8cdfa445d6629ffef4cb84967ff7017654045bc2 (patch) | |
| tree | 22f0b0907c024c78d26a731e2e1f5219407d8102 /vendor/time/src/ext | |
| parent | 4351c74c7c5f97156bc94d3a8549b9940ac80e3f (diff) | |
chore: add vendor directory
Diffstat (limited to 'vendor/time/src/ext')
| -rw-r--r-- | vendor/time/src/ext/digit_count.rs | 26 | ||||
| -rw-r--r-- | vendor/time/src/ext/instant.rs | 98 | ||||
| -rw-r--r-- | vendor/time/src/ext/mod.rs | 13 | ||||
| -rw-r--r-- | vendor/time/src/ext/numerical_duration.rs | 140 | ||||
| -rw-r--r-- | vendor/time/src/ext/numerical_std_duration.rs | 192 |
5 files changed, 469 insertions, 0 deletions
diff --git a/vendor/time/src/ext/digit_count.rs b/vendor/time/src/ext/digit_count.rs new file mode 100644 index 00000000..fb42ce9e --- /dev/null +++ b/vendor/time/src/ext/digit_count.rs @@ -0,0 +1,26 @@ +use num_conv::prelude::*; + +/// A trait that indicates the formatted width of the value can be determined. +/// +/// Note that this should not be implemented for any signed integers. This forces the caller to +/// write the sign if desired. +pub(crate) trait DigitCount { + /// The number of digits in the stringified value. + fn num_digits(self) -> u8; +} + +/// A macro to generate implementations of `DigitCount` for unsigned integers. +macro_rules! impl_digit_count { + ($($t:ty),* $(,)?) => { + $(impl DigitCount for $t { + fn num_digits(self) -> u8 { + match self.checked_ilog10() { + Some(n) => n.truncate::<u8>() + 1, + None => 1, + } + } + })* + }; +} + +impl_digit_count!(u8, u16, u32); diff --git a/vendor/time/src/ext/instant.rs b/vendor/time/src/ext/instant.rs new file mode 100644 index 00000000..313ad5b1 --- /dev/null +++ b/vendor/time/src/ext/instant.rs @@ -0,0 +1,98 @@ +use std::time::Instant as StdInstant; + +use crate::Duration; + +/// Sealed trait to prevent downstream implementations. +mod sealed { + /// A trait that cannot be implemented by downstream users. + pub trait Sealed: Sized {} + impl Sealed for std::time::Instant {} +} + +/// An extension trait for [`std::time::Instant`] that adds methods for +/// [`time::Duration`](Duration)s. +pub trait InstantExt: sealed::Sealed { + /// # Panics + /// + /// This function may panic if the resulting point in time cannot be represented by the + /// underlying data structure. See [`InstantExt::checked_add_signed`] for a non-panicking + /// version. + fn add_signed(self, duration: Duration) -> Self { + self.checked_add_signed(duration) + .expect("overflow when adding duration to instant") + } + + /// # Panics + /// + /// This function may panic if the resulting point in time cannot be represented by the + /// underlying data structure. See [`InstantExt::checked_sub_signed`] for a non-panicking + /// version. + fn sub_signed(self, duration: Duration) -> Self { + self.checked_sub_signed(duration) + .expect("overflow when subtracting duration from instant") + } + + /// Returns `Some(t)` where `t` is the time `self.checked_add_signed(duration)` if `t` can be + /// represented as `Instant` (which means it's inside the bounds of the underlying data + /// structure), `None` otherwise. + fn checked_add_signed(&self, duration: Duration) -> Option<Self>; + + /// Returns `Some(t)` where `t` is the time `self.checked_sub_signed(duration)` if `t` can be + /// represented as `Instant` (which means it's inside the bounds of the underlying data + /// structure), `None` otherwise. + fn checked_sub_signed(&self, duration: Duration) -> Option<Self>; + + /// Returns the amount of time elapsed from another instant to this one. This will be negative + /// if `earlier` is later than `self`. + /// + /// # Example + /// + /// ```rust + /// # use std::thread::sleep; + /// # use std::time::{Duration, Instant}; + /// # use time::ext::InstantExt; + /// let now = Instant::now(); + /// sleep(Duration::new(1, 0)); + /// let new_now = Instant::now(); + /// println!("{:?}", new_now.signed_duration_since(now)); // positive + /// println!("{:?}", now.signed_duration_since(new_now)); // negative + /// ``` + fn signed_duration_since(&self, earlier: Self) -> Duration; +} + +impl InstantExt for StdInstant { + fn checked_add_signed(&self, duration: Duration) -> Option<Self> { + if duration.is_positive() { + self.checked_add(duration.unsigned_abs()) + } else if duration.is_negative() { + self.checked_sub(duration.unsigned_abs()) + } else { + debug_assert!(duration.is_zero()); + Some(*self) + } + } + + fn checked_sub_signed(&self, duration: Duration) -> Option<Self> { + if duration.is_positive() { + self.checked_sub(duration.unsigned_abs()) + } else if duration.is_negative() { + self.checked_add(duration.unsigned_abs()) + } else { + debug_assert!(duration.is_zero()); + Some(*self) + } + } + + fn signed_duration_since(&self, earlier: Self) -> Duration { + if *self > earlier { + self.saturating_duration_since(earlier) + .try_into() + .unwrap_or(Duration::MAX) + } else { + earlier + .saturating_duration_since(*self) + .try_into() + .map_or(Duration::MIN, |d: Duration| -d) + } + } +} diff --git a/vendor/time/src/ext/mod.rs b/vendor/time/src/ext/mod.rs new file mode 100644 index 00000000..7cc2d0d9 --- /dev/null +++ b/vendor/time/src/ext/mod.rs @@ -0,0 +1,13 @@ +//! Extension traits. + +mod digit_count; +#[cfg(feature = "std")] +mod instant; +mod numerical_duration; +mod numerical_std_duration; + +pub(crate) use self::digit_count::DigitCount; +#[cfg(feature = "std")] +pub use self::instant::InstantExt; +pub use self::numerical_duration::NumericalDuration; +pub use self::numerical_std_duration::NumericalStdDuration; diff --git a/vendor/time/src/ext/numerical_duration.rs b/vendor/time/src/ext/numerical_duration.rs new file mode 100644 index 00000000..a1e93cc4 --- /dev/null +++ b/vendor/time/src/ext/numerical_duration.rs @@ -0,0 +1,140 @@ +use crate::convert::*; +use crate::Duration; + +/// Sealed trait to prevent downstream implementations. +mod sealed { + /// A trait that cannot be implemented by downstream users. + pub trait Sealed {} + impl Sealed for i64 {} + impl Sealed for f64 {} +} + +/// Create [`Duration`]s from numeric literals. +/// +/// # Examples +/// +/// Basic construction of [`Duration`]s. +/// +/// ```rust +/// # use time::{Duration, ext::NumericalDuration}; +/// assert_eq!(5.nanoseconds(), Duration::nanoseconds(5)); +/// assert_eq!(5.microseconds(), Duration::microseconds(5)); +/// assert_eq!(5.milliseconds(), Duration::milliseconds(5)); +/// assert_eq!(5.seconds(), Duration::seconds(5)); +/// assert_eq!(5.minutes(), Duration::minutes(5)); +/// assert_eq!(5.hours(), Duration::hours(5)); +/// assert_eq!(5.days(), Duration::days(5)); +/// assert_eq!(5.weeks(), Duration::weeks(5)); +/// ``` +/// +/// Signed integers work as well! +/// +/// ```rust +/// # use time::{Duration, ext::NumericalDuration}; +/// assert_eq!((-5).nanoseconds(), Duration::nanoseconds(-5)); +/// assert_eq!((-5).microseconds(), Duration::microseconds(-5)); +/// assert_eq!((-5).milliseconds(), Duration::milliseconds(-5)); +/// assert_eq!((-5).seconds(), Duration::seconds(-5)); +/// assert_eq!((-5).minutes(), Duration::minutes(-5)); +/// assert_eq!((-5).hours(), Duration::hours(-5)); +/// assert_eq!((-5).days(), Duration::days(-5)); +/// assert_eq!((-5).weeks(), Duration::weeks(-5)); +/// ``` +/// +/// Just like any other [`Duration`], they can be added, subtracted, etc. +/// +/// ```rust +/// # use time::ext::NumericalDuration; +/// assert_eq!(2.seconds() + 500.milliseconds(), 2_500.milliseconds()); +/// assert_eq!(2.seconds() - 500.milliseconds(), 1_500.milliseconds()); +/// ``` +/// +/// When called on floating point values, any remainder of the floating point value will be +/// truncated. Keep in mind that floating point numbers are inherently imprecise and have +/// limited capacity. +pub trait NumericalDuration: sealed::Sealed { + /// Create a [`Duration`] from the number of nanoseconds. + fn nanoseconds(self) -> Duration; + /// Create a [`Duration`] from the number of microseconds. + fn microseconds(self) -> Duration; + /// Create a [`Duration`] from the number of milliseconds. + fn milliseconds(self) -> Duration; + /// Create a [`Duration`] from the number of seconds. + fn seconds(self) -> Duration; + /// Create a [`Duration`] from the number of minutes. + fn minutes(self) -> Duration; + /// Create a [`Duration`] from the number of hours. + fn hours(self) -> Duration; + /// Create a [`Duration`] from the number of days. + fn days(self) -> Duration; + /// Create a [`Duration`] from the number of weeks. + fn weeks(self) -> Duration; +} + +impl NumericalDuration for i64 { + fn nanoseconds(self) -> Duration { + Duration::nanoseconds(self) + } + + fn microseconds(self) -> Duration { + Duration::microseconds(self) + } + + fn milliseconds(self) -> Duration { + Duration::milliseconds(self) + } + + fn seconds(self) -> Duration { + Duration::seconds(self) + } + + fn minutes(self) -> Duration { + Duration::minutes(self) + } + + fn hours(self) -> Duration { + Duration::hours(self) + } + + fn days(self) -> Duration { + Duration::days(self) + } + + fn weeks(self) -> Duration { + Duration::weeks(self) + } +} + +impl NumericalDuration for f64 { + fn nanoseconds(self) -> Duration { + Duration::nanoseconds(self as i64) + } + + fn microseconds(self) -> Duration { + Duration::nanoseconds((self * Nanosecond::per(Microsecond) as Self) as i64) + } + + fn milliseconds(self) -> Duration { + Duration::nanoseconds((self * Nanosecond::per(Millisecond) as Self) as i64) + } + + fn seconds(self) -> Duration { + Duration::nanoseconds((self * Nanosecond::per(Second) as Self) as i64) + } + + fn minutes(self) -> Duration { + Duration::nanoseconds((self * Nanosecond::per(Minute) as Self) as i64) + } + + fn hours(self) -> Duration { + Duration::nanoseconds((self * Nanosecond::per(Hour) as Self) as i64) + } + + fn days(self) -> Duration { + Duration::nanoseconds((self * Nanosecond::per(Day) as Self) as i64) + } + + fn weeks(self) -> Duration { + Duration::nanoseconds((self * Nanosecond::per(Week) as Self) as i64) + } +} diff --git a/vendor/time/src/ext/numerical_std_duration.rs b/vendor/time/src/ext/numerical_std_duration.rs new file mode 100644 index 00000000..d6bc7bab --- /dev/null +++ b/vendor/time/src/ext/numerical_std_duration.rs @@ -0,0 +1,192 @@ +use core::time::Duration as StdDuration; + +use num_conv::prelude::*; + +use crate::convert::*; + +/// Sealed trait to prevent downstream implementations. +mod sealed { + /// A trait that cannot be implemented by downstream users. + pub trait Sealed {} + impl Sealed for u64 {} + impl Sealed for f64 {} +} + +/// Create [`std::time::Duration`]s from numeric literals. +/// +/// # Examples +/// +/// Basic construction of [`std::time::Duration`]s. +/// +/// ```rust +/// # use time::ext::NumericalStdDuration; +/// # use core::time::Duration; +/// assert_eq!(5.std_nanoseconds(), Duration::from_nanos(5)); +/// assert_eq!(5.std_microseconds(), Duration::from_micros(5)); +/// assert_eq!(5.std_milliseconds(), Duration::from_millis(5)); +/// assert_eq!(5.std_seconds(), Duration::from_secs(5)); +/// assert_eq!(5.std_minutes(), Duration::from_secs(5 * 60)); +/// assert_eq!(5.std_hours(), Duration::from_secs(5 * 3_600)); +/// assert_eq!(5.std_days(), Duration::from_secs(5 * 86_400)); +/// assert_eq!(5.std_weeks(), Duration::from_secs(5 * 604_800)); +/// ``` +/// +/// Just like any other [`std::time::Duration`], they can be added, subtracted, etc. +/// +/// ```rust +/// # use time::ext::NumericalStdDuration; +/// assert_eq!( +/// 2.std_seconds() + 500.std_milliseconds(), +/// 2_500.std_milliseconds() +/// ); +/// assert_eq!( +/// 2.std_seconds() - 500.std_milliseconds(), +/// 1_500.std_milliseconds() +/// ); +/// ``` +/// +/// When called on floating point values, any remainder of the floating point value will be +/// truncated. Keep in mind that floating point numbers are inherently imprecise and have +/// limited capacity. +pub trait NumericalStdDuration: sealed::Sealed { + /// Create a [`std::time::Duration`] from the number of nanoseconds. + fn std_nanoseconds(self) -> StdDuration; + /// Create a [`std::time::Duration`] from the number of microseconds. + fn std_microseconds(self) -> StdDuration; + /// Create a [`std::time::Duration`] from the number of milliseconds. + fn std_milliseconds(self) -> StdDuration; + /// Create a [`std::time::Duration`] from the number of seconds. + fn std_seconds(self) -> StdDuration; + /// Create a [`std::time::Duration`] from the number of minutes. + fn std_minutes(self) -> StdDuration; + /// Create a [`std::time::Duration`] from the number of hours. + fn std_hours(self) -> StdDuration; + /// Create a [`std::time::Duration`] from the number of days. + fn std_days(self) -> StdDuration; + /// Create a [`std::time::Duration`] from the number of weeks. + fn std_weeks(self) -> StdDuration; +} + +impl NumericalStdDuration for u64 { + fn std_nanoseconds(self) -> StdDuration { + StdDuration::from_nanos(self) + } + + fn std_microseconds(self) -> StdDuration { + StdDuration::from_micros(self) + } + + fn std_milliseconds(self) -> StdDuration { + StdDuration::from_millis(self) + } + + fn std_seconds(self) -> StdDuration { + StdDuration::from_secs(self) + } + + /// # Panics + /// + /// This may panic if an overflow occurs. + fn std_minutes(self) -> StdDuration { + StdDuration::from_secs( + self.checked_mul(Second::per(Minute).extend()) + .expect("overflow constructing `time::Duration`"), + ) + } + + /// # Panics + /// + /// This may panic if an overflow occurs. + fn std_hours(self) -> StdDuration { + StdDuration::from_secs( + self.checked_mul(Second::per(Hour).extend()) + .expect("overflow constructing `time::Duration`"), + ) + } + + /// # Panics + /// + /// This may panic if an overflow occurs. + fn std_days(self) -> StdDuration { + StdDuration::from_secs( + self.checked_mul(Second::per(Day).extend()) + .expect("overflow constructing `time::Duration`"), + ) + } + + /// # Panics + /// + /// This may panic if an overflow occurs. + fn std_weeks(self) -> StdDuration { + StdDuration::from_secs( + self.checked_mul(Second::per(Week).extend()) + .expect("overflow constructing `time::Duration`"), + ) + } +} + +impl NumericalStdDuration for f64 { + /// # Panics + /// + /// This will panic if self is negative. + fn std_nanoseconds(self) -> StdDuration { + assert!(self >= 0.); + StdDuration::from_nanos(self as u64) + } + + /// # Panics + /// + /// This will panic if self is negative. + fn std_microseconds(self) -> StdDuration { + assert!(self >= 0.); + StdDuration::from_nanos((self * Nanosecond::per(Microsecond) as Self) as u64) + } + + /// # Panics + /// + /// This will panic if self is negative. + fn std_milliseconds(self) -> StdDuration { + assert!(self >= 0.); + StdDuration::from_nanos((self * Nanosecond::per(Millisecond) as Self) as u64) + } + + /// # Panics + /// + /// This will panic if self is negative. + fn std_seconds(self) -> StdDuration { + assert!(self >= 0.); + StdDuration::from_nanos((self * Nanosecond::per(Second) as Self) as u64) + } + + /// # Panics + /// + /// This will panic if self is negative. + fn std_minutes(self) -> StdDuration { + assert!(self >= 0.); + StdDuration::from_nanos((self * Nanosecond::per(Minute) as Self) as u64) + } + + /// # Panics + /// + /// This will panic if self is negative. + fn std_hours(self) -> StdDuration { + assert!(self >= 0.); + StdDuration::from_nanos((self * Nanosecond::per(Hour) as Self) as u64) + } + + /// # Panics + /// + /// This will panic if self is negative. + fn std_days(self) -> StdDuration { + assert!(self >= 0.); + StdDuration::from_nanos((self * Nanosecond::per(Day) as Self) as u64) + } + + /// # Panics + /// + /// This will panic if self is negative. + fn std_weeks(self) -> StdDuration { + assert!(self >= 0.); + StdDuration::from_nanos((self * Nanosecond::per(Week) as Self) as u64) + } +} |
