Skip to content

Commit 0f03a8b

Browse files
committed
Added Pressure.
Implement std::fmt::Display using SI units. Also removed trailing whitespace.
1 parent 6c8c57a commit 0f03a8b

File tree

7 files changed

+247
-125
lines changed

7 files changed

+247
-125
lines changed

src/length.rs

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ const METER_MILE_FACTOR: f64 = 0.000621371192237;
1818

1919
/// The `Length` struct can be used to deal with lengths in a common way.
2020
/// Common metric and imperial units are supported.
21-
///
21+
///
2222
/// # Example
23-
///
23+
///
2424
/// ```
2525
/// use measurements::Length;
26-
///
26+
///
2727
/// let football_field = Length::from_yards(100.0);
2828
/// let meters = football_field.as_meters();
2929
/// println!("There are {} meters in a football field.", meters);
@@ -38,106 +38,106 @@ impl Length {
3838
pub fn from_meters(meters: f64) -> Self {
3939
Length { meters: meters }
4040
}
41-
41+
4242
pub fn from_nanometers(nanometers: f64) -> Self {
4343
Self::from_meters(nanometers / METER_NANOMETER_FACTOR)
4444
}
45-
45+
4646
pub fn from_micrometers(micrometers: f64) -> Self {
4747
Self::from_meters(micrometers / METER_MICROMETER_FACTOR)
4848
}
49-
49+
5050
pub fn from_millimeters(millimeters: f64) -> Self {
5151
Self::from_meters(millimeters / METER_MILLIMETER_FACTOR)
5252
}
53-
53+
5454
pub fn from_centimeters(centimeters: f64) -> Self {
5555
Self::from_meters(centimeters / METER_CENTIMETER_FACTOR)
5656
}
57-
57+
5858
pub fn from_decameters(decameters: f64) -> Self {
5959
Self::from_meters(decameters / METER_DECAMETER_FACTOR)
6060
}
61-
61+
6262
pub fn from_hectometers(hectometers: f64) -> Self {
6363
Self::from_meters(hectometers / METER_HECTOMETER_FACTOR)
6464
}
65-
65+
6666
pub fn from_kilometers(kilometers: f64) -> Self {
6767
Self::from_meters(kilometers / METER_KILOMETER_FACTOR)
6868
}
69-
69+
7070
// Inputs, imperial
7171
pub fn from_inches(inches: f64) -> Self {
7272
Self::from_meters(inches / METER_INCH_FACTOR)
7373
}
74-
74+
7575
pub fn from_feet(feet: f64) -> Self {
7676
Self::from_meters(feet / METER_FEET_FACTOR)
7777
}
78-
78+
7979
pub fn from_yards(yards: f64) -> Self {
8080
Self::from_meters(yards / METER_YARD_FACTOR)
8181
}
82-
82+
8383
pub fn from_furlongs(furlongs: f64) -> Self {
8484
Self::from_meters(furlongs / METER_FURLONG_FACTOR)
8585
}
86-
86+
8787
pub fn from_miles(miles: f64) -> Self {
8888
Self::from_meters(miles / METER_MILE_FACTOR)
8989
}
90-
90+
9191
// Outputs, metric
9292
pub fn as_nanometers(&self) -> f64 {
9393
self.meters * METER_NANOMETER_FACTOR
9494
}
95-
95+
9696
pub fn as_micrometers(&self) -> f64 {
9797
self.meters * METER_MICROMETER_FACTOR
9898
}
99-
99+
100100
pub fn as_millimeters(&self) -> f64 {
101101
self.meters * METER_MILLIMETER_FACTOR
102102
}
103-
103+
104104
pub fn as_centimeters(&self) -> f64 {
105105
self.meters * METER_CENTIMETER_FACTOR
106106
}
107-
107+
108108
pub fn as_meters(&self) -> f64 {
109109
self.meters
110110
}
111-
111+
112112
pub fn as_decameters(&self) -> f64 {
113113
self.meters * METER_DECAMETER_FACTOR
114114
}
115-
115+
116116
pub fn as_hectometer(&self) -> f64 {
117117
self.meters * METER_HECTOMETER_FACTOR
118118
}
119-
119+
120120
pub fn as_kilometers(&self) -> f64 {
121121
self.meters * METER_KILOMETER_FACTOR
122122
}
123-
123+
124124
// Outputs, imperial
125125
pub fn as_inches(&self) -> f64 {
126126
self.meters * METER_INCH_FACTOR
127127
}
128-
128+
129129
pub fn as_feet(&self) -> f64 {
130130
self.meters * METER_FEET_FACTOR
131131
}
132-
132+
133133
pub fn as_yards(&self) -> f64 {
134134
self.meters * METER_YARD_FACTOR
135135
}
136-
136+
137137
pub fn as_furlongs(&self) -> f64 {
138138
self.meters * METER_FURLONG_FACTOR
139139
}
140-
140+
141141
pub fn as_miles(&self) -> f64 {
142142
self.meters * METER_MILE_FACTOR
143143
}
@@ -147,10 +147,16 @@ impl Measurement for Length {
147147
fn get_base_units(&self) -> f64 {
148148
self.meters
149149
}
150-
150+
151151
fn from_base_units(units: f64) -> Self {
152152
Self::from_meters(units)
153153
}
154154
}
155155

156156
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+
}

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ pub use weight::Weight;
1818
mod volume;
1919
pub use volume::Volume;
2020

21+
#[allow(dead_code)]
22+
mod pressure;
23+
pub use pressure::Pressure;
24+
2125
// Include when running tests, but don't export them
2226
#[cfg(test)]
2327
#[allow(dead_code)]

src/measurement.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
/// The `Measurement` trait and the `implement_measurement!` macro
22
/// provides a common way for various measurements to be implemented.
3-
///
3+
///
44
/// # Example
55
/// ```
66
/// // Importing the `implement_measurement` macro from the external crate is important
77
/// #[macro_use]
88
/// extern crate measurements;
9-
///
9+
///
1010
/// use measurements::Measurement;
11-
///
11+
///
1212
/// struct Cubits {
1313
/// forearms: f64
1414
/// }
15-
///
15+
///
1616
/// impl Measurement for Cubits {
1717
/// fn get_base_units(&self) -> f64 {
1818
/// self.forearms
1919
/// }
20-
///
20+
///
2121
/// fn from_base_units(units: f64) -> Self {
2222
/// Cubits { forearms: units }
2323
/// }
@@ -42,59 +42,59 @@ macro_rules! implement_measurement {
4242
($($t:ty)*) => ($(
4343
impl ::std::ops::Add for $t {
4444
type Output = Self;
45-
45+
4646
fn add(self, rhs: Self) -> Self {
4747
Self::from_base_units(self.get_base_units() + rhs.get_base_units())
4848
}
4949
}
5050

5151
impl ::std::ops::Sub for $t {
5252
type Output = Self;
53-
53+
5454
fn sub(self, rhs: Self) -> Self {
5555
Self::from_base_units(self.get_base_units() - rhs.get_base_units())
5656
}
5757
}
5858

59-
///
59+
///
6060
/// Dividing a `$t` by another `$` returns a ratio.
61-
///
61+
///
6262
impl ::std::ops::Div<$t> for $t {
6363
type Output = f64;
64-
64+
6565
fn div(self, rhs: Self) -> f64 {
6666
self.get_base_units() / rhs.get_base_units()
6767
}
6868
}
6969

70-
///
70+
///
7171
/// Dividing a `$` by a factor returns a new portion of the measurement.
72-
///
72+
///
7373
impl ::std::ops::Div<f64> for $t {
7474
type Output = Self;
75-
75+
7676
fn div(self, rhs: f64) -> Self {
7777
Self::from_base_units(self.get_base_units() / rhs)
7878
}
7979
}
8080

81-
///
81+
///
8282
/// Multiplying a `$t` by another `$t` returns the product of those measurements.
83-
///
83+
///
8484
impl ::std::ops::Mul<$t> for $t {
8585
type Output = Self;
86-
86+
8787
fn mul(self, rhs: Self) -> Self {
8888
Self::from_base_units(self.get_base_units() * rhs.get_base_units())
8989
}
9090
}
9191

92-
///
92+
///
9393
/// Multiplying a `$t` by a factor increases (or decreases) that measurement a number of times.
94-
///
94+
///
9595
impl ::std::ops::Mul<f64> for $t {
9696
type Output = Self;
97-
97+
9898
fn mul(self, rhs: f64) -> Self {
9999
Self::from_base_units(self.get_base_units() * rhs)
100100
}

src/pressure.rs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
use super::measurement::*;
2+
3+
/// The `Pressure` struct can be used to deal with presssures in a common way.
4+
/// Common metric and imperial units are supported.
5+
///
6+
/// # Example
7+
///
8+
/// ```
9+
/// use measurements::Pressure;
10+
///
11+
/// let earth = Pressue::from_atmospheres(1.0);
12+
/// let mbar = earth.as_mbar();
13+
/// println!("Atmospheric pressure is {} mbar.", mbar);
14+
/// ```
15+
#[derive(Copy, Clone, Debug)]
16+
pub struct Pressure {
17+
millibars: f64
18+
}
19+
20+
impl Pressure {
21+
pub fn from_hectopascals(hectopascals: f64) -> Pressure {
22+
Self::from_millibars(hectopascals)
23+
}
24+
25+
pub fn from_pascals(pascals: f64) -> Pressure {
26+
Self::from_hectopascals(pascals * 100.0)
27+
}
28+
29+
pub fn from_kilopascals(kilopascals: f64) -> Pressure {
30+
Self::from_hectopascals(kilopascals / 10.0)
31+
}
32+
33+
pub fn from_millibars(millibars: f64) -> Pressure {
34+
Pressure { millibars: millibars }
35+
}
36+
37+
pub fn from_bars(bars: f64) -> Pressure {
38+
Self::from_millibars(bars * 1000.0)
39+
}
40+
41+
pub fn from_psi(psi: f64) -> Pressure {
42+
Self::from_millibars(psi / 0.0145038)
43+
}
44+
45+
pub fn from_atmospheres(atmospheres: f64) -> Pressure {
46+
Self::from_millibars(atmospheres * 1013.25)
47+
}
48+
49+
pub fn as_hectopascals(&self) -> f64 {
50+
self.millibars
51+
}
52+
53+
pub fn as_pascals(&self) -> f64 {
54+
self.millibars * 100.0
55+
}
56+
57+
pub fn as_kilopascals(&self) -> f64 {
58+
self.millibars / 10.0
59+
}
60+
61+
pub fn as_millibars(&self) -> f64 {
62+
self.millibars
63+
}
64+
65+
pub fn as_bars(&self) -> f64 {
66+
self.millibars / 1000.0
67+
}
68+
69+
pub fn as_psi(&self) -> f64 {
70+
self.millibars * 0.0145038
71+
}
72+
73+
pub fn as_atmospheres(&self) -> f64 {
74+
self.millibars / 1013.25
75+
}
76+
}
77+
78+
impl Measurement for Pressure {
79+
fn get_base_units(&self) -> f64 {
80+
self.millibars
81+
}
82+
83+
fn from_base_units(units: f64) -> Self {
84+
Self::from_millibars(units)
85+
}
86+
}
87+
88+
implement_measurement! { Pressure }
89+
90+
impl ::std::fmt::Display for Pressure {
91+
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
92+
write!(f, "{:.1} Pa", self.as_pascals())
93+
}
94+
}

0 commit comments

Comments
 (0)