use std;
use capi;
use std::cmp::Ordering;
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, RemAssign, Sub, SubAssign};
pub use libc::timeval;
pub const NANOS_PER_SEC: u32 = 1_000_000_000;
pub const NANOS_PER_MILLI: u32 = 1_000_000;
pub const NANOS_PER_MICRO: u32 = 1_000;
pub const MICROS_PER_SEC: u64 = 1_000_000;
pub const MICROS_PER_MILLI: u64 = 1_000;
pub const MILLIS_PER_SEC: u64 = 1_000;
pub const USEC_INVALID: MicroSeconds = MicroSeconds(capi::PA_USEC_INVALID);
pub const USEC_MAX: MicroSeconds = MicroSeconds(capi::PA_USEC_MAX);
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Ord, PartialOrd)]
pub struct MicroSeconds(pub u64);
impl MicroSeconds {
pub fn is_valid(&self) -> bool {
*self != USEC_INVALID
}
}
#[repr(C)]
#[derive(Copy, Clone)]
pub struct Timeval(pub timeval);
impl PartialEq for Timeval {
fn eq(&self, other: &Self) -> bool {
self.0.tv_sec == other.0.tv_sec && self.0.tv_usec == other.0.tv_usec
}
}
impl Eq for Timeval {}
impl Ord for Timeval {
fn cmp(&self, other: &Self) -> Ordering {
match unsafe { capi::pa_timeval_cmp(&self.0, &other.0) } {
0 => Ordering::Equal,
r if r < 0 => Ordering::Less,
_ => Ordering::Greater,
}
}
}
impl PartialOrd for Timeval {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl std::fmt::Debug for Timeval {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "timeval {{ tv_sec: {}, tv_usec: {} }}", self.0.tv_sec, self.0.tv_usec)
}
}
impl Timeval {
pub fn get_time_of_day(&mut self) -> &mut Self {
unsafe { capi::pa_gettimeofday(&mut self.0) };
self
}
pub fn diff(a: &Self, b: &Self) -> MicroSeconds {
MicroSeconds(unsafe { capi::pa_timeval_diff(&a.0, &b.0) })
}
pub fn age(&self) -> MicroSeconds {
MicroSeconds(unsafe { capi::pa_timeval_age(&self.0) })
}
pub fn add(&mut self, t: MicroSeconds) {
unsafe { capi::pa_timeval_add(&mut self.0, t.0); }
}
pub fn sub(&mut self, t: MicroSeconds) {
unsafe { capi::pa_timeval_sub(&mut self.0, t.0); }
}
pub fn set(&mut self, t: MicroSeconds) {
unsafe { capi::pa_timeval_store(&mut self.0, t.0); }
}
}
impl From<Timeval> for MicroSeconds {
fn from(t: Timeval) -> Self {
MicroSeconds(unsafe { capi::pa_timeval_load(&t.0) })
}
}
impl From<MicroSeconds> for Timeval {
fn from(t: MicroSeconds) -> Self {
let mut tmp = Timeval(timeval { tv_sec: 0, tv_usec: 0 });
tmp.set(t);
tmp
}
}
impl MicroSeconds {
pub fn checked_add(self, other: Self) -> Option<Self> {
self.0.checked_add(other.0).and_then(|i| Some(MicroSeconds(i)))
}
pub fn checked_sub(self, other: Self) -> Option<Self> {
self.0.checked_sub(other.0).and_then(|i| Some(MicroSeconds(i)))
}
pub fn checked_mul(self, rhs: u32) -> Option<Self> {
self.0.checked_mul(rhs as u64).and_then(|i| Some(MicroSeconds(i)))
}
pub fn checked_div(self, rhs: u32) -> Option<Self> {
self.0.checked_div(rhs as u64).and_then(|i| Some(MicroSeconds(i)))
}
pub fn checked_rem(self, rhs: u32) -> Option<Self> {
self.0.checked_rem(rhs as u64).and_then(|i| Some(MicroSeconds(i)))
}
}
impl Add for MicroSeconds {
type Output = Self;
fn add(self, other: Self) -> Self {
MicroSeconds(self.0 + other.0)
}
}
impl AddAssign for MicroSeconds {
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs;
}
}
impl Sub for MicroSeconds {
type Output = Self;
fn sub(self, other: Self) -> Self {
MicroSeconds(self.0 - other.0)
}
}
impl SubAssign for MicroSeconds {
fn sub_assign(&mut self, rhs: Self) {
*self = *self - rhs;
}
}
impl Mul<u32> for MicroSeconds {
type Output = Self;
fn mul(self, rhs: u32) -> Self {
MicroSeconds(self.0 * rhs as u64)
}
}
impl MulAssign<u32> for MicroSeconds {
fn mul_assign(&mut self, rhs: u32) {
*self = *self * rhs;
}
}
impl Div<u32> for MicroSeconds {
type Output = Self;
fn div(self, rhs: u32) -> Self {
MicroSeconds(self.0 / rhs as u64)
}
}
impl DivAssign<u32> for MicroSeconds {
fn div_assign(&mut self, rhs: u32) {
*self = *self / rhs;
}
}
impl Rem<u32> for MicroSeconds {
type Output = Self;
fn rem(self, rhs: u32) -> Self {
MicroSeconds(self.0 % rhs as u64)
}
}
impl RemAssign<u32> for MicroSeconds {
fn rem_assign(&mut self, rhs: u32) {
*self = *self % rhs;
}
}
impl std::fmt::Display for MicroSeconds {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{} µs", self.0)
}
}