#![allow(trivial_numeric_casts)]
use crate::Duration;
use core::time::Duration as StdDuration;
pub trait NumericalDuration {
fn nanoseconds(self) -> Duration;
fn microseconds(self) -> Duration;
fn milliseconds(self) -> Duration;
fn seconds(self) -> Duration;
fn minutes(self) -> Duration;
fn hours(self) -> Duration;
fn days(self) -> Duration;
fn weeks(self) -> Duration;
}
macro_rules! impl_numerical_duration {
($($type:ty),* $(,)?) => {
$(
impl NumericalDuration for $type {
#[inline(always)]
fn nanoseconds(self) -> Duration {
Duration::nanoseconds(self as i64)
}
#[inline(always)]
fn microseconds(self) -> Duration {
Duration::microseconds(self as i64)
}
#[inline(always)]
fn milliseconds(self) -> Duration {
Duration::milliseconds(self as i64)
}
#[inline(always)]
fn seconds(self) -> Duration {
Duration::seconds(self as i64)
}
#[inline(always)]
fn minutes(self) -> Duration {
Duration::minutes(self as i64)
}
#[inline(always)]
fn hours(self) -> Duration {
Duration::hours(self as i64)
}
#[inline(always)]
fn days(self) -> Duration {
Duration::days(self as i64)
}
#[inline(always)]
fn weeks(self) -> Duration {
Duration::weeks(self as i64)
}
}
)*
};
}
macro_rules! impl_numerical_duration_nonzero {
($($type:ty),* $(,)?) => {
$(
impl NumericalDuration for $type {
#[inline(always)]
fn nanoseconds(self) -> Duration {
Duration::nanoseconds(self.get() as i64)
}
#[inline(always)]
fn microseconds(self) -> Duration {
Duration::microseconds(self.get() as i64)
}
#[inline(always)]
fn milliseconds(self) -> Duration {
Duration::milliseconds(self.get() as i64)
}
#[inline(always)]
fn seconds(self) -> Duration {
Duration::seconds(self.get() as i64)
}
#[inline(always)]
fn minutes(self) -> Duration {
Duration::minutes(self.get() as i64)
}
#[inline(always)]
fn hours(self) -> Duration {
Duration::hours(self.get() as i64)
}
#[inline(always)]
fn days(self) -> Duration {
Duration::days(self.get() as i64)
}
#[inline(always)]
fn weeks(self) -> Duration {
Duration::weeks(self.get() as i64)
}
}
)*
};
}
macro_rules! impl_numerical_duration_float {
($($type:ty),* $(,)?) => {
$(
impl NumericalDuration for $type {
#[inline(always)]
fn nanoseconds(self) -> Duration {
Duration::nanoseconds(self as i64)
}
#[inline]
fn microseconds(self) -> Duration {
Duration::nanoseconds((self * 1_000.) as i64)
}
#[inline]
fn milliseconds(self) -> Duration {
Duration::nanoseconds((self * 1_000_000.) as i64)
}
#[inline]
fn seconds(self) -> Duration {
Duration::nanoseconds((self * 1_000_000_000.) as i64)
}
#[inline]
fn minutes(self) -> Duration {
Duration::nanoseconds((self * 60_000_000_000.) as i64)
}
#[inline]
fn hours(self) -> Duration {
Duration::nanoseconds((self * 3_600_000_000_000.) as i64)
}
#[inline]
fn days(self) -> Duration {
Duration::nanoseconds((self * 86_400_000_000_000.) as i64)
}
#[inline]
fn weeks(self) -> Duration {
Duration::nanoseconds((self * 604_800_000_000_000.) as i64)
}
}
)*
};
}
impl_numerical_duration![u8, u16, u32, i8, i16, i32, i64];
impl_numerical_duration_nonzero![
core::num::NonZeroU8,
core::num::NonZeroU16,
core::num::NonZeroU32,
];
#[cfg(nonzero_signed)]
impl_numerical_duration_nonzero![
core::num::NonZeroI8,
core::num::NonZeroI16,
core::num::NonZeroI32,
core::num::NonZeroI64,
];
impl_numerical_duration_float![f32, f64];
pub trait NumericalStdDuration {
fn std_nanoseconds(self) -> StdDuration;
fn std_microseconds(self) -> StdDuration;
fn std_milliseconds(self) -> StdDuration;
fn std_seconds(self) -> StdDuration;
fn std_minutes(self) -> StdDuration;
fn std_hours(self) -> StdDuration;
fn std_days(self) -> StdDuration;
fn std_weeks(self) -> StdDuration;
}
macro_rules! impl_numerical_std_duration {
($($type:ty),* $(,)?) => {
$(
impl NumericalStdDuration for $type {
#[inline(always)]
fn std_nanoseconds(self) -> StdDuration {
StdDuration::from_nanos(self as u64)
}
#[inline(always)]
fn std_microseconds(self) -> StdDuration {
StdDuration::from_micros(self as u64)
}
#[inline(always)]
fn std_milliseconds(self) -> StdDuration {
StdDuration::from_millis(self as u64)
}
#[inline(always)]
fn std_seconds(self) -> StdDuration {
StdDuration::from_secs(self as u64)
}
#[inline(always)]
fn std_minutes(self) -> StdDuration {
StdDuration::from_secs(self as u64 * 60)
}
#[inline(always)]
fn std_hours(self) -> StdDuration {
StdDuration::from_secs(self as u64 * 3_600)
}
#[inline(always)]
fn std_days(self) -> StdDuration {
StdDuration::from_secs(self as u64 * 86_400)
}
#[inline(always)]
fn std_weeks(self) -> StdDuration {
StdDuration::from_secs(self as u64 * 604_800)
}
}
)*
};
}
macro_rules! impl_numerical_std_duration_nonzero {
($($type:ty),* $(,)?) => {
$(
impl NumericalStdDuration for $type {
#[inline(always)]
fn std_nanoseconds(self) -> StdDuration {
StdDuration::from_nanos(self.get() as u64)
}
#[inline(always)]
fn std_microseconds(self) -> StdDuration {
StdDuration::from_micros(self.get() as u64)
}
#[inline(always)]
fn std_milliseconds(self) -> StdDuration {
StdDuration::from_millis(self.get() as u64)
}
#[inline(always)]
fn std_seconds(self) -> StdDuration {
StdDuration::from_secs(self.get() as u64)
}
#[inline(always)]
fn std_minutes(self) -> StdDuration {
StdDuration::from_secs(self.get() as u64 * 60)
}
#[inline(always)]
fn std_hours(self) -> StdDuration {
StdDuration::from_secs(self.get() as u64 * 3_600)
}
#[inline(always)]
fn std_days(self) -> StdDuration {
StdDuration::from_secs(self.get() as u64 * 86_400)
}
#[inline(always)]
fn std_weeks(self) -> StdDuration {
StdDuration::from_secs(self.get() as u64 * 604_800)
}
}
)*
};
}
impl_numerical_std_duration![u8, u16, u32, u64];
impl_numerical_std_duration_nonzero![
core::num::NonZeroU8,
core::num::NonZeroU16,
core::num::NonZeroU32,
core::num::NonZeroU64,
];
impl NumericalStdDuration for i32 {
#[inline(always)]
fn std_nanoseconds(self) -> StdDuration {
assert!(self >= 0);
StdDuration::from_nanos(self as u64)
}
#[inline(always)]
fn std_microseconds(self) -> StdDuration {
assert!(self >= 0);
StdDuration::from_micros(self as u64)
}
#[inline(always)]
fn std_milliseconds(self) -> StdDuration {
assert!(self >= 0);
StdDuration::from_millis(self as u64)
}
#[inline(always)]
fn std_seconds(self) -> StdDuration {
assert!(self >= 0);
StdDuration::from_secs(self as u64)
}
#[inline(always)]
fn std_minutes(self) -> StdDuration {
assert!(self >= 0);
StdDuration::from_secs(self as u64 * 60)
}
#[inline(always)]
fn std_hours(self) -> StdDuration {
assert!(self >= 0);
StdDuration::from_secs(self as u64 * 3_600)
}
#[inline(always)]
fn std_days(self) -> StdDuration {
assert!(self >= 0);
StdDuration::from_secs(self as u64 * 86_400)
}
#[inline(always)]
fn std_weeks(self) -> StdDuration {
assert!(self >= 0);
StdDuration::from_secs(self as u64 * 604_800)
}
}
impl NumericalStdDuration for f64 {
#[inline(always)]
fn std_nanoseconds(self) -> StdDuration {
assert!(self >= 0.);
StdDuration::from_nanos(self as u64)
}
#[inline]
fn std_microseconds(self) -> StdDuration {
assert!(self >= 0.);
StdDuration::from_nanos((self * 1_000.) as u64)
}
#[inline]
fn std_milliseconds(self) -> StdDuration {
assert!(self >= 0.);
StdDuration::from_nanos((self * 1_000_000.) as u64)
}
#[inline]
fn std_seconds(self) -> StdDuration {
assert!(self >= 0.);
StdDuration::from_nanos((self * 1_000_000_000.) as u64)
}
#[inline]
fn std_minutes(self) -> StdDuration {
assert!(self >= 0.);
StdDuration::from_nanos((self * 60_000_000_000.) as u64)
}
#[inline]
fn std_hours(self) -> StdDuration {
assert!(self >= 0.);
StdDuration::from_nanos((self * 3_600_000_000_000.) as u64)
}
#[inline]
fn std_days(self) -> StdDuration {
assert!(self >= 0.);
StdDuration::from_nanos((self * 86_400_000_000_000.) as u64)
}
#[inline]
fn std_weeks(self) -> StdDuration {
assert!(self >= 0.);
StdDuration::from_nanos((self * 604_800_000_000_000.) as u64)
}
}
pub trait NumericalStdDurationShort {
fn nanoseconds(self) -> StdDuration;
fn microseconds(self) -> StdDuration;
fn milliseconds(self) -> StdDuration;
fn seconds(self) -> StdDuration;
fn minutes(self) -> StdDuration;
fn hours(self) -> StdDuration;
fn days(self) -> StdDuration;
fn weeks(self) -> StdDuration;
}
macro_rules! impl_numerical_std_duration {
($($type:ty),* $(,)?) => {
$(
impl NumericalStdDurationShort for $type {
#[inline(always)]
fn nanoseconds(self) -> StdDuration {
StdDuration::from_nanos(self as u64)
}
#[inline(always)]
fn microseconds(self) -> StdDuration {
StdDuration::from_micros(self as u64)
}
#[inline(always)]
fn milliseconds(self) -> StdDuration {
StdDuration::from_millis(self as u64)
}
#[inline(always)]
fn seconds(self) -> StdDuration {
StdDuration::from_secs(self as u64)
}
#[inline(always)]
fn minutes(self) -> StdDuration {
StdDuration::from_secs(self as u64 * 60)
}
#[inline(always)]
fn hours(self) -> StdDuration {
StdDuration::from_secs(self as u64 * 3_600)
}
#[inline(always)]
fn days(self) -> StdDuration {
StdDuration::from_secs(self as u64 * 86_400)
}
#[inline(always)]
fn weeks(self) -> StdDuration {
StdDuration::from_secs(self as u64 * 604_800)
}
}
)*
};
}
macro_rules! impl_numerical_std_duration_nonzero {
($($type:ty),* $(,)?) => {
$(
impl NumericalStdDurationShort for $type {
#[inline(always)]
fn nanoseconds(self) -> StdDuration {
StdDuration::from_nanos(self.get() as u64)
}
#[inline(always)]
fn microseconds(self) -> StdDuration {
StdDuration::from_micros(self.get() as u64)
}
#[inline(always)]
fn milliseconds(self) -> StdDuration {
StdDuration::from_millis(self.get() as u64)
}
#[inline(always)]
fn seconds(self) -> StdDuration {
StdDuration::from_secs(self.get() as u64)
}
#[inline(always)]
fn minutes(self) -> StdDuration {
StdDuration::from_secs(self.get() as u64 * 60)
}
#[inline(always)]
fn hours(self) -> StdDuration {
StdDuration::from_secs(self.get() as u64 * 3_600)
}
#[inline(always)]
fn days(self) -> StdDuration {
StdDuration::from_secs(self.get() as u64 * 86_400)
}
#[inline(always)]
fn weeks(self) -> StdDuration {
StdDuration::from_secs(self.get() as u64 * 604_800)
}
}
)*
};
}
impl_numerical_std_duration![u8, u16, u32, u64];
impl_numerical_std_duration_nonzero![
core::num::NonZeroU8,
core::num::NonZeroU16,
core::num::NonZeroU32,
core::num::NonZeroU64,
];
impl NumericalStdDurationShort for i32 {
#[inline(always)]
fn nanoseconds(self) -> StdDuration {
assert!(self >= 0);
StdDuration::from_nanos(self as u64)
}
#[inline(always)]
fn microseconds(self) -> StdDuration {
assert!(self >= 0);
StdDuration::from_micros(self as u64)
}
#[inline(always)]
fn milliseconds(self) -> StdDuration {
assert!(self >= 0);
StdDuration::from_millis(self as u64)
}
#[inline(always)]
fn seconds(self) -> StdDuration {
assert!(self >= 0);
StdDuration::from_secs(self as u64)
}
#[inline(always)]
fn minutes(self) -> StdDuration {
assert!(self >= 0);
StdDuration::from_secs(self as u64 * 60)
}
#[inline(always)]
fn hours(self) -> StdDuration {
assert!(self >= 0);
StdDuration::from_secs(self as u64 * 3_600)
}
#[inline(always)]
fn days(self) -> StdDuration {
assert!(self >= 0);
StdDuration::from_secs(self as u64 * 86_400)
}
#[inline(always)]
fn weeks(self) -> StdDuration {
assert!(self >= 0);
StdDuration::from_secs(self as u64 * 604_800)
}
}
impl NumericalStdDurationShort for f64 {
#[inline(always)]
fn nanoseconds(self) -> StdDuration {
assert!(self >= 0.);
StdDuration::from_nanos(self as u64)
}
#[inline]
fn microseconds(self) -> StdDuration {
assert!(self >= 0.);
StdDuration::from_nanos((self * 1_000.) as u64)
}
#[inline]
fn milliseconds(self) -> StdDuration {
assert!(self >= 0.);
StdDuration::from_nanos((self * 1_000_000.) as u64)
}
#[inline]
fn seconds(self) -> StdDuration {
assert!(self >= 0.);
StdDuration::from_nanos((self * 1_000_000_000.) as u64)
}
#[inline]
fn minutes(self) -> StdDuration {
assert!(self >= 0.);
StdDuration::from_nanos((self * 60_000_000_000.) as u64)
}
#[inline]
fn hours(self) -> StdDuration {
assert!(self >= 0.);
StdDuration::from_nanos((self * 3_600_000_000_000.) as u64)
}
#[inline]
fn days(self) -> StdDuration {
assert!(self >= 0.);
StdDuration::from_nanos((self * 86_400_000_000_000.) as u64)
}
#[inline]
fn weeks(self) -> StdDuration {
assert!(self >= 0.);
StdDuration::from_nanos((self * 604_800_000_000_000.) as u64)
}
}
#[cfg(test)]
mod test_numerical_duration {
use super::{Duration, NumericalDuration};
#[test]
fn unsigned() {
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));
}
#[test]
fn signed() {
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));
}
#[test]
fn float() {
assert_eq!(1.9.nanoseconds(), Duration::nanoseconds(1));
assert_eq!(1.0.nanoseconds(), Duration::nanoseconds(1));
assert_eq!(1.0.microseconds(), Duration::microseconds(1));
assert_eq!(1.0.milliseconds(), Duration::milliseconds(1));
assert_eq!(1.0.seconds(), Duration::seconds(1));
assert_eq!(1.0.minutes(), Duration::minutes(1));
assert_eq!(1.0.hours(), Duration::hours(1));
assert_eq!(1.0.days(), Duration::days(1));
assert_eq!(1.0.weeks(), Duration::weeks(1));
assert_eq!(1.5.nanoseconds(), Duration::nanoseconds(1));
assert_eq!(1.5.microseconds(), Duration::nanoseconds(1_500));
assert_eq!(1.5.milliseconds(), Duration::microseconds(1_500));
assert_eq!(1.5.seconds(), Duration::milliseconds(1_500));
assert_eq!(1.5.minutes(), Duration::seconds(90));
assert_eq!(1.5.hours(), Duration::minutes(90));
assert_eq!(1.5.days(), Duration::hours(36));
assert_eq!(1.5.weeks(), Duration::hours(252));
}
#[test]
fn arithmetic() {
assert_eq!(2.seconds() + 500.milliseconds(), 2_500.milliseconds());
assert_eq!(2.seconds() - 500.milliseconds(), 1_500.milliseconds());
}
}
#[cfg(test)]
mod test_numerical_std_duration {
use super::NumericalStdDuration;
use core::time::Duration;
#[test]
fn unsigned() {
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));
}
#[test]
fn float() {
assert_eq!(1.9.std_nanoseconds(), Duration::from_nanos(1));
assert_eq!(1.0.std_nanoseconds(), Duration::from_nanos(1));
assert_eq!(1.0.std_microseconds(), Duration::from_micros(1));
assert_eq!(1.0.std_milliseconds(), Duration::from_millis(1));
assert_eq!(1.0.std_seconds(), Duration::from_secs(1));
assert_eq!(1.0.std_minutes(), Duration::from_secs(60));
assert_eq!(1.0.std_hours(), Duration::from_secs(3_600));
assert_eq!(1.0.std_days(), Duration::from_secs(86_400));
assert_eq!(1.0.std_weeks(), Duration::from_secs(604_800));
assert_eq!(1.5.std_nanoseconds(), Duration::from_nanos(1));
assert_eq!(1.5.std_microseconds(), Duration::from_nanos(1_500));
assert_eq!(1.5.std_milliseconds(), Duration::from_micros(1_500));
assert_eq!(1.5.std_seconds(), Duration::from_millis(1_500));
assert_eq!(1.5.std_minutes(), Duration::from_secs(90));
assert_eq!(1.5.std_hours(), Duration::from_secs(90 * 60));
assert_eq!(1.5.std_days(), Duration::from_secs(36 * 3_600));
assert_eq!(1.5.std_weeks(), Duration::from_secs(252 * 3_600));
}
#[test]
fn arithmetic() {
assert_eq!(
2.std_seconds() + 500.std_milliseconds(),
2_500.std_milliseconds()
);
assert_eq!(
2.std_seconds() - 500.std_milliseconds(),
1_500.std_milliseconds()
);
}
}
#[cfg(test)]
mod test_numerical_std_duration_short {
use super::NumericalStdDurationShort;
use core::time::Duration;
#[test]
fn unsigned() {
assert_eq!(5.nanoseconds(), Duration::from_nanos(5));
assert_eq!(5.microseconds(), Duration::from_micros(5));
assert_eq!(5.milliseconds(), Duration::from_millis(5));
assert_eq!(5.seconds(), Duration::from_secs(5));
assert_eq!(5.minutes(), Duration::from_secs(5 * 60));
assert_eq!(5.hours(), Duration::from_secs(5 * 3_600));
assert_eq!(5.days(), Duration::from_secs(5 * 86_400));
assert_eq!(5.weeks(), Duration::from_secs(5 * 604_800));
}
#[test]
fn float() {
assert_eq!(1.9.nanoseconds(), Duration::from_nanos(1));
assert_eq!(1.0.nanoseconds(), Duration::from_nanos(1));
assert_eq!(1.0.microseconds(), Duration::from_micros(1));
assert_eq!(1.0.milliseconds(), Duration::from_millis(1));
assert_eq!(1.0.seconds(), Duration::from_secs(1));
assert_eq!(1.0.minutes(), Duration::from_secs(60));
assert_eq!(1.0.hours(), Duration::from_secs(3_600));
assert_eq!(1.0.days(), Duration::from_secs(86_400));
assert_eq!(1.0.weeks(), Duration::from_secs(604_800));
assert_eq!(1.5.nanoseconds(), Duration::from_nanos(1));
assert_eq!(1.5.microseconds(), Duration::from_nanos(1_500));
assert_eq!(1.5.milliseconds(), Duration::from_micros(1_500));
assert_eq!(1.5.seconds(), Duration::from_millis(1_500));
assert_eq!(1.5.minutes(), Duration::from_secs(90));
assert_eq!(1.5.hours(), Duration::from_secs(90 * 60));
assert_eq!(1.5.days(), Duration::from_secs(36 * 3_600));
assert_eq!(1.5.weeks(), Duration::from_secs(252 * 3_600));
}
#[test]
fn arithmetic() {
assert_eq!(2.seconds() + 500.milliseconds(), 2_500.milliseconds());
assert_eq!(2.seconds() - 500.milliseconds(), 1_500.milliseconds());
}
}