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 1
1
use super :: measurement:: * ;
2
+ use super :: Length ;
3
+ use std:: time:: Duration ;
2
4
3
5
/// The `Speed` struct can be used to deal with speeds in a common way.
4
6
/// Common metric and imperial units are supported.
@@ -60,6 +62,16 @@ impl Speed {
60
62
}
61
63
}
62
64
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
+
63
75
impl Measurement for Speed {
64
76
fn get_base_units ( & self ) -> f64 {
65
77
self . meters_per_second
Original file line number Diff line number Diff line change 1
1
use speed:: * ;
2
2
use super :: assert_almost_eq;
3
+ use std:: time:: Duration ;
4
+ use length:: Length ;
3
5
4
6
// Metric
5
7
#[ test]
@@ -14,6 +16,16 @@ fn kilometers_per_hour() {
14
16
assert_almost_eq ( r2, 27.7777777777 ) ;
15
17
}
16
18
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]
17
29
fn kilometres_per_hour ( ) {
18
30
let i1 = Speed :: from_metres_per_second ( 100.0 ) ;
19
31
let r1 = i1. as_kilometres_per_hour ( ) ;
You can’t perform that action at this time.
0 commit comments