Skip to content

Commit b3e4d59

Browse files
committed
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.
1 parent caebe66 commit b3e4d59

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ This project adheres to [Semantic Versioning](https://semver.org/).
66
## [Unreleased] - ReleaseDate
77
### Added
88

9+
- Added const constructors for `TimeSpec` and `TimeVal`
10+
(#[1760](https://github.com/nix-rust/nix/pull/1760))
911
- Added `aio_writev` and `aio_readv`.
1012
(#[1713](https://github.com/nix-rust/nix/pull/1713))
11-
1213
- impl `From<uid_t>` for `Uid` and `From<gid_t>` for `Gid`
1314
(#[1727](https://github.com/nix-rust/nix/pull/1727))
1415
- impl From<SockaddrIn> for std::net::SocketAddrV4 and

src/sys/time.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,15 @@ impl TimeValLike for TimeSpec {
330330
}
331331

332332
impl TimeSpec {
333+
/// Construct a new `TimeSpec` from its components
334+
#[cfg_attr(target_env = "musl", allow(deprecated))] // https://github.com/rust-lang/libc/issues/1848
335+
pub const fn new(seconds: time_t, nanoseconds: timespec_tv_nsec_t) -> Self {
336+
Self(timespec {
337+
tv_sec: seconds,
338+
tv_nsec: nanoseconds,
339+
})
340+
}
341+
333342
fn nanos_mod_sec(&self) -> timespec_tv_nsec_t {
334343
if self.tv_sec() < 0 && self.tv_nsec() > 0 {
335344
self.tv_nsec() - NANOS_PER_SEC as timespec_tv_nsec_t
@@ -564,6 +573,15 @@ impl TimeValLike for TimeVal {
564573
}
565574

566575
impl TimeVal {
576+
/// Construct a new `TimeVal` from its components
577+
#[cfg_attr(target_env = "musl", allow(deprecated))] // https://github.com/rust-lang/libc/issues/1848
578+
pub const fn new(seconds: time_t, microseconds: suseconds_t) -> Self {
579+
Self(timeval {
580+
tv_sec: seconds,
581+
tv_usec: microseconds,
582+
})
583+
}
584+
567585
fn micros_mod_sec(&self) -> suseconds_t {
568586
if self.tv_sec() < 0 && self.tv_usec() > 0 {
569587
self.tv_usec() - MICROS_PER_SEC as suseconds_t

0 commit comments

Comments
 (0)