@@ -40,7 +40,7 @@ const TIMEOUT: Duration = Duration::from_secs(30);
4040/// So, I implemented a global time dependent on `futures-timer`,
4141/// Because in the browser environment, it is always single-threaded, so feel free to be unsafe
4242#[ cfg( target_arch = "wasm32" ) ]
43- static mut TIME : Instant = Instant :: from_f64 ( 0. 0) ;
43+ static mut TIME : Instant = Instant :: from_u64 ( 0 ) ;
4444
4545/// The session
4646pub struct Session < T > {
@@ -276,7 +276,7 @@ where
276276 if self
277277 . pings
278278 . iter ( )
279- . any ( |( _id, time) | time . elapsed ( ) > TIMEOUT )
279+ . any ( |( _id, time) | Instant :: now ( ) . saturating_duration_since ( * time ) > TIMEOUT )
280280 {
281281 return Err ( io:: ErrorKind :: TimedOut . into ( ) ) ;
282282 }
@@ -794,7 +794,7 @@ mod timer {
794794 #[ derive( Debug , Copy , Clone ) ]
795795 pub struct Instant {
796796 /// mock
797- inner : f64 ,
797+ inner : u64 ,
798798 }
799799
800800 impl PartialEq for Instant {
@@ -820,7 +820,7 @@ mod timer {
820820 }
821821
822822 impl Instant {
823- pub const fn from_f64 ( val : f64 ) -> Self {
823+ pub const fn from_u64 ( val : u64 ) -> Self {
824824 Instant { inner : val }
825825 }
826826
@@ -832,6 +832,10 @@ mod timer {
832832 * self - earlier
833833 }
834834
835+ pub fn saturating_duration_since ( & self , earlier : Instant ) -> Duration {
836+ * self - earlier
837+ }
838+
835839 pub fn elapsed ( & self ) -> Duration {
836840 Instant :: now ( ) - * self
837841 }
@@ -841,31 +845,29 @@ mod timer {
841845 type Output = Instant ;
842846
843847 fn add ( self , other : Duration ) -> Instant {
844- let new_val = self . inner + other. as_millis ( ) as f64 ;
845- Instant {
846- inner : new_val as f64 ,
847- }
848+ let new_val = self . inner + other. as_millis ( ) as u64 ;
849+ Instant { inner : new_val }
848850 }
849851 }
850852
851853 impl Sub < Duration > for Instant {
852854 type Output = Instant ;
853855
854856 fn sub ( self , other : Duration ) -> Instant {
855- let new_val = self . inner - other. as_millis ( ) as f64 ;
856- Instant {
857- inner : new_val as f64 ,
858- }
857+ let new_val = self
858+ . inner
859+ . checked_sub ( other. as_millis ( ) as u64 )
860+ . unwrap_or_default ( ) ;
861+ Instant { inner : new_val }
859862 }
860863 }
861864
862865 impl Sub < Instant > for Instant {
863866 type Output = Duration ;
864867
865868 fn sub ( self , other : Instant ) -> Duration {
866- let ms = self . inner - other. inner ;
867- assert ! ( ms >= 0.0 ) ;
868- Duration :: from_millis ( ms as u64 )
869+ let ms = self . inner . checked_sub ( other. inner ) . unwrap_or_default ( ) ;
870+ Duration :: from_millis ( ms)
869871 }
870872 }
871873
0 commit comments