7
7
///
8
8
/// # Example
9
9
/// ```
10
+ /// extern crate core;
10
11
/// // Importing the `implement_measurement` macro from the external crate is important
11
12
/// #[macro_use]
12
13
/// extern crate measurements;
13
14
///
14
15
/// use measurements::Measurement;
15
16
///
17
+ ///
16
18
/// struct Cubits {
17
19
/// forearms: f64
18
20
/// }
38
40
/// // You should't need it in your own code.
39
41
/// fn main() { }
40
42
/// ```
41
- ///
42
- /// *Note*: If you are in a `no_std` environment, you have to
43
- /// `use core as std;` for the macros to run.
44
43
pub trait Measurement {
45
44
/// Returns a string containing the most appropriate units for this quantity,
46
45
/// and a floating point value representing this quantity in those units.
@@ -80,13 +79,13 @@ pub trait Measurement {
80
79
}
81
80
82
81
/// This is a special macro that creates the code to implement
83
- /// `std ::fmt::Display`.
82
+ /// `core ::fmt::Display`.
84
83
#[ macro_export]
85
84
macro_rules! implement_display {
86
85
( $( $t: ty) * ) => ( $(
87
86
88
- impl :: std :: fmt:: Display for $t {
89
- fn fmt( & self , f: & mut :: std :: fmt:: Formatter ) -> :: std :: fmt:: Result {
87
+ impl :: core :: fmt:: Display for $t {
88
+ fn fmt( & self , f: & mut :: core :: fmt:: Formatter ) -> :: core :: fmt:: Result {
90
89
let ( unit, value) = self . get_appropriate_units( ) ;
91
90
value. fmt( f) ?; // Value
92
91
write!( f, "\u{00A0} {}" , unit)
@@ -103,15 +102,15 @@ macro_rules! implement_measurement {
103
102
104
103
implement_display!( $t ) ;
105
104
106
- impl :: std :: ops:: Add for $t {
105
+ impl :: core :: ops:: Add for $t {
107
106
type Output = Self ;
108
107
109
108
fn add( self , rhs: Self ) -> Self {
110
109
Self :: from_base_units( self . as_base_units( ) + rhs. as_base_units( ) )
111
110
}
112
111
}
113
112
114
- impl :: std :: ops:: Sub for $t {
113
+ impl :: core :: ops:: Sub for $t {
115
114
type Output = Self ;
116
115
117
116
fn sub( self , rhs: Self ) -> Self {
@@ -121,7 +120,7 @@ macro_rules! implement_measurement {
121
120
122
121
// Dividing a `$t` by another `$t` returns a ratio.
123
122
//
124
- impl :: std :: ops:: Div <$t> for $t {
123
+ impl :: core :: ops:: Div <$t> for $t {
125
124
type Output = f64 ;
126
125
127
126
fn div( self , rhs: Self ) -> f64 {
@@ -131,7 +130,7 @@ macro_rules! implement_measurement {
131
130
132
131
// Dividing a `$t` by a factor returns a new portion of the measurement.
133
132
//
134
- impl :: std :: ops:: Div <f64 > for $t {
133
+ impl :: core :: ops:: Div <f64 > for $t {
135
134
type Output = Self ;
136
135
137
136
fn div( self , rhs: f64 ) -> Self {
@@ -141,7 +140,7 @@ macro_rules! implement_measurement {
141
140
142
141
// Multiplying a `$t` by a factor increases (or decreases) that
143
142
// measurement a number of times.
144
- impl :: std :: ops:: Mul <f64 > for $t {
143
+ impl :: core :: ops:: Mul <f64 > for $t {
145
144
type Output = Self ;
146
145
147
146
fn mul( self , rhs: f64 ) -> Self {
@@ -150,23 +149,23 @@ macro_rules! implement_measurement {
150
149
}
151
150
152
151
// Multiplying `$t` by a factor is commutative
153
- impl :: std :: ops:: Mul <$t> for f64 {
152
+ impl :: core :: ops:: Mul <$t> for f64 {
154
153
type Output = $t;
155
154
156
155
fn mul( self , rhs: $t) -> $t {
157
156
rhs * self
158
157
}
159
158
}
160
159
161
- impl :: std :: cmp:: Eq for $t { }
162
- impl :: std :: cmp:: PartialEq for $t {
160
+ impl :: core :: cmp:: Eq for $t { }
161
+ impl :: core :: cmp:: PartialEq for $t {
163
162
fn eq( & self , other: & Self ) -> bool {
164
163
self . as_base_units( ) == other. as_base_units( )
165
164
}
166
165
}
167
166
168
- impl :: std :: cmp:: PartialOrd for $t {
169
- fn partial_cmp( & self , other: & Self ) -> Option <:: std :: cmp:: Ordering > {
167
+ impl :: core :: cmp:: PartialOrd for $t {
168
+ fn partial_cmp( & self , other: & Self ) -> Option <:: core :: cmp:: Ordering > {
170
169
self . as_base_units( ) . partial_cmp( & other. as_base_units( ) )
171
170
}
172
171
}
0 commit comments