use std;
use capi;
use libc;
use std::cmp::Ordering;
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, RemAssign, Sub, SubAssign};
use std::time::Duration;
use super::{UnixTs, MonotonicTs, MicroSeconds, USEC_INVALID};
const PA_TIMEVAL_RTCLOCK: i64 = 1 << 30;
#[repr(C)]
#[derive(Copy, Clone)]
pub struct Timeval(pub libc::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 new(sec: libc::time_t, usec: libc::suseconds_t) -> Self {
Timeval(libc::timeval { tv_sec: sec, tv_usec: usec })
}
pub fn new_zero() -> Self {
Timeval::new(0, 0)
}
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(crate) fn set_rt(&mut self, v: MicroSeconds, rtclock: bool) -> &mut Self {
assert_ne!(v, USEC_INVALID);
*self = v.into();
match rtclock {
true => { self.0.tv_usec |= PA_TIMEVAL_RTCLOCK; },
false => { self.wallclock_from_rtclock(); },
}
self
}
pub(crate) fn wallclock_from_rtclock(&mut self) -> &mut Self {
let wc_now = (UnixTs::now()).0;
let rt_now = Timeval::from((MonotonicTs::now()).0);
let _ = match rt_now.cmp(self) {
Ordering::Less => { wc_now.add(Timeval::diff(self, &rt_now)) },
_ => { wc_now.sub(Timeval::diff(&rt_now, self)) },
};
*self = wc_now;
self
}
pub fn checked_add(self, other: Self) -> Option<Self> {
let self_us = MicroSeconds::from(self);
let other_us = MicroSeconds::from(other);
self_us.checked_add(other_us).and_then(|i| Some(i.into()))
}
pub fn checked_add_us(self, rhs: MicroSeconds) -> Option<Self> {
let self_us = MicroSeconds::from(self);
self_us.checked_add(rhs).and_then(|i| Some(i.into()))
}
pub fn checked_add_duration(self, rhs: Duration) -> Option<Self> {
let self_us = MicroSeconds::from(self);
let rhs_us = MicroSeconds::from(rhs);
self_us.checked_add(rhs_us).and_then(|i| Some(i.into()))
}
pub fn checked_sub(self, other: Self) -> Option<Self> {
let self_us = MicroSeconds::from(self);
let other_us = MicroSeconds::from(other);
self_us.checked_sub(other_us).and_then(|i| Some(i.into()))
}
pub fn checked_sub_us(self, rhs: MicroSeconds) -> Option<Self> {
let self_us = MicroSeconds::from(self);
self_us.checked_sub(rhs).and_then(|i| Some(i.into()))
}
pub fn checked_sub_duration(self, rhs: Duration) -> Option<Self> {
let self_us = MicroSeconds::from(self);
let rhs_us = MicroSeconds::from(rhs);
self_us.checked_sub(rhs_us).and_then(|i| Some(i.into()))
}
pub fn checked_mul(self, rhs: u32) -> Option<Self> {
let self_us = MicroSeconds::from(self);
self_us.checked_mul(rhs).and_then(|i| Some(i.into()))
}
pub fn checked_div(self, rhs: u32) -> Option<Self> {
let self_us = MicroSeconds::from(self);
self_us.checked_div(rhs).and_then(|i| Some(i.into()))
}
pub fn checked_rem(self, rhs: u32) -> Option<Self> {
let self_us = MicroSeconds::from(self);
self_us.checked_rem(rhs).and_then(|i| Some(i.into()))
}
}
impl Add for Timeval {
type Output = Self;
fn add(self, other: Self) -> Self {
self.checked_add(other).unwrap()
}
}
impl AddAssign for Timeval {
fn add_assign(&mut self, rhs: Self) {
*self = self.checked_add(rhs).unwrap();
}
}
impl Add<MicroSeconds> for Timeval {
type Output = Self;
fn add(self, rhs: MicroSeconds) -> Self {
self.checked_add_us(rhs).unwrap()
}
}
impl AddAssign<MicroSeconds> for Timeval {
fn add_assign(&mut self, rhs: MicroSeconds) {
*self = self.checked_add_us(rhs).unwrap();
}
}
impl Add<Duration> for Timeval {
type Output = Self;
fn add(self, rhs: Duration) -> Self {
self.checked_add_duration(rhs).unwrap()
}
}
impl AddAssign<Duration> for Timeval {
fn add_assign(&mut self, rhs: Duration) {
*self = self.checked_add_duration(rhs).unwrap();
}
}
impl Sub for Timeval {
type Output = Self;
fn sub(self, other: Self) -> Self {
self.checked_sub(other).unwrap()
}
}
impl SubAssign for Timeval {
fn sub_assign(&mut self, rhs: Self) {
*self = self.checked_sub(rhs).unwrap();
}
}
impl Sub<MicroSeconds> for Timeval {
type Output = Self;
fn sub(self, rhs: MicroSeconds) -> Self {
self.checked_sub_us(rhs).unwrap()
}
}
impl SubAssign<MicroSeconds> for Timeval {
fn sub_assign(&mut self, rhs: MicroSeconds) {
*self = self.checked_sub_us(rhs).unwrap();
}
}
impl Sub<Duration> for Timeval {
type Output = Self;
fn sub(self, rhs: Duration) -> Self {
self.checked_sub_duration(rhs).unwrap()
}
}
impl SubAssign<Duration> for Timeval {
fn sub_assign(&mut self, rhs: Duration) {
*self = self.checked_sub_duration(rhs).unwrap();
}
}
impl Mul<u32> for Timeval {
type Output = Self;
fn mul(self, rhs: u32) -> Self {
self.checked_mul(rhs).unwrap()
}
}
impl MulAssign<u32> for Timeval {
fn mul_assign(&mut self, rhs: u32) {
*self = self.checked_mul(rhs).unwrap();
}
}
impl Div<u32> for Timeval {
type Output = Self;
fn div(self, rhs: u32) -> Self {
self.checked_div(rhs).unwrap()
}
}
impl DivAssign<u32> for Timeval {
fn div_assign(&mut self, rhs: u32) {
*self = self.checked_div(rhs).unwrap();
}
}
impl Rem<u32> for Timeval {
type Output = Self;
fn rem(self, rhs: u32) -> Self {
self.checked_rem(rhs).unwrap()
}
}
impl RemAssign<u32> for Timeval {
fn rem_assign(&mut self, rhs: u32) {
*self = self.checked_rem(rhs).unwrap();
}
}