@@ -92,30 +92,38 @@ impl MonitorEvaluator for MonitorEvalHandle {
9292 }
9393}
9494
95- /// Get offset between HMON and monitor starting time points as [`u32`].
96- pub ( crate ) fn hmon_time_offset ( hmon_starting_point : Instant , monitor_starting_point : Instant ) -> u32 {
95+ /// Get offset between HMON and monitor starting time points as integers.
96+ pub ( crate ) fn hmon_time_offset < T > ( hmon_starting_point : Instant , monitor_starting_point : Instant ) -> T
97+ where
98+ T : TryFrom < u128 > ,
99+ <T as TryFrom < u128 > >:: Error : core:: fmt:: Debug ,
100+ {
97101 let result = hmon_starting_point. checked_duration_since ( monitor_starting_point) ;
98102 let duration_since = result. expect ( "HMON starting point is earlier than monitor starting point" ) ;
99- duration_to_u32 ( duration_since)
103+ duration_to_int ( duration_since)
100104}
101105
102- /// Get duration as [`u32`].
103- pub ( crate ) fn duration_to_u32 ( duration : Duration ) -> u32 {
106+ /// Get duration as an integer.
107+ pub ( crate ) fn duration_to_int < T > ( duration : Duration ) -> T
108+ where
109+ T : TryFrom < u128 > ,
110+ <T as TryFrom < u128 > >:: Error : core:: fmt:: Debug ,
111+ {
104112 let millis = duration. as_millis ( ) ;
105- u32 :: try_from ( millis) . expect ( "Monitor running for too long" )
113+ T :: try_from ( millis) . expect ( "Monitor running for too long" )
106114}
107115
108116#[ cfg( all( test, not( loom) ) ) ]
109117mod tests {
110- use crate :: common:: { duration_to_u32 , hmon_time_offset} ;
118+ use crate :: common:: { duration_to_int , hmon_time_offset} ;
111119 use core:: time:: Duration ;
112120 use std:: time:: Instant ;
113121
114122 #[ test]
115123 fn hmon_time_offset_valid ( ) {
116124 let monitor_starting_point = Instant :: now ( ) ;
117125 let hmon_starting_point = Instant :: now ( ) ;
118- let offset = hmon_time_offset ( hmon_starting_point, monitor_starting_point) ;
126+ let offset: u32 = hmon_time_offset ( hmon_starting_point, monitor_starting_point) ;
119127 // Allow small offset.
120128 assert ! ( offset < 10 ) ;
121129 }
@@ -125,7 +133,7 @@ mod tests {
125133 fn hmon_time_offset_wrong_order ( ) {
126134 let hmon_starting_point = Instant :: now ( ) ;
127135 let monitor_starting_point = Instant :: now ( ) ;
128- let _offset = hmon_time_offset ( hmon_starting_point, monitor_starting_point) ;
136+ let _offset: u32 = hmon_time_offset ( hmon_starting_point, monitor_starting_point) ;
129137 }
130138
131139 #[ test]
@@ -136,19 +144,19 @@ mod tests {
136144 let hmon_starting_point = Instant :: now ( )
137145 . checked_add ( Duration :: from_secs ( HUNDRED_DAYS_AS_SECS ) )
138146 . unwrap ( ) ;
139- let _offset = hmon_time_offset ( hmon_starting_point, monitor_starting_point) ;
147+ let _offset: u32 = hmon_time_offset ( hmon_starting_point, monitor_starting_point) ;
140148 }
141149
142150 #[ test]
143- fn duration_to_u32_valid ( ) {
144- let result = duration_to_u32 ( Duration :: from_millis ( 1234 ) ) ;
151+ fn duration_to_int_valid ( ) {
152+ let result: u32 = duration_to_int ( Duration :: from_millis ( 1234 ) ) ;
145153 assert_eq ! ( result, 1234 ) ;
146154 }
147155
148156 #[ test]
149157 #[ should_panic( expected = "Monitor running for too long" ) ]
150- fn duration_to_u32_too_large ( ) {
158+ fn duration_to_int_too_large ( ) {
151159 const HUNDRED_DAYS_AS_SECS : u64 = 100 * 24 * 60 * 60 ;
152- let _result = duration_to_u32 ( Duration :: from_secs ( HUNDRED_DAYS_AS_SECS ) ) ;
160+ let _result: u32 = duration_to_int ( Duration :: from_secs ( HUNDRED_DAYS_AS_SECS ) ) ;
153161 }
154162}
0 commit comments