File tree Expand file tree Collapse file tree 2 files changed +24
-0
lines changed Expand file tree Collapse file tree 2 files changed +24
-0
lines changed Original file line number Diff line number Diff line change 11use super :: measurement:: * ;
2+ use super :: Length ;
3+ use std:: time:: Duration ;
24
35/// The `Speed` struct can be used to deal with speeds in a common way.
46/// Common metric and imperial units are supported.
@@ -60,6 +62,16 @@ impl Speed {
6062 }
6163}
6264
65+ impl :: std:: ops:: Div < Duration > for Length {
66+ type Output = Speed ;
67+
68+ fn div ( self , rhs : Duration ) -> Speed {
69+ // It would be useful if Duration had a method that did this...
70+ let seconds: f64 = rhs. as_secs ( ) as f64 + ( ( rhs. subsec_nanos ( ) as f64 ) * 1e-9 ) ;
71+ Speed :: from_meters_per_second ( self . as_meters ( ) / seconds)
72+ }
73+ }
74+
6375impl Measurement for Speed {
6476 fn get_base_units ( & self ) -> f64 {
6577 self . meters_per_second
Original file line number Diff line number Diff line change 11use speed:: * ;
22use super :: assert_almost_eq;
3+ use std:: time:: Duration ;
4+ use length:: Length ;
35
46// Metric
57#[ test]
@@ -14,6 +16,16 @@ fn kilometers_per_hour() {
1416 assert_almost_eq ( r2, 27.7777777777 ) ;
1517}
1618
19+ #[ test]
20+ fn length_over_time ( ) {
21+ let l1 = Length :: from_meters ( 3.0 ) ;
22+ let t1 = Duration :: new ( 1 , 500_000_000 ) ;
23+ let i1 = l1 / t1;
24+ let r1 = i1. as_meters_per_second ( ) ;
25+ assert_almost_eq ( r1, 2.0 ) ;
26+ }
27+
28+ #[ test]
1729fn kilometres_per_hour ( ) {
1830 let i1 = Speed :: from_metres_per_second ( 100.0 ) ;
1931 let r1 = i1. as_kilometres_per_hour ( ) ;
You can’t perform that action at this time.
0 commit comments