Skip to content

Commit 6d0ad77

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 6d0ad77

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,14 @@ impl TimeValLike for TimeSpec {
330330
}
331331

332332
impl TimeSpec {
333+
/// Construct a new `TimeSpec` from its components
334+
pub const fn new(seconds: time_t, nanoseconds: timespec_tv_nsec_t) -> Self {
335+
Self(timespec {
336+
tv_sec: seconds,
337+
tv_nsec: nanoseconds,
338+
})
339+
}
340+
333341
fn nanos_mod_sec(&self) -> timespec_tv_nsec_t {
334342
if self.tv_sec() < 0 && self.tv_nsec() > 0 {
335343
self.tv_nsec() - NANOS_PER_SEC as timespec_tv_nsec_t
@@ -564,6 +572,14 @@ impl TimeValLike for TimeVal {
564572
}
565573

566574
impl TimeVal {
575+
/// Construct a new `TimeVal` from its components
576+
pub const fn new(seconds: time_t, microseconds: suseconds_t) -> Self {
577+
Self(timeval {
578+
tv_sec: seconds,
579+
tv_usec: microseconds,
580+
})
581+
}
582+
567583
fn micros_mod_sec(&self) -> suseconds_t {
568584
if self.tv_sec() < 0 && self.tv_usec() > 0 {
569585
self.tv_usec() - MICROS_PER_SEC as suseconds_t

0 commit comments

Comments
 (0)