← std

std/time.kur

//! Time: `Instant`, a reading of a monotonic clock; `Duration`, a span of
//! time; and `sleep`. `Duration`'s arithmetic, and an `Instant` moved by
//! one, saturate: at the largest `Duration` or `Instant` going up, at zero
//! going down. Both compare with `Ord`, so a deadline is an `Instant`:
//! `Instant::now() >= start.add(timeout)`, or `start.elapsed() >= timeout`.
use package as std;

use core::cmp::{Ord, Order};
use core::hash::Eq;
use libc::time::{kur_libc_time_monotonic, kur_libc_time_sleep};

/// A span of time, in nanoseconds: up to some 584 years.
exposed struct Duration {
    nanoseconds: u64,
}

impl Duration {
    /// A duration of `nanoseconds` nanoseconds.
    fn from_nanos(nanoseconds: u64) -> Duration {
        Duration.{ nanoseconds }
    }

    /// A duration of `milliseconds` milliseconds.
    fn from_millis(milliseconds: u64) -> Duration {
        Duration.{ nanoseconds: saturating_mul(milliseconds, 1000000u64) }
    }

    /// A duration of `seconds` seconds.
    fn from_secs(seconds: u64) -> Duration {
        Duration.{ nanoseconds: saturating_mul(seconds, 1000000000u64) }
    }

    /// The whole nanoseconds.
    fn as_nanos(self) -> u64 {
        self.nanoseconds
    }

    /// The whole milliseconds.
    fn as_millis(self) -> u64 {
        self.nanoseconds / 1000000u64
    }

    /// The whole seconds.
    fn as_secs(self) -> u64 {
        self.nanoseconds / 1000000000u64
    }

    /// Whether the duration is no time at all.
    fn is_zero(self) -> bool {
        self.nanoseconds == 0u64
    }

    /// The two durations together.
    fn add(self, other: Duration) -> Duration {
        Duration.{ nanoseconds: saturating_add(self.nanoseconds, other.nanoseconds) }
    }

    /// What is left of this duration after `other`; zero if `other` is
    /// longer.
    fn sub(self, other: Duration) -> Duration {
        Duration.{ nanoseconds: saturating_sub(self.nanoseconds, other.nanoseconds) }
    }

    /// The duration `times` times over.
    fn mul(self, times: u64) -> Duration {
        Duration.{ nanoseconds: saturating_mul(self.nanoseconds, times) }
    }
}

/// `a + b`, or the largest `u64` when the sum is past it. Used by
/// `Duration` and `Instant`; not meant for direct use.
fn saturating_add(a: u64, b: u64) -> u64 {
    if a > 18446744073709551615u64 - b { 18446744073709551615u64 } else { a + b }
}

/// `a - b`, or 0 when `b` is larger. Used by `Duration` and `Instant`; not
/// meant for direct use.
fn saturating_sub(a: u64, b: u64) -> u64 {
    if b > a { 0u64 } else { a - b }
}

/// `a * b`, or the largest `u64` when the product is past it. Used by
/// `Duration`; not meant for direct use.
fn saturating_mul(a: u64, b: u64) -> u64 {
    if a != 0u64 && b > 18446744073709551615u64 / a { 18446744073709551615u64 } else { a * b }
}

/// Durations are equal when they are as long.
instance Eq[Duration, Duration] {
    fn eq(a: Duration, b: Duration) -> (Duration, Duration, bool) {
        (a, b, a.nanoseconds == b.nanoseconds)
    }
}

/// The shorter duration orders first.
instance Ord[Duration, Duration] {
    fn cmp(a: Duration, b: Duration) -> (Duration, Duration, Order) {
        let (x, y, order) = Ord[u64, u64]::cmp(a.nanoseconds, b.nanoseconds);
        (a, b, order)
    }
}

/// Suspends the calling thread for at least `duration`, sleeping on for what
/// is left when a signal interrupts it.
fn sleep(duration: Duration) {
    unsafe kur_libc_time_sleep(duration.nanoseconds);
}

/// A reading of `CLOCK_MONOTONIC`: it never goes back, and its start is
/// arbitrary, so only the difference between two readings means anything.
struct Instant {
    nanoseconds: u64,
}

impl Instant {
    /// The current reading of the monotonic clock, from the C runtime, which
    /// names the host's `CLOCK_MONOTONIC`.
    fn now() -> Instant {
        Instant.{ nanoseconds: unsafe kur_libc_time_monotonic() }
    }

    /// The time from `earlier` to this reading; zero if `earlier` is later.
    fn since(self, earlier: Instant) -> Duration {
        Duration.{ nanoseconds: saturating_sub(self.nanoseconds, earlier.nanoseconds) }
    }

    /// The time from this reading to now: `Instant::now().since(self)`.
    fn elapsed(self) -> Duration {
        Instant::now().since(self)
    }

    /// The reading `duration` after this one.
    fn add(self, duration: Duration) -> Instant {
        Instant.{ nanoseconds: saturating_add(self.nanoseconds, duration.nanoseconds) }
    }
}

/// Readings are equal when they are of the same moment.
instance Eq[Instant, Instant] {
    fn eq(a: Instant, b: Instant) -> (Instant, Instant, bool) {
        (a, b, a.nanoseconds == b.nanoseconds)
    }
}

/// The earlier reading orders first.
instance Ord[Instant, Instant] {
    fn cmp(a: Instant, b: Instant) -> (Instant, Instant, Order) {
        let (x, y, order) = Ord[u64, u64]::cmp(a.nanoseconds, b.nanoseconds);
        (a, b, order)
    }
}