use super::sys;
#[repr(transparent)]
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct DispatchTime(pub u64);
const NANOS_PER_SEC: u64 = 1_000_000_000;
const NANOS_PER_MILLI: u64 = 1_000_000;
const NANOS_PER_MICRO: u64 = 1_000;
impl DispatchTime {
pub const IMMEDIATE: DispatchTime = DispatchTime(0);
pub const DISTANT_FUTURE: DispatchTime = DispatchTime(!0);
#[inline]
pub fn now() -> DispatchTime {
DispatchTime::IMMEDIATE.offset_nanos(0)
}
#[inline]
pub fn from_secs_from_now(secs: u64) -> Self {
Self::from_nanos_from_now(secs.saturating_mul(NANOS_PER_SEC))
}
#[inline]
pub fn from_millis_from_now(millis: u64) -> Self {
Self::from_nanos_from_now(millis.saturating_mul(NANOS_PER_MILLI))
}
#[inline]
pub fn from_micros_from_now(micros: u64) -> Self {
Self::from_nanos_from_now(micros.saturating_mul(NANOS_PER_MICRO))
}
#[inline]
pub fn from_nanos_from_now(nanos: u64) -> Self {
let (delta, rem) = match nanos.checked_sub(i64::MAX as u64) {
Some(rem) => (i64::MAX, rem as i64),
None => (nanos as i64, 0),
};
let value = Self::IMMEDIATE.offset_nanos(delta);
if rem == 0 {
value
} else {
value.offset_nanos(rem)
}
}
#[inline]
#[doc(alias = "dispatch_time")]
pub fn offset_nanos(self, delta: i64) -> Self {
unsafe { sys::dispatch_time(self, delta) }
}
}