Skip to content

Commit 143d92f

Browse files
author
James O'Cull
committed
Added Volume units and update docs. Bump to v0.2.1
1 parent 6e6e674 commit 143d92f

File tree

6 files changed

+518
-9
lines changed

6 files changed

+518
-9
lines changed

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
[package]
22
name = "measurements"
3-
version = "0.2.0"
3+
version = "0.2.1"
44
authors = ["James O'Cull <[email protected]>"]
55
documentation = "https://jocull.github.io/rust-measurements/"
66
repository = "https://github.com/jocull/rust-measurements"
7-
description = "Handle metric, imperial, and other measurements with ease! Types: Length, Temperature, Weight"
7+
keywords = ["units of measure", "measurements", "lengths", "weights", "temperatures", "volumes"]
8+
description = "Handle metric, imperial, and other measurements with ease! Types: Length, Temperature, Weight, Volume"
89
license = "MIT"

README.md

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,65 @@
11
[![Build Status](https://travis-ci.org/jocull/rust-measurements.svg)](https://travis-ci.org/jocull/rust-measurements)
22

3-
# And Rust said, "Let there be units".
3+
# Type-safe units of measure for Rust
44

5-
Hurray! Now you can work with units of measure in a headache-free way.
5+
### Why should I care? I already have numbers...
66

7-
Currently available units:
7+
Working with units can be very error prone.
8+
If one person is working in feet and one person is working in meters, what happens?
9+
10+
Doing all of your math in raw numerical types can be unsafe and downright confusing.
11+
What can we do to help?
12+
13+
### Typed measurements to the rescue!
14+
15+
Working in typed measurements increases safety by dealing with what you really care about: units of measure.
16+
17+
Conversions to and from different units are simple, and operator overrides allow you to work with the measurements directly.
18+
19+
### Currently available measurement types
820

921
- Length
1022
- Temperature
1123
- Weight
24+
- Volume
1225

13-
### Example
26+
### Examples
1427

15-
In your Cargo.toml...
28+
In your Cargo.toml add the dependency...
1629

1730
```
1831
[dependencies]
19-
measurements = "^0.2.0"
32+
measurements = "^0.2.1"
2033
```
2134

2235
In your code...
2336

2437
```rust
2538
extern crate measurements;
2639

27-
use measurements::{Length, Temperature, Weight};
40+
use measurements::{Length, Temperature, Weight, Volume};
2841

42+
// Lengths!
2943
let football_field = Length::from_yards(100.0);
3044
let meters = football_field.as_meters();
3145
println!("There are {} meters in a football field.", meters);
3246

47+
/// Temperatures!
3348
let boiling_water = Temperature::from_celsius(100.0);
3449
let fahrenheit = boiling_water.as_fahrenheit();
3550
println!("Boiling water measures at {} degrees fahrenheit.", fahrenheit);
3651

52+
// Weights!
3753
let metric_ton = Weight::from_metric_tons(1.0);
3854
let united_states_tons = metric_ton.as_short_tons();
3955
let united_states_pounds = metric_ton.as_pounds();
4056
println!("One metric ton is {} U.S. tons - that's {} pounds!", united_states_tons, united_states_pounds);
57+
58+
// Volumes!
59+
let gallon = Volume::from_gallons(1.0);
60+
let pint = Volume::from_pints(1.0);
61+
let beers = gallon / pint;
62+
println!("A gallon of beer will pour {} pints!", beers);
4163
```
4264

4365
--------------------------------------

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ pub use temperature::Temperature;
1414
mod weight;
1515
pub use weight::Weight;
1616

17+
#[allow(dead_code)]
18+
mod volume;
19+
pub use volume::Volume;
20+
1721
// Include when running tests, but don't export them
1822
#[cfg(test)]
1923
#[allow(dead_code)]

src/tests/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
mod length_tests;
22
mod temperature_tests;
33
mod weight_tests;
4+
mod volume_tests;
45

56
const DEFAULT_DELTA: f64 = 1.000000001;
67

src/tests/volume_tests.rs

Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
use volume::*;
2+
use super::{assert_almost_eq};
3+
4+
// Volume Units
5+
// Metric
6+
#[test]
7+
fn litres() {
8+
let t = Volume::from_litres(100.0);
9+
let o = t.as_litres();
10+
assert_almost_eq(o, 100.0);
11+
}
12+
13+
#[test]
14+
fn cubic_centimeters() {
15+
let t = Volume::from_litres(1.0);
16+
let o = t.as_cubic_centimeters();
17+
assert_almost_eq(o, 1000.0);
18+
19+
let t = Volume::from_cubic_centimeters(1000.0);
20+
let o = t.as_litres();
21+
assert_almost_eq(o, 1.0);
22+
}
23+
24+
#[test]
25+
fn milliliters() {
26+
let t = Volume::from_litres(1.0);
27+
let o = t.as_milliliters();
28+
assert_almost_eq(o, 1000.0);
29+
30+
let t = Volume::from_milliliters(1000.0);
31+
let o = t.as_litres();
32+
assert_almost_eq(o, 1.0);
33+
}
34+
35+
#[test]
36+
fn cubic_meters() {
37+
let t = Volume::from_litres(100.0);
38+
let o = t.as_cubic_meters();
39+
assert_almost_eq(o, 0.1);
40+
41+
let t = Volume::from_cubic_meters(100.0);
42+
let o = t.as_litres();
43+
assert_almost_eq(o, 100000.0);
44+
}
45+
46+
// Imperial
47+
#[test]
48+
fn drops() {
49+
let t = Volume::from_litres(100.0);
50+
let o = t.as_drops();
51+
assert_almost_eq(o, 1541962.98055);
52+
53+
let t = Volume::from_drops(100.0);
54+
let o = t.as_litres();
55+
assert_almost_eq(o, 0.00648524);
56+
}
57+
58+
#[test]
59+
fn drams() {
60+
let t = Volume::from_litres(100.0);
61+
let o = t.as_drams();
62+
assert_almost_eq(o, 27051.0351863);
63+
64+
let t = Volume::from_drams(100.0);
65+
let o = t.as_litres();
66+
assert_almost_eq(o, 0.36967162);
67+
}
68+
69+
#[test]
70+
fn teaspoons() {
71+
let t = Volume::from_litres(100.0);
72+
let o = t.as_teaspoons();
73+
assert_almost_eq(o, 20288.41362);
74+
75+
let t = Volume::from_teaspoons(100.0);
76+
let o = t.as_litres();
77+
assert_almost_eq(o, 0.492892159402);
78+
}
79+
80+
#[test]
81+
fn tablespoons() {
82+
let t = Volume::from_litres(100.0);
83+
let o = t.as_tablespoons();
84+
assert_almost_eq(o, 6762.80454);
85+
86+
let t = Volume::from_tablespoons(100.0);
87+
let o = t.as_litres();
88+
assert_almost_eq(o, 1.47867647821);
89+
}
90+
91+
#[test]
92+
fn cubic_inches() {
93+
let t = Volume::from_litres(100.0);
94+
let o = t.as_cubic_inches();
95+
assert_almost_eq(o, 6102.37440947);
96+
97+
let t = Volume::from_cubic_inches(100.0);
98+
let o = t.as_litres();
99+
assert_almost_eq(o, 1.6387064);
100+
}
101+
102+
#[test]
103+
fn fluid_ounces_uk() {
104+
let t = Volume::from_litres(100.0);
105+
let o = t.as_fluid_ounces_uk();
106+
assert_almost_eq(o, 3519.506424);
107+
108+
let t = Volume::from_fluid_ounces_uk(100.0);
109+
let o = t.as_litres();
110+
assert_almost_eq(o, 2.84130750034);
111+
}
112+
113+
#[test]
114+
fn fluid_ounces() {
115+
let t = Volume::from_litres(100.0);
116+
let o = t.as_fluid_ounces();
117+
assert_almost_eq(o, 3381.40227);
118+
119+
let t = Volume::from_fluid_ounces(100.0);
120+
let o = t.as_litres();
121+
assert_almost_eq(o, 2.95735295641);
122+
}
123+
124+
#[test]
125+
fn cups() {
126+
let t = Volume::from_litres(100.0);
127+
let o = t.as_cups();
128+
assert_almost_eq(o, 422.6752838);
129+
130+
let t = Volume::from_cups(100.0);
131+
let o = t.as_litres();
132+
assert_almost_eq(o, 23.6588236485);
133+
}
134+
135+
#[test]
136+
fn pints() {
137+
let t = Volume::from_litres(100.0);
138+
let o = t.as_pints();
139+
assert_almost_eq(o, 211.337641887);
140+
141+
let t = Volume::from_pints(100.0);
142+
let o = t.as_litres();
143+
assert_almost_eq(o, 47.3176473);
144+
}
145+
146+
#[test]
147+
fn pints_uk() {
148+
let t = Volume::from_litres(100.0);
149+
let o = t.as_pints_uk();
150+
assert_almost_eq(o, 175.975398639);
151+
152+
let t = Volume::from_pints_uk(100.0);
153+
let o = t.as_litres();
154+
assert_almost_eq(o, 56.826125);
155+
}
156+
157+
#[test]
158+
fn quarts() {
159+
let t = Volume::from_litres(100.0);
160+
let o = t.as_quarts();
161+
assert_almost_eq(o, 105.668820943);
162+
163+
let t = Volume::from_quarts(100.0);
164+
let o = t.as_litres();
165+
assert_almost_eq(o, 94.6352946);
166+
}
167+
168+
#[test]
169+
fn gallons() {
170+
let t = Volume::from_litres(100.0);
171+
let o = t.as_gallons();
172+
assert_almost_eq(o, 26.4172052358);
173+
174+
let t = Volume::from_gallons(100.0);
175+
let o = t.as_litres();
176+
assert_almost_eq(o, 378.5411784);
177+
}
178+
179+
#[test]
180+
fn gallons_uk() {
181+
let t = Volume::from_litres(100.0);
182+
let o = t.as_gallons_uk();
183+
assert_almost_eq(o, 21.9969248299);
184+
185+
let t = Volume::from_gallons_uk(100.0);
186+
let o = t.as_litres();
187+
assert_almost_eq(o, 454.609);
188+
}
189+
190+
#[test]
191+
fn cubic_feet() {
192+
let t = Volume::from_litres(100.0);
193+
let o = t.as_cubic_feet();
194+
assert_almost_eq(o, 3.53146667215);
195+
196+
let t = Volume::from_cubic_feet(100.0);
197+
let o = t.as_litres();
198+
assert_almost_eq(o, 2831.6846592);
199+
}
200+
201+
#[test]
202+
fn cubic_yards() {
203+
let t = Volume::from_litres(100.0);
204+
let o = t.as_cubic_yards();
205+
assert_almost_eq(o, 0.13079506193);
206+
207+
let t = Volume::from_cubic_yards(100.0);
208+
let o = t.as_litres();
209+
assert_almost_eq(o, 76455.4857992);
210+
}
211+
212+
// Traits
213+
#[test]
214+
fn add() {
215+
let a = Volume::from_litres(2.0);
216+
let b = Volume::from_litres(4.0);
217+
let c = a + b;
218+
assert_almost_eq(c.as_litres(), 6.0);
219+
}
220+
221+
#[test]
222+
fn sub() {
223+
let a = Volume::from_litres(2.0);
224+
let b = Volume::from_litres(4.0);
225+
let c = a - b;
226+
assert_almost_eq(c.as_litres(), -2.0);
227+
}
228+
229+
#[test]
230+
fn mul() {
231+
let a = Volume::from_litres(2.0);
232+
let b = Volume::from_litres(4.0);
233+
let c = a * b;
234+
let d = a * 2.0;
235+
assert_almost_eq(c.as_litres(), 8.0);
236+
assert_almost_eq(d.as_litres(), 4.0);
237+
}
238+
239+
#[test]
240+
fn div() {
241+
let a = Volume::from_litres(2.0);
242+
let b = Volume::from_litres(4.0);
243+
let c = a / b;
244+
let d = a / 2.0;
245+
assert_almost_eq(c, 0.5);
246+
assert_almost_eq(d.as_litres(), 1.0);
247+
}
248+
249+
#[test]
250+
fn eq() {
251+
let a = Volume::from_litres(2.0);
252+
let b = Volume::from_litres(2.0);
253+
assert_eq!(a == b, true);
254+
}
255+
256+
#[test]
257+
fn neq() {
258+
let a = Volume::from_litres(2.0);
259+
let b = Volume::from_litres(4.0);
260+
assert_eq!(a == b, false);
261+
}
262+
263+
#[test]
264+
fn cmp() {
265+
let a = Volume::from_litres(2.0);
266+
let b = Volume::from_litres(4.0);
267+
assert_eq!(a < b, true);
268+
assert_eq!(a <= b, true);
269+
assert_eq!(a > b, false);
270+
assert_eq!(a >= b, false);
271+
}

0 commit comments

Comments
 (0)