@@ -321,3 +321,104 @@ fn debug_formatting_precision_high() {
321321 assert_eq ! ( format!( "{:.10?}" , Duration :: new( 4 , 001_000_000 ) ) , "4.0010000000s" ) ;
322322 assert_eq ! ( format!( "{:.20?}" , Duration :: new( 4 , 001_000_000 ) ) , "4.00100000000000000000s" ) ;
323323}
324+
325+ #[ test]
326+ fn duration_const ( ) {
327+ // test that the methods of `Duration` are usable in a const context
328+
329+ const DURATION : Duration = Duration :: new ( 0 , 123_456_789 ) ;
330+
331+ const SUB_SEC_MILLIS : u32 = DURATION . subsec_millis ( ) ;
332+ assert_eq ! ( SUB_SEC_MILLIS , 123 ) ;
333+
334+ const SUB_SEC_MICROS : u32 = DURATION . subsec_micros ( ) ;
335+ assert_eq ! ( SUB_SEC_MICROS , 123_456 ) ;
336+
337+ const SUB_SEC_NANOS : u32 = DURATION . subsec_nanos ( ) ;
338+ assert_eq ! ( SUB_SEC_NANOS , 123_456_789 ) ;
339+
340+ const ZERO : Duration = Duration :: zero ( ) ;
341+ assert_eq ! ( ZERO , Duration :: new( 0 , 0 ) ) ;
342+
343+ const IS_ZERO : bool = ZERO . is_zero ( ) ;
344+ assert ! ( IS_ZERO ) ;
345+
346+ const ONE : Duration = Duration :: new ( 1 , 0 ) ;
347+
348+ const SECONDS : u64 = ONE . as_secs ( ) ;
349+ assert_eq ! ( SECONDS , 1 ) ;
350+
351+ const FROM_SECONDS : Duration = Duration :: from_secs ( 1 ) ;
352+ assert_eq ! ( FROM_SECONDS , ONE ) ;
353+
354+ const SECONDS_F32 : f32 = ONE . as_secs_f32 ( ) ;
355+ assert_eq ! ( SECONDS_F32 , 1.0 ) ;
356+
357+ const FROM_SECONDS_F32 : Duration = Duration :: from_secs_f32 ( 1.0 ) ;
358+ assert_eq ! ( FROM_SECONDS_F32 , ONE ) ;
359+
360+ const SECONDS_F64 : f64 = ONE . as_secs_f64 ( ) ;
361+ assert_eq ! ( SECONDS_F64 , 1.0 ) ;
362+
363+ const FROM_SECONDS_F64 : Duration = Duration :: from_secs_f64 ( 1.0 ) ;
364+ assert_eq ! ( FROM_SECONDS_F64 , ONE ) ;
365+
366+ const MILLIS : u128 = ONE . as_millis ( ) ;
367+ assert_eq ! ( MILLIS , 1_000 ) ;
368+
369+ const FROM_MILLIS : Duration = Duration :: from_millis ( 1_000 ) ;
370+ assert_eq ! ( FROM_MILLIS , ONE ) ;
371+
372+ const MICROS : u128 = ONE . as_micros ( ) ;
373+ assert_eq ! ( MICROS , 1_000_000 ) ;
374+
375+ const FROM_MICROS : Duration = Duration :: from_micros ( 1_000_000 ) ;
376+ assert_eq ! ( FROM_MICROS , ONE ) ;
377+
378+ const NANOS : u128 = ONE . as_nanos ( ) ;
379+ assert_eq ! ( NANOS , 1_000_000_000 ) ;
380+
381+ const FROM_NANOS : Duration = Duration :: from_nanos ( 1_000_000_000 ) ;
382+ assert_eq ! ( FROM_NANOS , ONE ) ;
383+
384+ const MAX : Duration = Duration :: new ( u64:: MAX , 999_999_999 ) ;
385+
386+ const CHECKED_ADD : Option < Duration > = MAX . checked_add ( ONE ) ;
387+ assert_eq ! ( CHECKED_ADD , None ) ;
388+
389+ const CHECKED_SUB : Option < Duration > = ZERO . checked_sub ( ONE ) ;
390+ assert_eq ! ( CHECKED_SUB , None ) ;
391+
392+ const CHECKED_MUL : Option < Duration > = ONE . checked_mul ( 1 ) ;
393+ assert_eq ! ( CHECKED_MUL , Some ( ONE ) ) ;
394+
395+ const MUL_F32 : Duration = ONE . mul_f32 ( 1.0 ) ;
396+ assert_eq ! ( MUL_F32 , ONE ) ;
397+
398+ const MUL_F64 : Duration = ONE . mul_f64 ( 1.0 ) ;
399+ assert_eq ! ( MUL_F64 , ONE ) ;
400+
401+ const CHECKED_DIV : Option < Duration > = ONE . checked_div ( 1 ) ;
402+ assert_eq ! ( CHECKED_DIV , Some ( ONE ) ) ;
403+
404+ const DIV_F32 : Duration = ONE . div_f32 ( 1.0 ) ;
405+ assert_eq ! ( DIV_F32 , ONE ) ;
406+
407+ const DIV_F64 : Duration = ONE . div_f64 ( 1.0 ) ;
408+ assert_eq ! ( DIV_F64 , ONE ) ;
409+
410+ const DIV_DURATION_F32 : f32 = ONE . div_duration_f32 ( ONE ) ;
411+ assert_eq ! ( DIV_DURATION_F32 , 1.0 ) ;
412+
413+ const DIV_DURATION_F64 : f64 = ONE . div_duration_f64 ( ONE ) ;
414+ assert_eq ! ( DIV_DURATION_F64 , 1.0 ) ;
415+
416+ const SATURATING_ADD : Duration = MAX . saturating_add ( ONE ) ;
417+ assert_eq ! ( SATURATING_ADD , MAX ) ;
418+
419+ const SATURATING_SUB : Duration = ZERO . saturating_sub ( ONE ) ;
420+ assert_eq ! ( SATURATING_SUB , ZERO ) ;
421+
422+ const SATURATING_MUL : Duration = MAX . saturating_mul ( 2 ) ;
423+ assert_eq ! ( SATURATING_MUL , MAX ) ;
424+ }
0 commit comments