Skip to content

Commit

Permalink
Add const constructors for TimeSpec and TimeVal
Browse files Browse the repository at this point in the history
These are basically the same as From<libc::timespec> and
From<libc::timeval>, but they're const and require less typing.
  • Loading branch information
asomers committed Jul 11, 2022
1 parent caebe66 commit 6d0ad77
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -6,9 +6,10 @@ This project adheres to [Semantic Versioning](https://semver.org/).
## [Unreleased] - ReleaseDate
### Added

- Added const constructors for `TimeSpec` and `TimeVal`
(#[1760](https://github.com/nix-rust/nix/pull/1760))
- Added `aio_writev` and `aio_readv`.
(#[1713](https://github.com/nix-rust/nix/pull/1713))

- impl `From<uid_t>` for `Uid` and `From<gid_t>` for `Gid`
(#[1727](https://github.com/nix-rust/nix/pull/1727))
- impl From<SockaddrIn> for std::net::SocketAddrV4 and
Expand Down
16 changes: 16 additions & 0 deletions src/sys/time.rs
Expand Up @@ -330,6 +330,14 @@ impl TimeValLike for TimeSpec {
}

impl TimeSpec {
/// Construct a new `TimeSpec` from its components
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
Expand Down Expand Up @@ -564,6 +572,14 @@ impl TimeValLike for TimeVal {
}

impl TimeVal {
/// Construct a new `TimeVal` from its components
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
Expand Down

0 comments on commit 6d0ad77

Please sign in to comment.