summaryrefslogtreecommitdiff
path: root/src/sys/time.rs
diff options
context:
space:
mode:
authorCostin-Robert Sin <sin.costinrobert@gmail.com>2022-06-21 15:36:05 +0300
committerCostin-Robert Sin <sin.costinrobert@gmail.com>2022-06-24 00:35:52 +0300
commit3e6cb639f0d9afde57d9cc03526c2e488641231b (patch)
treec882f2947fa5b22e67a58918c45012655b892b30 /src/sys/time.rs
parent8f08a69c28fad5ee875d69e4c8a1b8eed2203cb4 (diff)
downloadnix-3e6cb639f0d9afde57d9cc03526c2e488641231b.zip
Fix all formating problems to pass CI formating test
Signed-off-by: Costin-Robert Sin <sin.costinrobert@gmail.com>
Diffstat (limited to 'src/sys/time.rs')
-rw-r--r--src/sys/time.rs193
1 files changed, 124 insertions, 69 deletions
diff --git a/src/sys/time.rs b/src/sys/time.rs
index c29259b2..c559cc47 100644
--- a/src/sys/time.rs
+++ b/src/sys/time.rs
@@ -1,9 +1,10 @@
-use std::{cmp, fmt, ops};
-use std::time::Duration;
-use std::convert::From;
+#[cfg_attr(target_env = "musl", allow(deprecated))]
+// https://github.com/rust-lang/libc/issues/1848
+pub use libc::{suseconds_t, time_t};
use libc::{timespec, timeval};
-#[cfg_attr(target_env = "musl", allow(deprecated))] // https://github.com/rust-lang/libc/issues/1848
-pub use libc::{time_t, suseconds_t};
+use std::convert::From;
+use std::time::Duration;
+use std::{cmp, fmt, ops};
#[cfg(any(
all(feature = "time", any(target_os = "android", target_os = "linux")),
@@ -62,10 +63,12 @@ pub(crate) mod timer {
},
it_value: *t.as_ref(),
}),
- Expiration::IntervalDelayed(start, interval) => TimerSpec(libc::itimerspec {
- it_interval: *interval.as_ref(),
- it_value: *start.as_ref(),
- }),
+ Expiration::IntervalDelayed(start, interval) => {
+ TimerSpec(libc::itimerspec {
+ it_interval: *interval.as_ref(),
+ it_value: *start.as_ref(),
+ })
+ }
Expiration::Interval(t) => TimerSpec(libc::itimerspec {
it_interval: *t.as_ref(),
it_value: *t.as_ref(),
@@ -94,7 +97,12 @@ pub(crate) mod timer {
const TFD_TIMER_ABSTIME = libc::TFD_TIMER_ABSTIME;
}
}
- #[cfg(any(target_os = "freebsd", target_os = "netbsd", target_os = "dragonfly", target_os = "illumos"))]
+ #[cfg(any(
+ target_os = "freebsd",
+ target_os = "netbsd",
+ target_os = "dragonfly",
+ target_os = "illumos"
+ ))]
bitflags! {
/// Flags that are used for arming the timer.
pub struct TimerSetTimeFlags: libc::c_int {
@@ -117,10 +125,15 @@ pub(crate) mod timer {
it_interval: int_ts,
it_value: val_ts,
}) => {
- if (int_ts.tv_sec == val_ts.tv_sec) && (int_ts.tv_nsec == val_ts.tv_nsec) {
+ if (int_ts.tv_sec == val_ts.tv_sec)
+ && (int_ts.tv_nsec == val_ts.tv_nsec)
+ {
Expiration::Interval(int_ts.into())
} else {
- Expiration::IntervalDelayed(val_ts.into(), int_ts.into())
+ Expiration::IntervalDelayed(
+ val_ts.into(),
+ int_ts.into(),
+ )
}
}
}
@@ -136,14 +149,16 @@ pub trait TimeValLike: Sized {
#[inline]
fn hours(hours: i64) -> Self {
- let secs = hours.checked_mul(SECS_PER_HOUR)
+ let secs = hours
+ .checked_mul(SECS_PER_HOUR)
.expect("TimeValLike::hours ouf of bounds");
Self::seconds(secs)
}
#[inline]
fn minutes(minutes: i64) -> Self {
- let secs = minutes.checked_mul(SECS_PER_MINUTE)
+ let secs = minutes
+ .checked_mul(SECS_PER_MINUTE)
.expect("TimeValLike::minutes out of bounds");
Self::seconds(secs)
}
@@ -243,15 +258,23 @@ impl PartialOrd for TimeSpec {
impl TimeValLike for TimeSpec {
#[inline]
fn seconds(seconds: i64) -> TimeSpec {
- assert!((TS_MIN_SECONDS..=TS_MAX_SECONDS).contains(&seconds),
- "TimeSpec out of bounds; seconds={}", seconds);
- #[cfg_attr(target_env = "musl", allow(deprecated))] // https://github.com/rust-lang/libc/issues/1848
- TimeSpec(timespec {tv_sec: seconds as time_t, tv_nsec: 0 })
+ assert!(
+ (TS_MIN_SECONDS..=TS_MAX_SECONDS).contains(&seconds),
+ "TimeSpec out of bounds; seconds={}",
+ seconds
+ );
+ #[cfg_attr(target_env = "musl", allow(deprecated))]
+ // https://github.com/rust-lang/libc/issues/1848
+ TimeSpec(timespec {
+ tv_sec: seconds as time_t,
+ tv_nsec: 0,
+ })
}
#[inline]
fn milliseconds(milliseconds: i64) -> TimeSpec {
- let nanoseconds = milliseconds.checked_mul(1_000_000)
+ let nanoseconds = milliseconds
+ .checked_mul(1_000_000)
.expect("TimeSpec::milliseconds out of bounds");
TimeSpec::nanoseconds(nanoseconds)
@@ -260,7 +283,8 @@ impl TimeValLike for TimeSpec {
/// Makes a new `TimeSpec` with given number of microseconds.
#[inline]
fn microseconds(microseconds: i64) -> TimeSpec {
- let nanoseconds = microseconds.checked_mul(1_000)
+ let nanoseconds = microseconds
+ .checked_mul(1_000)
.expect("TimeSpec::milliseconds out of bounds");
TimeSpec::nanoseconds(nanoseconds)
@@ -270,11 +294,16 @@ impl TimeValLike for TimeSpec {
#[inline]
fn nanoseconds(nanoseconds: i64) -> TimeSpec {
let (secs, nanos) = div_mod_floor_64(nanoseconds, NANOS_PER_SEC);
- assert!((TS_MIN_SECONDS..=TS_MAX_SECONDS).contains(&secs),
- "TimeSpec out of bounds");
- #[cfg_attr(target_env = "musl", allow(deprecated))] // https://github.com/rust-lang/libc/issues/1848
- TimeSpec(timespec {tv_sec: secs as time_t,
- tv_nsec: nanos as timespec_tv_nsec_t })
+ assert!(
+ (TS_MIN_SECONDS..=TS_MAX_SECONDS).contains(&secs),
+ "TimeSpec out of bounds"
+ );
+ #[cfg_attr(target_env = "musl", allow(deprecated))]
+ // https://github.com/rust-lang/libc/issues/1848
+ TimeSpec(timespec {
+ tv_sec: secs as time_t,
+ tv_nsec: nanos as timespec_tv_nsec_t,
+ })
}
fn num_seconds(&self) -> i64 {
@@ -319,10 +348,11 @@ impl TimeSpec {
}
pub const fn from_duration(duration: Duration) -> Self {
- #[cfg_attr(target_env = "musl", allow(deprecated))] // https://github.com/rust-lang/libc/issues/1848
+ #[cfg_attr(target_env = "musl", allow(deprecated))]
+ // https://github.com/rust-lang/libc/issues/1848
TimeSpec(timespec {
tv_sec: duration.as_secs() as time_t,
- tv_nsec: duration.subsec_nanos() as timespec_tv_nsec_t
+ tv_nsec: duration.subsec_nanos() as timespec_tv_nsec_t,
})
}
@@ -343,8 +373,7 @@ impl ops::Add for TimeSpec {
type Output = TimeSpec;
fn add(self, rhs: TimeSpec) -> TimeSpec {
- TimeSpec::nanoseconds(
- self.num_nanoseconds() + rhs.num_nanoseconds())
+ TimeSpec::nanoseconds(self.num_nanoseconds() + rhs.num_nanoseconds())
}
}
@@ -352,8 +381,7 @@ impl ops::Sub for TimeSpec {
type Output = TimeSpec;
fn sub(self, rhs: TimeSpec) -> TimeSpec {
- TimeSpec::nanoseconds(
- self.num_nanoseconds() - rhs.num_nanoseconds())
+ TimeSpec::nanoseconds(self.num_nanoseconds() - rhs.num_nanoseconds())
}
}
@@ -361,7 +389,9 @@ impl ops::Mul<i32> for TimeSpec {
type Output = TimeSpec;
fn mul(self, rhs: i32) -> TimeSpec {
- let usec = self.num_nanoseconds().checked_mul(i64::from(rhs))
+ let usec = self
+ .num_nanoseconds()
+ .checked_mul(i64::from(rhs))
.expect("TimeSpec multiply out of bounds");
TimeSpec::nanoseconds(usec)
@@ -407,8 +437,6 @@ impl fmt::Display for TimeSpec {
}
}
-
-
#[repr(transparent)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct TimeVal(timeval);
@@ -456,15 +484,23 @@ impl PartialOrd for TimeVal {
impl TimeValLike for TimeVal {
#[inline]
fn seconds(seconds: i64) -> TimeVal {
- assert!((TV_MIN_SECONDS..=TV_MAX_SECONDS).contains(&seconds),
- "TimeVal out of bounds; seconds={}", seconds);
- #[cfg_attr(target_env = "musl", allow(deprecated))] // https://github.com/rust-lang/libc/issues/1848
- TimeVal(timeval {tv_sec: seconds as time_t, tv_usec: 0 })
+ assert!(
+ (TV_MIN_SECONDS..=TV_MAX_SECONDS).contains(&seconds),
+ "TimeVal out of bounds; seconds={}",
+ seconds
+ );
+ #[cfg_attr(target_env = "musl", allow(deprecated))]
+ // https://github.com/rust-lang/libc/issues/1848
+ TimeVal(timeval {
+ tv_sec: seconds as time_t,
+ tv_usec: 0,
+ })
}
#[inline]
fn milliseconds(milliseconds: i64) -> TimeVal {
- let microseconds = milliseconds.checked_mul(1_000)
+ let microseconds = milliseconds
+ .checked_mul(1_000)
.expect("TimeVal::milliseconds out of bounds");
TimeVal::microseconds(microseconds)
@@ -474,11 +510,16 @@ impl TimeValLike for TimeVal {
#[inline]
fn microseconds(microseconds: i64) -> TimeVal {
let (secs, micros) = div_mod_floor_64(microseconds, MICROS_PER_SEC);
- assert!((TV_MIN_SECONDS..=TV_MAX_SECONDS).contains(&secs),
- "TimeVal out of bounds");
- #[cfg_attr(target_env = "musl", allow(deprecated))] // https://github.com/rust-lang/libc/issues/1848
- TimeVal(timeval {tv_sec: secs as time_t,
- tv_usec: micros as suseconds_t })
+ assert!(
+ (TV_MIN_SECONDS..=TV_MAX_SECONDS).contains(&secs),
+ "TimeVal out of bounds"
+ );
+ #[cfg_attr(target_env = "musl", allow(deprecated))]
+ // https://github.com/rust-lang/libc/issues/1848
+ TimeVal(timeval {
+ tv_sec: secs as time_t,
+ tv_usec: micros as suseconds_t,
+ })
}
/// Makes a new `TimeVal` with given number of nanoseconds. Some precision
@@ -487,11 +528,16 @@ impl TimeValLike for TimeVal {
fn nanoseconds(nanoseconds: i64) -> TimeVal {
let microseconds = nanoseconds / 1000;
let (secs, micros) = div_mod_floor_64(microseconds, MICROS_PER_SEC);
- assert!((TV_MIN_SECONDS..=TV_MAX_SECONDS).contains(&secs),
- "TimeVal out of bounds");
- #[cfg_attr(target_env = "musl", allow(deprecated))] // https://github.com/rust-lang/libc/issues/1848
- TimeVal(timeval {tv_sec: secs as time_t,
- tv_usec: micros as suseconds_t })
+ assert!(
+ (TV_MIN_SECONDS..=TV_MAX_SECONDS).contains(&secs),
+ "TimeVal out of bounds"
+ );
+ #[cfg_attr(target_env = "musl", allow(deprecated))]
+ // https://github.com/rust-lang/libc/issues/1848
+ TimeVal(timeval {
+ tv_sec: secs as time_t,
+ tv_usec: micros as suseconds_t,
+ })
}
fn num_seconds(&self) -> i64 {
@@ -548,8 +594,7 @@ impl ops::Add for TimeVal {
type Output = TimeVal;
fn add(self, rhs: TimeVal) -> TimeVal {
- TimeVal::microseconds(
- self.num_microseconds() + rhs.num_microseconds())
+ TimeVal::microseconds(self.num_microseconds() + rhs.num_microseconds())
}
}
@@ -557,8 +602,7 @@ impl ops::Sub for TimeVal {
type Output = TimeVal;
fn sub(self, rhs: TimeVal) -> TimeVal {
- TimeVal::microseconds(
- self.num_microseconds() - rhs.num_microseconds())
+ TimeVal::microseconds(self.num_microseconds() - rhs.num_microseconds())
}
}
@@ -566,7 +610,9 @@ impl ops::Mul<i32> for TimeVal {
type Output = TimeVal;
fn mul(self, rhs: i32) -> TimeVal {
- let usec = self.num_microseconds().checked_mul(i64::from(rhs))
+ let usec = self
+ .num_microseconds()
+ .checked_mul(i64::from(rhs))
.expect("TimeVal multiply out of bounds");
TimeVal::microseconds(usec)
@@ -624,18 +670,16 @@ fn div_mod_floor_64(this: i64, other: i64) -> (i64, i64) {
#[inline]
fn div_floor_64(this: i64, other: i64) -> i64 {
match div_rem_64(this, other) {
- (d, r) if (r > 0 && other < 0)
- || (r < 0 && other > 0) => d - 1,
- (d, _) => d,
+ (d, r) if (r > 0 && other < 0) || (r < 0 && other > 0) => d - 1,
+ (d, _) => d,
}
}
#[inline]
fn mod_floor_64(this: i64, other: i64) -> i64 {
match this % other {
- r if (r > 0 && other < 0)
- || (r < 0 && other > 0) => r + other,
- r => r,
+ r if (r > 0 && other < 0) || (r < 0 && other > 0) => r + other,
+ r => r,
}
}
@@ -652,10 +696,14 @@ mod test {
#[test]
pub fn test_timespec() {
assert!(TimeSpec::seconds(1) != TimeSpec::zero());
- assert_eq!(TimeSpec::seconds(1) + TimeSpec::seconds(2),
- TimeSpec::seconds(3));
- assert_eq!(TimeSpec::minutes(3) + TimeSpec::seconds(2),
- TimeSpec::seconds(182));
+ assert_eq!(
+ TimeSpec::seconds(1) + TimeSpec::seconds(2),
+ TimeSpec::seconds(3)
+ );
+ assert_eq!(
+ TimeSpec::minutes(3) + TimeSpec::seconds(2),
+ TimeSpec::seconds(182)
+ );
}
#[test]
@@ -690,17 +738,24 @@ mod test {
assert_eq!(TimeSpec::seconds(42).to_string(), "42 seconds");
assert_eq!(TimeSpec::milliseconds(42).to_string(), "0.042 seconds");
assert_eq!(TimeSpec::microseconds(42).to_string(), "0.000042 seconds");
- assert_eq!(TimeSpec::nanoseconds(42).to_string(), "0.000000042 seconds");
+ assert_eq!(
+ TimeSpec::nanoseconds(42).to_string(),
+ "0.000000042 seconds"
+ );
assert_eq!(TimeSpec::seconds(-86401).to_string(), "-86401 seconds");
}
#[test]
pub fn test_timeval() {
assert!(TimeVal::seconds(1) != TimeVal::zero());
- assert_eq!(TimeVal::seconds(1) + TimeVal::seconds(2),
- TimeVal::seconds(3));
- assert_eq!(TimeVal::minutes(3) + TimeVal::seconds(2),
- TimeVal::seconds(182));
+ assert_eq!(
+ TimeVal::seconds(1) + TimeVal::seconds(2),
+ TimeVal::seconds(3)
+ );
+ assert_eq!(
+ TimeVal::minutes(3) + TimeVal::seconds(2),
+ TimeVal::seconds(182)
+ );
}
#[test]