1- #! [ allow ( dead_code ) ]
1+ use hermit_abi :: { self , timespec } ;
22
3- use core:: hash:: { Hash , Hasher } ;
4-
5- use super :: hermit_abi:: { self , CLOCK_MONOTONIC , CLOCK_REALTIME , timespec} ;
63use crate :: cmp:: Ordering ;
7- use crate :: ops :: { Add , AddAssign , Sub , SubAssign } ;
4+ use crate :: hash :: { Hash , Hasher } ;
85use crate :: time:: Duration ;
96
107const NSEC_PER_SEC : i32 = 1_000_000_000 ;
118
129#[ derive( Copy , Clone , Debug ) ]
13- struct Timespec {
14- t : timespec ,
10+ pub struct Timespec {
11+ pub t : timespec ,
1512}
1613
1714impl Timespec {
18- const MAX : Timespec = Self :: new ( i64:: MAX , 1_000_000_000 - 1 ) ;
15+ pub const MAX : Timespec = Self :: new ( i64:: MAX , 1_000_000_000 - 1 ) ;
1916
20- const MIN : Timespec = Self :: new ( i64:: MIN , 0 ) ;
17+ pub const MIN : Timespec = Self :: new ( i64:: MIN , 0 ) ;
2118
22- const fn zero ( ) -> Timespec {
19+ pub const fn zero ( ) -> Timespec {
2320 Timespec { t : timespec { tv_sec : 0 , tv_nsec : 0 } }
2421 }
2522
26- const fn new ( tv_sec : i64 , tv_nsec : i32 ) -> Timespec {
23+ pub const fn new ( tv_sec : i64 , tv_nsec : i32 ) -> Timespec {
2724 assert ! ( tv_nsec >= 0 && tv_nsec < NSEC_PER_SEC ) ;
2825 // SAFETY: The assert above checks tv_nsec is within the valid range
2926 Timespec { t : timespec { tv_sec, tv_nsec } }
3027 }
3128
32- fn sub_timespec ( & self , other : & Timespec ) -> Result < Duration , Duration > {
29+ pub fn sub_timespec ( & self , other : & Timespec ) -> Result < Duration , Duration > {
3330 fn sub_ge_to_unsigned ( a : i64 , b : i64 ) -> u64 {
3431 debug_assert ! ( a >= b) ;
3532 a. wrapping_sub ( b) . cast_unsigned ( )
@@ -57,7 +54,7 @@ impl Timespec {
5754 }
5855 }
5956
60- fn checked_add_duration ( & self , other : & Duration ) -> Option < Timespec > {
57+ pub fn checked_add_duration ( & self , other : & Duration ) -> Option < Timespec > {
6158 let mut secs = self . t . tv_sec . checked_add_unsigned ( other. as_secs ( ) ) ?;
6259
6360 // Nano calculations can't overflow because nanos are <1B which fit
@@ -70,7 +67,7 @@ impl Timespec {
7067 Some ( Timespec { t : timespec { tv_sec : secs, tv_nsec : nsec as _ } } )
7168 }
7269
73- fn checked_sub_duration ( & self , other : & Duration ) -> Option < Timespec > {
70+ pub fn checked_sub_duration ( & self , other : & Duration ) -> Option < Timespec > {
7471 let mut secs = self . t . tv_sec . checked_sub_unsigned ( other. as_secs ( ) ) ?;
7572
7673 // Similar to above, nanos can't overflow.
@@ -111,132 +108,3 @@ impl Hash for Timespec {
111108 self . t . tv_nsec . hash ( state) ;
112109 }
113110}
114-
115- #[ derive( Copy , Clone , PartialEq , Eq , PartialOrd , Ord , Debug , Hash ) ]
116- pub struct Instant ( Timespec ) ;
117-
118- impl Instant {
119- pub fn now ( ) -> Instant {
120- let mut time: Timespec = Timespec :: zero ( ) ;
121- let _ = unsafe { hermit_abi:: clock_gettime ( CLOCK_MONOTONIC , & raw mut time. t ) } ;
122-
123- Instant ( time)
124- }
125-
126- #[ stable( feature = "time2" , since = "1.8.0" ) ]
127- pub fn elapsed ( & self ) -> Duration {
128- Instant :: now ( ) - * self
129- }
130-
131- pub fn duration_since ( & self , earlier : Instant ) -> Duration {
132- self . checked_duration_since ( earlier) . unwrap_or_default ( )
133- }
134-
135- pub fn checked_duration_since ( & self , earlier : Instant ) -> Option < Duration > {
136- self . checked_sub_instant ( & earlier)
137- }
138-
139- pub fn checked_sub_instant ( & self , other : & Instant ) -> Option < Duration > {
140- self . 0 . sub_timespec ( & other. 0 ) . ok ( )
141- }
142-
143- pub fn checked_add_duration ( & self , other : & Duration ) -> Option < Instant > {
144- Some ( Instant ( self . 0 . checked_add_duration ( other) ?) )
145- }
146-
147- pub fn checked_sub_duration ( & self , other : & Duration ) -> Option < Instant > {
148- Some ( Instant ( self . 0 . checked_sub_duration ( other) ?) )
149- }
150-
151- pub fn checked_add ( & self , duration : Duration ) -> Option < Instant > {
152- self . 0 . checked_add_duration ( & duration) . map ( Instant )
153- }
154-
155- pub fn checked_sub ( & self , duration : Duration ) -> Option < Instant > {
156- self . 0 . checked_sub_duration ( & duration) . map ( Instant )
157- }
158- }
159-
160- impl Add < Duration > for Instant {
161- type Output = Instant ;
162-
163- /// # Panics
164- ///
165- /// This function may panic if the resulting point in time cannot be represented by the
166- /// underlying data structure. See [`Instant::checked_add`] for a version without panic.
167- fn add ( self , other : Duration ) -> Instant {
168- self . checked_add ( other) . expect ( "overflow when adding duration to instant" )
169- }
170- }
171-
172- impl AddAssign < Duration > for Instant {
173- fn add_assign ( & mut self , other : Duration ) {
174- * self = * self + other;
175- }
176- }
177-
178- impl Sub < Duration > for Instant {
179- type Output = Instant ;
180-
181- fn sub ( self , other : Duration ) -> Instant {
182- self . checked_sub ( other) . expect ( "overflow when subtracting duration from instant" )
183- }
184- }
185-
186- impl SubAssign < Duration > for Instant {
187- fn sub_assign ( & mut self , other : Duration ) {
188- * self = * self - other;
189- }
190- }
191-
192- impl Sub < Instant > for Instant {
193- type Output = Duration ;
194-
195- /// Returns the amount of time elapsed from another instant to this one,
196- /// or zero duration if that instant is later than this one.
197- ///
198- /// # Panics
199- ///
200- /// Previous Rust versions panicked when `other` was later than `self`. Currently this
201- /// method saturates. Future versions may reintroduce the panic in some circumstances.
202- /// See [Monotonicity].
203- ///
204- /// [Monotonicity]: Instant#monotonicity
205- fn sub ( self , other : Instant ) -> Duration {
206- self . duration_since ( other)
207- }
208- }
209-
210- #[ derive( Copy , Clone , PartialEq , Eq , PartialOrd , Ord , Hash , Debug ) ]
211- pub struct SystemTime ( Timespec ) ;
212-
213- pub const UNIX_EPOCH : SystemTime = SystemTime ( Timespec :: zero ( ) ) ;
214-
215- impl SystemTime {
216- pub const MAX : SystemTime = SystemTime ( Timespec :: MAX ) ;
217-
218- pub const MIN : SystemTime = SystemTime ( Timespec :: MIN ) ;
219-
220- pub fn new ( tv_sec : i64 , tv_nsec : i32 ) -> SystemTime {
221- SystemTime ( Timespec :: new ( tv_sec, tv_nsec) )
222- }
223-
224- pub fn now ( ) -> SystemTime {
225- let mut time: Timespec = Timespec :: zero ( ) ;
226- let _ = unsafe { hermit_abi:: clock_gettime ( CLOCK_REALTIME , & raw mut time. t ) } ;
227-
228- SystemTime ( time)
229- }
230-
231- pub fn sub_time ( & self , other : & SystemTime ) -> Result < Duration , Duration > {
232- self . 0 . sub_timespec ( & other. 0 )
233- }
234-
235- pub fn checked_add_duration ( & self , other : & Duration ) -> Option < SystemTime > {
236- Some ( SystemTime ( self . 0 . checked_add_duration ( other) ?) )
237- }
238-
239- pub fn checked_sub_duration ( & self , other : & Duration ) -> Option < SystemTime > {
240- Some ( SystemTime ( self . 0 . checked_sub_duration ( other) ?) )
241- }
242- }
0 commit comments