Skip to content

Commit

Permalink
[zx][rs] Reorder time module.
Browse files Browse the repository at this point in the history
Group Time & Duration impls together with the type defs.

Change-Id: I2300417db4fa58b7fd574ce6f077bf8fa711e931
Reviewed-on: https://fuchsia-review.googlesource.com/c/fuchsia/+/1103860
Reviewed-by: Adam Barth <abarth@google.com>
Commit-Queue: Auto-Submit <auto-submit@fuchsia-infra.iam.gserviceaccount.com>
Fuchsia-Auto-Submit: Adam Perry <adamperry@google.com>
  • Loading branch information
anp authored and CQ Bot committed Aug 20, 2024
1 parent 83a47ea commit bb1d71c
Showing 1 changed file with 75 additions and 75 deletions.
150 changes: 75 additions & 75 deletions src/lib/zircon/rust/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,58 @@ use crate::{ok, AsHandleRef, Handle, HandleBased, HandleRef, Status};
use fuchsia_zircon_sys as sys;
use std::{ops, time as stdtime};

#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(transparent)]
pub struct Duration(sys::zx_duration_t);

#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(transparent)]
pub struct Time(sys::zx_time_t);

impl From<stdtime::Duration> for Duration {
fn from(dur: stdtime::Duration) -> Self {
Duration::from_seconds(dur.as_secs() as i64)
+ Duration::from_nanos(dur.subsec_nanos() as i64)
impl Time {
pub const INFINITE: Time = Time(sys::ZX_TIME_INFINITE);
pub const INFINITE_PAST: Time = Time(sys::ZX_TIME_INFINITE_PAST);
pub const ZERO: Time = Time(0);

/// Get the current monotonic time.
///
/// Wraps the
/// [zx_clock_get_monotonic](https://fuchsia.dev/fuchsia-src/reference/syscalls/clock_get_monotonic.md)
/// syscall.
pub fn get_monotonic() -> Time {
unsafe { Time(sys::zx_clock_get_monotonic()) }
}
}

impl ops::Add<Duration> for Time {
type Output = Time;
fn add(self, dur: Duration) -> Time {
Time::from_nanos(dur.into_nanos().saturating_add(self.into_nanos()))
/// Compute a deadline for the time in the future that is the given `Duration` away.
///
/// Wraps the
/// [zx_deadline_after](https://fuchsia.dev/fuchsia-src/reference/syscalls/deadline_after.md)
/// syscall.
pub fn after(duration: Duration) -> Time {
unsafe { Time(sys::zx_deadline_after(duration.0)) }
}
}

impl ops::Add<Time> for Duration {
type Output = Time;
fn add(self, time: Time) -> Time {
Time::from_nanos(self.into_nanos().saturating_add(time.into_nanos()))
/// Sleep until the given time.
///
/// Wraps the
/// [zx_nanosleep](https://fuchsia.dev/fuchsia-src/reference/syscalls/nanosleep.md)
/// syscall.
pub fn sleep(self) {
unsafe {
sys::zx_nanosleep(self.0);
}
}
}

impl ops::Add for Duration {
type Output = Duration;
fn add(self, dur: Duration) -> Duration {
Duration::from_nanos(self.into_nanos().saturating_add(dur.into_nanos()))
/// Returns the number of nanoseconds since the epoch contained by this `Time`.
pub const fn into_nanos(self) -> i64 {
self.0
}

pub const fn from_nanos(nanos: i64) -> Self {
Time(nanos)
}
}

impl ops::Sub for Duration {
type Output = Duration;
fn sub(self, dur: Duration) -> Duration {
Duration::from_nanos(self.into_nanos().saturating_sub(dur.into_nanos()))
impl ops::Add<Duration> for Time {
type Output = Time;
fn add(self, dur: Duration) -> Time {
Time::from_nanos(dur.into_nanos().saturating_add(self.into_nanos()))
}
}

Expand All @@ -65,25 +77,57 @@ impl ops::Sub<Time> for Time {
}
}

impl ops::AddAssign for Duration {
impl ops::AddAssign<Duration> for Time {
fn add_assign(&mut self, dur: Duration) {
self.0 = self.0.saturating_add(dur.into_nanos());
}
}

impl ops::SubAssign for Duration {
impl ops::SubAssign<Duration> for Time {
fn sub_assign(&mut self, dur: Duration) {
self.0 = self.0.saturating_sub(dur.into_nanos());
}
}

impl ops::AddAssign<Duration> for Time {
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(transparent)]
pub struct Duration(sys::zx_duration_t);

impl From<stdtime::Duration> for Duration {
fn from(dur: stdtime::Duration) -> Self {
Duration::from_seconds(dur.as_secs() as i64)
+ Duration::from_nanos(dur.subsec_nanos() as i64)
}
}

impl ops::Add<Time> for Duration {
type Output = Time;
fn add(self, time: Time) -> Time {
Time::from_nanos(self.into_nanos().saturating_add(time.into_nanos()))
}
}

impl ops::Add for Duration {
type Output = Duration;
fn add(self, dur: Duration) -> Duration {
Duration::from_nanos(self.into_nanos().saturating_add(dur.into_nanos()))
}
}

impl ops::Sub for Duration {
type Output = Duration;
fn sub(self, dur: Duration) -> Duration {
Duration::from_nanos(self.into_nanos().saturating_sub(dur.into_nanos()))
}
}

impl ops::AddAssign for Duration {
fn add_assign(&mut self, dur: Duration) {
self.0 = self.0.saturating_add(dur.into_nanos());
}
}

impl ops::SubAssign<Duration> for Time {
impl ops::SubAssign for Duration {
fn sub_assign(&mut self, dur: Duration) {
self.0 = self.0.saturating_sub(dur.into_nanos());
}
Expand Down Expand Up @@ -271,50 +315,6 @@ impl DurationNum for i64 {
}
}

impl Time {
pub const INFINITE: Time = Time(sys::ZX_TIME_INFINITE);
pub const INFINITE_PAST: Time = Time(sys::ZX_TIME_INFINITE_PAST);
pub const ZERO: Time = Time(0);

/// Get the current monotonic time.
///
/// Wraps the
/// [zx_clock_get_monotonic](https://fuchsia.dev/fuchsia-src/reference/syscalls/clock_get_monotonic.md)
/// syscall.
pub fn get_monotonic() -> Time {
unsafe { Time(sys::zx_clock_get_monotonic()) }
}

/// Compute a deadline for the time in the future that is the given `Duration` away.
///
/// Wraps the
/// [zx_deadline_after](https://fuchsia.dev/fuchsia-src/reference/syscalls/deadline_after.md)
/// syscall.
pub fn after(duration: Duration) -> Time {
unsafe { Time(sys::zx_deadline_after(duration.0)) }
}

/// Sleep until the given time.
///
/// Wraps the
/// [zx_nanosleep](https://fuchsia.dev/fuchsia-src/reference/syscalls/nanosleep.md)
/// syscall.
pub fn sleep(self) {
unsafe {
sys::zx_nanosleep(self.0);
}
}

/// Returns the number of nanoseconds since the epoch contained by this `Time`.
pub const fn into_nanos(self) -> i64 {
self.0
}

pub const fn from_nanos(nanos: i64) -> Self {
Time(nanos)
}
}

/// Read the number of high-precision timer ticks since boot. These ticks may be processor cycles,
/// high speed timer, profiling timer, etc. They are not guaranteed to continue advancing when the
/// system is asleep.
Expand Down

0 comments on commit bb1d71c

Please sign in to comment.