diff options
Diffstat (limited to 'src/libstd/sys/unix/time.rs')
| -rw-r--r-- | src/libstd/sys/unix/time.rs | 124 |
1 files changed, 43 insertions, 81 deletions
diff --git a/src/libstd/sys/unix/time.rs b/src/libstd/sys/unix/time.rs index a9122defa55..23104419978 100644 --- a/src/libstd/sys/unix/time.rs +++ b/src/libstd/sys/unix/time.rs @@ -15,20 +15,21 @@ struct Timespec { impl Timespec { const fn zero() -> Timespec { - Timespec { - t: libc::timespec { tv_sec: 0, tv_nsec: 0 }, - } + Timespec { t: libc::timespec { tv_sec: 0, tv_nsec: 0 } } } fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> { if self >= other { Ok(if self.t.tv_nsec >= other.t.tv_nsec { - Duration::new((self.t.tv_sec - other.t.tv_sec) as u64, - (self.t.tv_nsec - other.t.tv_nsec) as u32) + Duration::new( + (self.t.tv_sec - other.t.tv_sec) as u64, + (self.t.tv_nsec - other.t.tv_nsec) as u32, + ) } else { - Duration::new((self.t.tv_sec - 1 - other.t.tv_sec) as u64, - self.t.tv_nsec as u32 + (NSEC_PER_SEC as u32) - - other.t.tv_nsec as u32) + Duration::new( + (self.t.tv_sec - 1 - other.t.tv_sec) as u64, + self.t.tv_nsec as u32 + (NSEC_PER_SEC as u32) - other.t.tv_nsec as u32, + ) }) } else { match other.sub_timespec(self) { @@ -52,12 +53,7 @@ impl Timespec { nsec -= NSEC_PER_SEC as u32; secs = secs.checked_add(1)?; } - Some(Timespec { - t: libc::timespec { - tv_sec: secs, - tv_nsec: nsec as _, - }, - }) + Some(Timespec { t: libc::timespec { tv_sec: secs, tv_nsec: nsec as _ } }) } fn checked_sub_duration(&self, other: &Duration) -> Option<Timespec> { @@ -73,12 +69,7 @@ impl Timespec { nsec += NSEC_PER_SEC as i32; secs = secs.checked_sub(1)?; } - Some(Timespec { - t: libc::timespec { - tv_sec: secs, - tv_nsec: nsec as _, - }, - }) + Some(Timespec { t: libc::timespec { tv_sec: secs, tv_nsec: nsec as _ } }) } } @@ -105,7 +96,7 @@ impl Ord for Timespec { } impl Hash for Timespec { - fn hash<H : Hasher>(&self, state: &mut H) { + fn hash<H: Hasher>(&self, state: &mut H) { self.t.tv_sec.hash(state); self.t.tv_nsec.hash(state); } @@ -120,12 +111,12 @@ mod inner { use crate::sys_common::mul_div_u64; use crate::time::Duration; - use super::NSEC_PER_SEC; use super::Timespec; + use super::NSEC_PER_SEC; #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)] pub struct Instant { - t: u64 + t: u64, } #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] @@ -133,9 +124,7 @@ mod inner { t: Timespec, } - pub const UNIX_EPOCH: SystemTime = SystemTime { - t: Timespec::zero(), - }; + pub const UNIX_EPOCH: SystemTime = SystemTime { t: Timespec::zero() }; #[repr(C)] #[derive(Copy, Clone)] @@ -170,15 +159,11 @@ mod inner { } pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> { - Some(Instant { - t: self.t.checked_add(checked_dur2intervals(other)?)?, - }) + Some(Instant { t: self.t.checked_add(checked_dur2intervals(other)?)? }) } pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> { - Some(Instant { - t: self.t.checked_sub(checked_dur2intervals(other)?)?, - }) + Some(Instant { t: self.t.checked_sub(checked_dur2intervals(other)?)? }) } } @@ -186,18 +171,12 @@ mod inner { pub fn now() -> SystemTime { use crate::ptr; - let mut s = libc::timeval { - tv_sec: 0, - tv_usec: 0, - }; - cvt(unsafe { - libc::gettimeofday(&mut s, ptr::null_mut()) - }).unwrap(); - return SystemTime::from(s) + let mut s = libc::timeval { tv_sec: 0, tv_usec: 0 }; + cvt(unsafe { libc::gettimeofday(&mut s, ptr::null_mut()) }).unwrap(); + return SystemTime::from(s); } - pub fn sub_time(&self, other: &SystemTime) - -> Result<Duration, Duration> { + pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> { self.t.sub_timespec(&other.t) } @@ -228,25 +207,21 @@ mod inner { impl fmt::Debug for SystemTime { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("SystemTime") - .field("tv_sec", &self.t.t.tv_sec) - .field("tv_nsec", &self.t.t.tv_nsec) - .finish() + .field("tv_sec", &self.t.t.tv_sec) + .field("tv_nsec", &self.t.t.tv_nsec) + .finish() } } fn checked_dur2intervals(dur: &Duration) -> Option<u64> { - let nanos = dur.as_secs() - .checked_mul(NSEC_PER_SEC)? - .checked_add(dur.subsec_nanos() as u64)?; + let nanos = + dur.as_secs().checked_mul(NSEC_PER_SEC)?.checked_add(dur.subsec_nanos() as u64)?; let info = info(); Some(mul_div_u64(nanos, info.denom as u64, info.numer as u64)) } fn info() -> mach_timebase_info { - static mut INFO: mach_timebase_info = mach_timebase_info { - numer: 0, - denom: 0, - }; + static mut INFO: mach_timebase_info = mach_timebase_info { numer: 0, denom: 0 }; static STATE: AtomicUsize = AtomicUsize::new(0); unsafe { @@ -258,8 +233,7 @@ mod inner { // ... otherwise learn for ourselves ... let mut info = mem::zeroed(); extern "C" { - fn mach_timebase_info(info: mach_timebase_info_t) - -> kern_return_t; + fn mach_timebase_info(info: mach_timebase_info_t) -> kern_return_t; } mach_timebase_info(&mut info); @@ -293,9 +267,7 @@ mod inner { t: Timespec, } - pub const UNIX_EPOCH: SystemTime = SystemTime { - t: Timespec::zero(), - }; + pub const UNIX_EPOCH: SystemTime = SystemTime { t: Timespec::zero() }; impl Instant { pub fn now() -> Instant { @@ -303,16 +275,14 @@ mod inner { } pub const fn zero() -> Instant { - Instant { - t: Timespec::zero(), - } + Instant { t: Timespec::zero() } } pub fn actually_monotonic() -> bool { - (cfg!(target_os = "linux") && cfg!(target_arch = "x86_64")) || - (cfg!(target_os = "linux") && cfg!(target_arch = "x86")) || - cfg!(target_os = "fuchsia") || - false // last clause, used so `||` is always trailing above + (cfg!(target_os = "linux") && cfg!(target_arch = "x86_64")) + || (cfg!(target_os = "linux") && cfg!(target_arch = "x86")) + || cfg!(target_os = "fuchsia") + || false // last clause, used so `||` is always trailing above } pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> { @@ -331,9 +301,9 @@ mod inner { impl fmt::Debug for Instant { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("Instant") - .field("tv_sec", &self.t.t.tv_sec) - .field("tv_nsec", &self.t.t.tv_nsec) - .finish() + .field("tv_sec", &self.t.t.tv_sec) + .field("tv_nsec", &self.t.t.tv_nsec) + .finish() } } @@ -342,8 +312,7 @@ mod inner { SystemTime { t: now(libc::CLOCK_REALTIME) } } - pub fn sub_time(&self, other: &SystemTime) - -> Result<Duration, Duration> { + pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> { self.t.sub_timespec(&other.t) } @@ -365,9 +334,9 @@ mod inner { impl fmt::Debug for SystemTime { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("SystemTime") - .field("tv_sec", &self.t.t.tv_sec) - .field("tv_nsec", &self.t.t.tv_nsec) - .finish() + .field("tv_sec", &self.t.t.tv_sec) + .field("tv_nsec", &self.t.t.tv_nsec) + .finish() } } @@ -377,15 +346,8 @@ mod inner { pub type clock_t = libc::c_ulong; fn now(clock: clock_t) -> Timespec { - let mut t = Timespec { - t: libc::timespec { - tv_sec: 0, - tv_nsec: 0, - } - }; - cvt(unsafe { - libc::clock_gettime(clock, &mut t.t) - }).unwrap(); + let mut t = Timespec { t: libc::timespec { tv_sec: 0, tv_nsec: 0 } }; + cvt(unsafe { libc::clock_gettime(clock, &mut t.t) }).unwrap(); t } } |
