7
7
//! by an Area to get a Pressure.
8
8
9
9
#![ deny( warnings, missing_docs) ]
10
- #![ no_std]
11
10
12
- extern crate time;
11
+ #![ cfg_attr( feature="no_std" , no_std) ]
12
+
13
+ #[ cfg( feature = "no_std" ) ]
14
+ use core as std;
15
+ #[ cfg( feature = "no_std" ) ]
16
+ use core:: time as time;
17
+
18
+ #[ cfg( not( feature = "no_std" ) ) ]
19
+ use std:: time as time;
20
+
21
+ use std:: f64:: consts:: PI as PI ;
13
22
14
23
#[ macro_use]
15
24
mod measurement;
@@ -76,6 +85,8 @@ mod torque_energy;
76
85
pub use torque_energy:: TorqueEnergy ;
77
86
78
87
pub mod prelude;
88
+
89
+ #[ cfg( test) ]
79
90
pub mod test_utils;
80
91
81
92
/// For given types A, B and C, implement, using base units:
@@ -85,15 +96,15 @@ pub mod test_utils;
85
96
/// - C = A / B
86
97
macro_rules! impl_maths {
87
98
( $a: ty, $b: ty) => {
88
- impl :: core :: ops:: Mul <$b> for $b {
99
+ impl std :: ops:: Mul <$b> for $b {
89
100
type Output = $a;
90
101
91
102
fn mul( self , rhs: $b) -> Self :: Output {
92
103
Self :: Output :: from_base_units( self . as_base_units( ) * rhs. as_base_units( ) )
93
104
}
94
105
}
95
106
96
- impl :: core :: ops:: Div <$b> for $a {
107
+ impl std :: ops:: Div <$b> for $a {
97
108
type Output = $b;
98
109
99
110
fn div( self , rhs: $b) -> Self :: Output {
@@ -103,31 +114,31 @@ macro_rules! impl_maths {
103
114
} ;
104
115
105
116
( $a: ty, $b: ty, $c: ty) => {
106
- impl :: core :: ops:: Mul <$b> for $c {
117
+ impl std :: ops:: Mul <$b> for $c {
107
118
type Output = $a;
108
119
109
120
fn mul( self , rhs: $b) -> Self :: Output {
110
121
Self :: Output :: from_base_units( self . as_base_units( ) * rhs. as_base_units( ) )
111
122
}
112
123
}
113
124
114
- impl :: core :: ops:: Mul <$c> for $b {
125
+ impl std :: ops:: Mul <$c> for $b {
115
126
type Output = $a;
116
127
117
128
fn mul( self , rhs: $c) -> Self :: Output {
118
129
Self :: Output :: from_base_units( self . as_base_units( ) * rhs. as_base_units( ) )
119
130
}
120
131
}
121
132
122
- impl :: core :: ops:: Div <$c> for $a {
133
+ impl std :: ops:: Div <$c> for $a {
123
134
type Output = $b;
124
135
125
136
fn div( self , rhs: $c) -> Self :: Output {
126
137
Self :: Output :: from_base_units( self . as_base_units( ) / rhs. as_base_units( ) )
127
138
}
128
139
}
129
140
130
- impl :: core :: ops:: Div <$b> for $a {
141
+ impl std :: ops:: Div <$b> for $a {
131
142
type Output = $c;
132
143
133
144
fn div( self , rhs: $b) -> Self :: Output {
@@ -139,11 +150,13 @@ macro_rules! impl_maths {
139
150
140
151
impl Measurement for time:: Duration {
141
152
fn as_base_units ( & self ) -> f64 {
142
- ( self . num_microseconds ( ) . unwrap ( ) as f64 ) / 1e6
153
+ self . as_secs ( ) as f64 + ( f64 :: from ( self . subsec_nanos ( ) ) * 1e-9 )
143
154
}
144
155
145
156
fn from_base_units ( units : f64 ) -> Self {
146
- time:: Duration :: microseconds ( ( units * 1e6 ) as i64 )
157
+ let subsec_nanos = ( ( units * 1e9 ) % 1e9 ) as u32 ;
158
+ let secs = units as u64 ;
159
+ time:: Duration :: new ( secs, subsec_nanos)
147
160
}
148
161
149
162
fn get_base_units_name ( & self ) -> & ' static str {
@@ -171,34 +184,35 @@ impl_maths!(TorqueEnergy, Force, Length);
171
184
// Implement the divisions manually (the above macro only implemented the
172
185
// TorqueEnergy / X operations).
173
186
174
- impl :: core :: ops:: Div < Length > for Torque {
187
+ impl std :: ops:: Div < Length > for Torque {
175
188
type Output = Force ;
176
189
177
190
fn div ( self , rhs : Length ) -> Self :: Output {
178
191
Self :: Output :: from_base_units ( self . as_base_units ( ) / rhs. as_base_units ( ) )
179
192
}
180
193
}
181
194
182
- impl :: core :: ops:: Div < Force > for Torque {
195
+ impl std :: ops:: Div < Force > for Torque {
183
196
type Output = Length ;
184
197
185
198
fn div ( self , rhs : Force ) -> Self :: Output {
186
199
Self :: Output :: from_base_units ( self . as_base_units ( ) / rhs. as_base_units ( ) )
187
200
}
188
201
}
189
202
190
- impl :: core :: ops:: Div < Length > for Energy {
203
+ impl std :: ops:: Div < Length > for Energy {
191
204
type Output = Force ;
192
205
193
206
fn div ( self , rhs : Length ) -> Self :: Output {
194
207
Self :: Output :: from_base_units ( self . as_base_units ( ) / rhs. as_base_units ( ) )
195
208
}
196
209
}
197
210
198
- impl :: core :: ops:: Div < Force > for Energy {
211
+ impl std :: ops:: Div < Force > for Energy {
199
212
type Output = Length ;
200
213
201
214
fn div ( self , rhs : Force ) -> Self :: Output {
202
215
Self :: Output :: from_base_units ( self . as_base_units ( ) / rhs. as_base_units ( ) )
203
216
}
204
217
}
218
+
0 commit comments