@@ -18,126 +18,126 @@ const METER_MILE_FACTOR: f64 = 0.000621371192237;
18
18
19
19
/// The `Length` struct can be used to deal with lengths in a common way.
20
20
/// Common metric and imperial units are supported.
21
- ///
21
+ ///
22
22
/// # Example
23
- ///
23
+ ///
24
24
/// ```
25
25
/// use measurements::Length;
26
- ///
26
+ ///
27
27
/// let football_field = Length::from_yards(100.0);
28
28
/// let meters = football_field.as_meters();
29
29
/// println!("There are {} meters in a football field.", meters);
30
30
/// ```
31
31
#[ derive( Copy , Clone , Debug ) ]
32
32
pub struct Length {
33
- meters : f64
33
+ meters : f64 ,
34
34
}
35
35
36
36
impl Length {
37
37
// Inputs, metric
38
38
pub fn from_meters ( meters : f64 ) -> Self {
39
39
Length { meters : meters }
40
40
}
41
-
41
+
42
42
pub fn from_nanometers ( nanometers : f64 ) -> Self {
43
43
Self :: from_meters ( nanometers / METER_NANOMETER_FACTOR )
44
44
}
45
-
45
+
46
46
pub fn from_micrometers ( micrometers : f64 ) -> Self {
47
47
Self :: from_meters ( micrometers / METER_MICROMETER_FACTOR )
48
48
}
49
-
49
+
50
50
pub fn from_millimeters ( millimeters : f64 ) -> Self {
51
51
Self :: from_meters ( millimeters / METER_MILLIMETER_FACTOR )
52
52
}
53
-
53
+
54
54
pub fn from_centimeters ( centimeters : f64 ) -> Self {
55
55
Self :: from_meters ( centimeters / METER_CENTIMETER_FACTOR )
56
56
}
57
-
57
+
58
58
pub fn from_decameters ( decameters : f64 ) -> Self {
59
59
Self :: from_meters ( decameters / METER_DECAMETER_FACTOR )
60
60
}
61
-
61
+
62
62
pub fn from_hectometers ( hectometers : f64 ) -> Self {
63
63
Self :: from_meters ( hectometers / METER_HECTOMETER_FACTOR )
64
64
}
65
-
65
+
66
66
pub fn from_kilometers ( kilometers : f64 ) -> Self {
67
67
Self :: from_meters ( kilometers / METER_KILOMETER_FACTOR )
68
68
}
69
-
69
+
70
70
// Inputs, imperial
71
71
pub fn from_inches ( inches : f64 ) -> Self {
72
72
Self :: from_meters ( inches / METER_INCH_FACTOR )
73
73
}
74
-
74
+
75
75
pub fn from_feet ( feet : f64 ) -> Self {
76
76
Self :: from_meters ( feet / METER_FEET_FACTOR )
77
77
}
78
-
78
+
79
79
pub fn from_yards ( yards : f64 ) -> Self {
80
80
Self :: from_meters ( yards / METER_YARD_FACTOR )
81
81
}
82
-
82
+
83
83
pub fn from_furlongs ( furlongs : f64 ) -> Self {
84
84
Self :: from_meters ( furlongs / METER_FURLONG_FACTOR )
85
85
}
86
-
86
+
87
87
pub fn from_miles ( miles : f64 ) -> Self {
88
88
Self :: from_meters ( miles / METER_MILE_FACTOR )
89
89
}
90
-
90
+
91
91
// Outputs, metric
92
92
pub fn as_nanometers ( & self ) -> f64 {
93
93
self . meters * METER_NANOMETER_FACTOR
94
94
}
95
-
95
+
96
96
pub fn as_micrometers ( & self ) -> f64 {
97
97
self . meters * METER_MICROMETER_FACTOR
98
98
}
99
-
99
+
100
100
pub fn as_millimeters ( & self ) -> f64 {
101
101
self . meters * METER_MILLIMETER_FACTOR
102
102
}
103
-
103
+
104
104
pub fn as_centimeters ( & self ) -> f64 {
105
105
self . meters * METER_CENTIMETER_FACTOR
106
106
}
107
-
107
+
108
108
pub fn as_meters ( & self ) -> f64 {
109
109
self . meters
110
110
}
111
-
111
+
112
112
pub fn as_decameters ( & self ) -> f64 {
113
113
self . meters * METER_DECAMETER_FACTOR
114
114
}
115
-
115
+
116
116
pub fn as_hectometer ( & self ) -> f64 {
117
117
self . meters * METER_HECTOMETER_FACTOR
118
118
}
119
-
119
+
120
120
pub fn as_kilometers ( & self ) -> f64 {
121
121
self . meters * METER_KILOMETER_FACTOR
122
122
}
123
-
123
+
124
124
// Outputs, imperial
125
125
pub fn as_inches ( & self ) -> f64 {
126
126
self . meters * METER_INCH_FACTOR
127
127
}
128
-
128
+
129
129
pub fn as_feet ( & self ) -> f64 {
130
130
self . meters * METER_FEET_FACTOR
131
131
}
132
-
132
+
133
133
pub fn as_yards ( & self ) -> f64 {
134
134
self . meters * METER_YARD_FACTOR
135
135
}
136
-
136
+
137
137
pub fn as_furlongs ( & self ) -> f64 {
138
138
self . meters * METER_FURLONG_FACTOR
139
139
}
140
-
140
+
141
141
pub fn as_miles ( & self ) -> f64 {
142
142
self . meters * METER_MILE_FACTOR
143
143
}
@@ -147,10 +147,16 @@ impl Measurement for Length {
147
147
fn get_base_units ( & self ) -> f64 {
148
148
self . meters
149
149
}
150
-
150
+
151
151
fn from_base_units ( units : f64 ) -> Self {
152
152
Self :: from_meters ( units)
153
153
}
154
154
}
155
155
156
156
implement_measurement ! { Length }
157
+
158
+ impl :: std:: fmt:: Display for Length {
159
+ fn fmt ( & self , f : & mut :: std:: fmt:: Formatter ) -> :: std:: fmt:: Result {
160
+ write ! ( f, "{:.1} m" , self . as_meters( ) )
161
+ }
162
+ }
0 commit comments