pub use fixed_wide::fixed::{Fixed,Fix}; pub use ratio_ops::ratio::{Ratio,Divide}; //integer units #[derive(Clone,Copy,Hash,Eq,PartialEq,PartialOrd,Debug)] pub struct Time(i64); impl Time{ pub const MIN:Self=Self(i64::MIN); pub const MAX:Self=Self(i64::MAX); pub const ZERO:Self=Self(0); pub const ONE_SECOND:Self=Self(1_000_000_000); pub const ONE_MILLISECOND:Self=Self(1_000_000); pub const ONE_MICROSECOND:Self=Self(1_000); pub const ONE_NANOSECOND:Self=Self(1); #[inline] pub const fn raw(num:i64)->Self{ Self(num) } #[inline] pub const fn get(self)->i64{ self.0 } #[inline] pub const fn from_secs(num:i64)->Self{ Self(Self::ONE_SECOND.0*num) } #[inline] pub const fn from_millis(num:i64)->Self{ Self(Self::ONE_MILLISECOND.0*num) } #[inline] pub const fn from_micros(num:i64)->Self{ Self(Self::ONE_MICROSECOND.0*num) } #[inline] pub const fn from_nanos(num:i64)->Self{ Self(Self::ONE_NANOSECOND.0*num) } //should I have checked subtraction? force all time variables to be positive? #[inline] pub const fn nanos(self)->i64{ self.0 } #[inline] pub const fn to_ratio(self)->Ratio{ Ratio::new(Planar64::raw(self.0),Planar64::raw(1_000_000_000)) } } impl From for Time{ #[inline] fn from(value:Planar64)->Self{ Time((value*Planar64::raw(1_000_000_000)).fix_1().to_raw()) } } impl From> for Time where Num:core::ops::Mul, N1:Divide, T1:Fix, { #[inline] fn from(value:Ratio)->Self{ Time((value*Planar64::raw(1_000_000_000)).divide().fix().to_raw()) } } impl std::fmt::Display for Time{ #[inline] fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{ write!(f,"{}s+{:09}ns",self.0/Self::ONE_SECOND.0,self.0%Self::ONE_SECOND.0) } } impl std::default::Default for Time{ fn default()->Self{ Self(0) } } impl std::ops::Neg for Time{ type Output=Time; #[inline] fn neg(self)->Self::Output { Time(-self.0) } } macro_rules! impl_time_additive_operator { ($trait:ty, $method:ident) => { impl $trait for Time{ type Output=Time; #[inline] fn $method(self,rhs:Self)->Self::Output { Time(self.0.$method(rhs.0)) } } }; } impl_time_additive_operator!(core::ops::Add,add); impl_time_additive_operator!(core::ops::Sub,sub); impl_time_additive_operator!(core::ops::Rem,rem); macro_rules! impl_time_additive_assign_operator { ($trait:ty, $method:ident) => { impl $trait for Time{ #[inline] fn $method(&mut self,rhs:Self){ self.0.$method(rhs.0) } } }; } impl_time_additive_assign_operator!(core::ops::AddAssign,add_assign); impl_time_additive_assign_operator!(core::ops::SubAssign,sub_assign); impl_time_additive_assign_operator!(core::ops::RemAssign,rem_assign); impl std::ops::Mul for Time{ type Output=Ratio,fixed_wide::fixed::Fixed<2,64>>; #[inline] fn mul(self,rhs:Self)->Self::Output{ Ratio::new(Fixed::raw(self.0)*Fixed::raw(rhs.0),Fixed::raw_digit(1_000_000_000i64.pow(2))) } } impl std::ops::Div for Time{ type Output=Time; #[inline] fn div(self,rhs:i64)->Self::Output{ Time(self.0/rhs) } } impl std::ops::Mul for Time{ type Output=Time; #[inline] fn mul(self,rhs:i64)->Self::Output{ Time(self.0*rhs) } } impl core::ops::Mul