use crate::time::{Duration, Time};
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct TimeInt(pub(crate) i64);
impl nohash_hasher::IsEnabled for TimeInt {}
impl re_types_core::SizeBytes for TimeInt {
#[inline]
fn heap_size_bytes(&self) -> u64 {
0
}
}
impl TimeInt {
pub const BEGINNING: Self = Self(i64::MIN / 2);
pub const MIN: Self = Self(i64::MIN);
pub const MAX: Self = Self(i64::MAX);
#[inline]
pub fn from_nanos(nanos: i64) -> Self {
Self(nanos)
}
#[inline]
pub fn from_milliseconds(millis: i64) -> Self {
Self::from_nanos(millis * 1_000_000)
}
#[inline]
pub fn from_seconds(seconds: i64) -> Self {
Self::from_nanos(seconds.saturating_mul(1_000_000_000))
}
#[inline]
pub fn from_sequence(sequence: i64) -> Self {
Self(sequence)
}
#[inline]
pub fn as_i64(&self) -> i64 {
self.0
}
#[inline]
pub fn as_f32(&self) -> f32 {
self.0 as _
}
#[inline]
pub fn as_f64(&self) -> f64 {
self.0 as _
}
#[inline]
pub fn abs(&self) -> Self {
Self(self.0.saturating_abs())
}
}
impl From<i64> for TimeInt {
#[inline]
fn from(seq: i64) -> Self {
Self(seq)
}
}
impl From<Duration> for TimeInt {
#[inline]
fn from(duration: Duration) -> Self {
Self(duration.as_nanos())
}
}
impl From<Time> for TimeInt {
#[inline]
fn from(time: Time) -> Self {
Self(time.nanos_since_epoch())
}
}
impl From<TimeInt> for Time {
#[inline]
fn from(int: TimeInt) -> Self {
Self::from_ns_since_epoch(int.as_i64())
}
}
impl From<TimeInt> for Duration {
#[inline]
fn from(int: TimeInt) -> Self {
Self::from_nanos(int.as_i64())
}
}
impl From<TimeInt> for re_types_core::datatypes::TimeInt {
#[inline]
fn from(int: TimeInt) -> Self {
Self(int.as_i64())
}
}
impl std::ops::Neg for TimeInt {
type Output = Self;
#[inline]
fn neg(self) -> Self::Output {
Self(self.0.saturating_neg())
}
}
impl std::ops::Add for TimeInt {
type Output = Self;
#[inline]
fn add(self, rhs: Self) -> Self::Output {
Self(self.0.saturating_add(rhs.0))
}
}
impl std::ops::Sub for TimeInt {
type Output = Self;
#[inline]
fn sub(self, rhs: Self) -> Self::Output {
Self(self.0.saturating_sub(rhs.0))
}
}
impl std::ops::AddAssign for TimeInt {
#[inline]
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs;
}
}
impl std::ops::SubAssign for TimeInt {
#[inline]
fn sub_assign(&mut self, rhs: Self) {
*self = *self - rhs;
}
}
impl std::iter::Sum for TimeInt {
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
let mut sum = TimeInt(0);
for item in iter {
sum += item;
}
sum
}
}