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
@@ -77,11 +77,7 @@ impl From<timespec> for TimeSpec {
impl From<Duration> 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 {
