15
15
use crate :: cmp;
16
16
use crate :: error:: Error ;
17
17
use crate :: fmt;
18
- use crate :: ops:: { Add , Sub , AddAssign , SubAssign } ;
18
+ use crate :: ops:: { Add , AddAssign , Sub , SubAssign } ;
19
19
use crate :: sys:: time;
20
- use crate :: sys_common:: FromInner ;
21
20
use crate :: sys_common:: mutex:: Mutex ;
21
+ use crate :: sys_common:: FromInner ;
22
22
23
23
#[ stable( feature = "time" , since = "1.3.0" ) ]
24
24
pub use core:: time:: Duration ;
@@ -216,17 +216,17 @@ impl Instant {
216
216
// * https://bugzilla.mozilla.org/show_bug.cgi?id=1487778 - a similar
217
217
// Firefox bug
218
218
//
219
- // It simply seems that this it just happens so that a lot in the wild
220
- // we 're seeing panics across various platforms where consecutive calls
219
+ // It seems that this just happens a lot in the wild.
220
+ // We 're seeing panics across various platforms where consecutive calls
221
221
// to `Instant::now`, such as via the `elapsed` function, are panicking
222
222
// as they're going backwards. Placed here is a last-ditch effort to try
223
223
// to fix things up. We keep a global "latest now" instance which is
224
224
// returned instead of what the OS says if the OS goes backwards.
225
225
//
226
- // To hopefully mitigate the impact of this though a few platforms are
226
+ // To hopefully mitigate the impact of this, a few platforms are
227
227
// whitelisted as "these at least haven't gone backwards yet".
228
228
if time:: Instant :: actually_monotonic ( ) {
229
- return Instant ( os_now)
229
+ return Instant ( os_now) ;
230
230
}
231
231
232
232
static LOCK : Mutex = Mutex :: new ( ) ;
@@ -353,8 +353,7 @@ impl Add<Duration> for Instant {
353
353
///
354
354
/// [`checked_add`]: ../../std/time/struct.Instant.html#method.checked_add
355
355
fn add ( self , other : Duration ) -> Instant {
356
- self . checked_add ( other)
357
- . expect ( "overflow when adding duration to instant" )
356
+ self . checked_add ( other) . expect ( "overflow when adding duration to instant" )
358
357
}
359
358
}
360
359
@@ -370,8 +369,7 @@ impl Sub<Duration> for Instant {
370
369
type Output = Instant ;
371
370
372
371
fn sub ( self , other : Duration ) -> Instant {
373
- self . checked_sub ( other)
374
- . expect ( "overflow when subtracting duration from instant" )
372
+ self . checked_sub ( other) . expect ( "overflow when subtracting duration from instant" )
375
373
}
376
374
}
377
375
@@ -464,8 +462,7 @@ impl SystemTime {
464
462
/// println!("{:?}", difference);
465
463
/// ```
466
464
#[ stable( feature = "time2" , since = "1.8.0" ) ]
467
- pub fn duration_since ( & self , earlier : SystemTime )
468
- -> Result < Duration , SystemTimeError > {
465
+ pub fn duration_since ( & self , earlier : SystemTime ) -> Result < Duration , SystemTimeError > {
469
466
self . 0 . sub_time ( & earlier. 0 ) . map_err ( SystemTimeError )
470
467
}
471
468
@@ -532,8 +529,7 @@ impl Add<Duration> for SystemTime {
532
529
///
533
530
/// [`checked_add`]: ../../std/time/struct.SystemTime.html#method.checked_add
534
531
fn add ( self , dur : Duration ) -> SystemTime {
535
- self . checked_add ( dur)
536
- . expect ( "overflow when adding duration to instant" )
532
+ self . checked_add ( dur) . expect ( "overflow when adding duration to instant" )
537
533
}
538
534
}
539
535
@@ -549,8 +545,7 @@ impl Sub<Duration> for SystemTime {
549
545
type Output = SystemTime ;
550
546
551
547
fn sub ( self , dur : Duration ) -> SystemTime {
552
- self . checked_sub ( dur)
553
- . expect ( "overflow when subtracting duration from instant" )
548
+ self . checked_sub ( dur) . expect ( "overflow when subtracting duration from instant" )
554
549
}
555
550
}
556
551
@@ -626,7 +621,9 @@ impl SystemTimeError {
626
621
627
622
#[ stable( feature = "time2" , since = "1.8.0" ) ]
628
623
impl Error for SystemTimeError {
629
- fn description ( & self ) -> & str { "other time was not earlier than self" }
624
+ fn description ( & self ) -> & str {
625
+ "other time was not earlier than self"
626
+ }
630
627
}
631
628
632
629
#[ stable( feature = "time2" , since = "1.8.0" ) ]
@@ -644,17 +641,16 @@ impl FromInner<time::SystemTime> for SystemTime {
644
641
645
642
#[ cfg( test) ]
646
643
mod tests {
647
- use super :: { Instant , SystemTime , Duration , UNIX_EPOCH } ;
644
+ use super :: { Duration , Instant , SystemTime , UNIX_EPOCH } ;
648
645
649
646
macro_rules! assert_almost_eq {
650
- ( $a: expr, $b: expr) => ( {
647
+ ( $a: expr, $b: expr) => { {
651
648
let ( a, b) = ( $a, $b) ;
652
649
if a != b {
653
- let ( a, b) = if a > b { ( a, b) } else { ( b, a) } ;
654
- assert!( a - Duration :: new( 0 , 1000 ) <= b,
655
- "{:?} is not almost equal to {:?}" , a, b) ;
650
+ let ( a, b) = if a > b { ( a, b) } else { ( b, a) } ;
651
+ assert!( a - Duration :: new( 0 , 1000 ) <= b, "{:?} is not almost equal to {:?}" , a, b) ;
656
652
}
657
- } )
653
+ } } ;
658
654
}
659
655
660
656
#[ test]
@@ -729,7 +725,7 @@ mod tests {
729
725
fn instant_saturating_duration_since_nopanic ( ) {
730
726
let a = Instant :: now ( ) ;
731
727
let ret = ( a - Duration :: new ( 1 , 0 ) ) . saturating_duration_since ( a) ;
732
- assert_eq ! ( ret, Duration :: new( 0 , 0 ) ) ;
728
+ assert_eq ! ( ret, Duration :: new( 0 , 0 ) ) ;
733
729
}
734
730
735
731
#[ test]
@@ -755,15 +751,14 @@ mod tests {
755
751
756
752
let second = Duration :: new ( 1 , 0 ) ;
757
753
assert_almost_eq ! ( a. duration_since( a - second) . unwrap( ) , second) ;
758
- assert_almost_eq ! ( a. duration_since( a + second) . unwrap_err( )
759
- . duration( ) , second) ;
754
+ assert_almost_eq ! ( a. duration_since( a + second) . unwrap_err( ) . duration( ) , second) ;
760
755
761
756
assert_almost_eq ! ( a - second + second, a) ;
762
757
assert_almost_eq ! ( a. checked_sub( second) . unwrap( ) . checked_add( second) . unwrap( ) , a) ;
763
758
764
759
let one_second_from_epoch = UNIX_EPOCH + Duration :: new ( 1 , 0 ) ;
765
- let one_second_from_epoch2 = UNIX_EPOCH + Duration :: new ( 0 , 500_000_000 )
766
- + Duration :: new ( 0 , 500_000_000 ) ;
760
+ let one_second_from_epoch2 =
761
+ UNIX_EPOCH + Duration :: new ( 0 , 500_000_000 ) + Duration :: new ( 0 , 500_000_000 ) ;
767
762
assert_eq ! ( one_second_from_epoch, one_second_from_epoch2) ;
768
763
769
764
// checked_add_duration will not panic on overflow
0 commit comments