From 6542a6cb566557b0e47e962815525bbecb9ba2ae Mon Sep 17 00:00:00 2001 From: Daniel Dulaney Date: Tue, 13 Jul 2021 11:31:11 -0400 Subject: [PATCH 1/2] Enable creating a const TimeSpec Previously, there was no way to create a TimeSpec in a const context because all creation was through traits. This adds two utility functions to create a const TimeSpec from a libc::timespec or a std::time::Duration --- src/sys/time.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/sys/time.rs b/src/sys/time.rs index 7546d1b367..c786500a3c 100644 --- a/src/sys/time.rs +++ b/src/sys/time.rs @@ -77,11 +77,7 @@ impl From for TimeSpec { impl From for TimeSpec { fn from(duration: Duration) -> Self { - #[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 - }) + Self::from_duration(duration) } } @@ -198,6 +194,18 @@ impl TimeSpec { pub fn tv_nsec(&self) -> timespec_tv_nsec_t { self.0.tv_nsec } + + pub const fn from_duration(duration: Duration) -> Self { + #[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 + }) + } + + pub const fn from_timespec(timespec: timespec) -> Self { + Self(timespec) + } } impl ops::Neg for TimeSpec { From 7de40a921a493643727709596d14c2f2187166b8 Mon Sep 17 00:00:00 2001 From: Daniel Dulaney Date: Wed, 14 Jul 2021 01:29:51 -0400 Subject: [PATCH 2/2] Update changelog with TimeSpec additions --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 21cb4b85a2..9ddef3680e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ This project adheres to [Semantic Versioning](https://semver.org/). ## [Unreleased] - ReleaseDate ### Added +- Added `TimeSpec::from_duration` and `TimeSpec::from_timespec` + (#[1465](https://github.com/nix-rust/nix/pull/1465)) + ### Changed ### Fixed ### Removed