summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlan Somers <asomers@gmail.com>2022-07-10 18:09:51 -0600
committerAlan Somers <asomers@gmail.com>2022-07-10 18:43:00 -0600
commitb3e4d59bd7ad8a816b2ebe5f4564d8b62bbba4f5 (patch)
tree4ece7f3100f8e259d94d7052957f40abba92b22a /src
parentcaebe66185451e465e5e6454219c83ad86bfc553 (diff)
downloadnix-b3e4d59bd7ad8a816b2ebe5f4564d8b62bbba4f5.zip
Add const constructors for TimeSpec and TimeVal
These are basically the same as From<libc::timespec> and From<libc::timeval>, but they're const and require less typing.
Diffstat (limited to 'src')
-rw-r--r--src/sys/time.rs18
1 files changed, 18 insertions, 0 deletions
diff --git a/src/sys/time.rs b/src/sys/time.rs
index c559cc47..b7ab3986 100644
--- a/src/sys/time.rs
+++ b/src/sys/time.rs
@@ -330,6 +330,15 @@ impl TimeValLike for TimeSpec {
}
impl TimeSpec {
+ /// Construct a new `TimeSpec` from its components
+ #[cfg_attr(target_env = "musl", allow(deprecated))] // https://github.com/rust-lang/libc/issues/1848
+ pub const fn new(seconds: time_t, nanoseconds: timespec_tv_nsec_t) -> Self {
+ Self(timespec {
+ tv_sec: seconds,
+ tv_nsec: nanoseconds,
+ })
+ }
+
fn nanos_mod_sec(&self) -> timespec_tv_nsec_t {
if self.tv_sec() < 0 && self.tv_nsec() > 0 {
self.tv_nsec() - NANOS_PER_SEC as timespec_tv_nsec_t
@@ -564,6 +573,15 @@ impl TimeValLike for TimeVal {
}
impl TimeVal {
+ /// Construct a new `TimeVal` from its components
+ #[cfg_attr(target_env = "musl", allow(deprecated))] // https://github.com/rust-lang/libc/issues/1848
+ pub const fn new(seconds: time_t, microseconds: suseconds_t) -> Self {
+ Self(timeval {
+ tv_sec: seconds,
+ tv_usec: microseconds,
+ })
+ }
+
fn micros_mod_sec(&self) -> suseconds_t {
if self.tv_sec() < 0 && self.tv_usec() > 0 {
self.tv_usec() - MICROS_PER_SEC as suseconds_t