use core::time::Duration as StdDuration;
use crate::convert::*;
mod sealed {
pub trait Sealed {}
impl Sealed for u64 {}
impl Sealed for f64 {}
}
#[diagnostic::on_unimplemented(note = "this extension trait is intended to be used with numeric \
literals, such as `5.std_seconds()`")]
pub trait NumericalStdDuration: sealed::Sealed {
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;
}
impl NumericalStdDuration for u64 {
#[inline]
fn std_nanoseconds(self) -> StdDuration {
StdDuration::from_nanos(self)
}
#[inline]
fn std_microseconds(self) -> StdDuration {
StdDuration::from_micros(self)
}
#[inline]
fn std_milliseconds(self) -> StdDuration {
StdDuration::from_millis(self)
}
#[inline]
fn std_seconds(self) -> StdDuration {
StdDuration::from_secs(self)
}
#[inline]
#[track_caller]
fn std_minutes(self) -> StdDuration {
StdDuration::from_secs(
self.checked_mul(Second::per_t(Minute))
.expect("overflow constructing `time::Duration`"),
)
}
#[inline]
#[track_caller]
fn std_hours(self) -> StdDuration {
StdDuration::from_secs(
self.checked_mul(Second::per_t(Hour))
.expect("overflow constructing `time::Duration`"),
)
}
#[inline]
#[track_caller]
fn std_days(self) -> StdDuration {
StdDuration::from_secs(
self.checked_mul(Second::per_t(Day))
.expect("overflow constructing `time::Duration`"),
)
}
#[inline]
#[track_caller]
fn std_weeks(self) -> StdDuration {
StdDuration::from_secs(
self.checked_mul(Second::per_t(Week))
.expect("overflow constructing `time::Duration`"),
)
}
}
impl NumericalStdDuration for f64 {
#[inline]
#[track_caller]
fn std_nanoseconds(self) -> StdDuration {
assert!(self >= 0.);
StdDuration::from_nanos(self as u64)
}
#[inline]
#[track_caller]
fn std_microseconds(self) -> StdDuration {
assert!(self >= 0.);
StdDuration::from_nanos((self * Nanosecond::per_t::<Self>(Microsecond)) as u64)
}
#[inline]
#[track_caller]
fn std_milliseconds(self) -> StdDuration {
assert!(self >= 0.);
StdDuration::from_nanos((self * Nanosecond::per_t::<Self>(Millisecond)) as u64)
}
#[inline]
#[track_caller]
fn std_seconds(self) -> StdDuration {
assert!(self >= 0.);
StdDuration::from_nanos((self * Nanosecond::per_t::<Self>(Second)) as u64)
}
#[inline]
#[track_caller]
fn std_minutes(self) -> StdDuration {
assert!(self >= 0.);
StdDuration::from_nanos((self * Nanosecond::per_t::<Self>(Minute)) as u64)
}
#[inline]
#[track_caller]
fn std_hours(self) -> StdDuration {
assert!(self >= 0.);
StdDuration::from_nanos((self * Nanosecond::per_t::<Self>(Hour)) as u64)
}
#[inline]
#[track_caller]
fn std_days(self) -> StdDuration {
assert!(self >= 0.);
StdDuration::from_nanos((self * Nanosecond::per_t::<Self>(Day)) as u64)
}
#[inline]
#[track_caller]
fn std_weeks(self) -> StdDuration {
assert!(self >= 0.);
StdDuration::from_nanos((self * Nanosecond::per_t::<Self>(Week)) as u64)
}
}