Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable creating a const TimeSpec #1465

Merged
merged 3 commits into from
Jul 24, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 13 additions & 5 deletions src/sys/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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 {
Expand Down