Skip to content

Commit b979c75

Browse files
author
James O'Cull
committed
Rearranged library layout to be cleaner. Updated docs and tests.
1 parent 0907cc6 commit b979c75

File tree

6 files changed

+74
-74
lines changed

6 files changed

+74
-74
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ In your code...
2424
```rust
2525
extern crate measurements;
2626

27-
use measurements::length::Length;
28-
use measurements::temperature::Temperature;
29-
use measurements::weight::Weight;
27+
use measurements::{Length, Temperature, Weight};
3028

3129
let football_field = Length::from_yards(100.0);
3230
let meters = football_field.as_meters();

src/length.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ 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+
///
22+
/// # Example
23+
///
24+
/// ```
25+
/// use measurements::Length;
26+
///
27+
/// let football_field = Length::from_yards(100.0);
28+
/// let meters = football_field.as_meters();
29+
/// println!("There are {} meters in a football field.", meters);
30+
/// ```
2131
#[derive(Copy, Clone, Debug)]
2232
pub struct Length {
2333
meters: f64

src/lib.rs

Lines changed: 8 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,18 @@
1-
/// The `Measurement` trait and the `implement_measurement!` macro
2-
/// provides a common way for various measurements to be implemented.
3-
///
4-
/// # Example
5-
/// ```
6-
/// // Importing the `implement_measurement` macro from the external crate is important
7-
/// #[macro_use]
8-
/// extern crate measurements;
9-
///
10-
/// use measurements::measurement::*;
11-
///
12-
/// struct Cubits {
13-
/// forearms: f64
14-
/// }
15-
///
16-
/// impl Measurement for Cubits {
17-
/// fn get_base_units(&self) -> f64 {
18-
/// self.forearms
19-
/// }
20-
///
21-
/// fn from_base_units(units: f64) -> Self {
22-
/// Cubits { forearms: units }
23-
/// }
24-
/// }
25-
///
26-
/// // Invoke the macro to automatically implement Add, Sub, etc...
27-
/// implement_measurement! { Cubits }
28-
///
29-
/// // The main function here is only included to make doc tests compile.
30-
/// // You should't need it in your own code.
31-
/// fn main() { }
32-
/// ```
331
#[macro_use]
34-
pub mod measurement;
2+
mod measurement;
3+
pub use measurement::Measurement;
354

36-
/// The `Length` struct can be used to deal with lengths in a common way.
37-
/// Common metric and imperial units are supported.
38-
///
39-
/// # Example
40-
///
41-
/// ```
42-
/// use measurements::length::Length;
43-
///
44-
/// let football_field = Length::from_yards(100.0);
45-
/// let meters = football_field.as_meters();
46-
/// println!("There are {} meters in a football field.", meters);
47-
/// ```
485
#[allow(dead_code)]
49-
pub mod length;
6+
mod length;
7+
pub use length::Length;
508

51-
/// The `Temperature` struct can be used to deal with temperatures in a common way.
52-
///
53-
/// # Example
54-
///
55-
/// ```
56-
/// use measurements::temperature::Temperature;
57-
///
58-
/// let boiling_water = Temperature::from_celsius(100.0);
59-
/// let fahrenheit = boiling_water.as_fahrenheit();
60-
/// println!("Boiling water measures at {} degrees fahrenheit.", fahrenheit);
61-
/// ```
629
#[allow(dead_code)]
63-
pub mod temperature;
10+
mod temperature;
11+
pub use temperature::Temperature;
6412

65-
/// The `Weight` struct can be used to deal with weights in a common way.
66-
///
67-
/// #Example
68-
///
69-
/// ```
70-
/// use measurements::weight::Weight;
71-
///
72-
/// let metric_ton = Weight::from_metric_tons(1.0);
73-
/// let united_states_tons = metric_ton.as_short_tons();
74-
/// let united_states_pounds = metric_ton.as_pounds();
75-
/// println!("One metric ton is {} U.S. tons - that's {} pounds!", united_states_tons, united_states_pounds);
76-
/// ```
7713
#[allow(dead_code)]
78-
pub mod weight;
14+
mod weight;
15+
pub use weight::Weight;
7916

8017
// Include when running tests, but don't export them
8118
#[cfg(test)]

src/measurement.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,42 @@
1+
/// The `Measurement` trait and the `implement_measurement!` macro
2+
/// provides a common way for various measurements to be implemented.
3+
///
4+
/// # Example
5+
/// ```
6+
/// // Importing the `implement_measurement` macro from the external crate is important
7+
/// #[macro_use]
8+
/// extern crate measurements;
9+
///
10+
/// use measurements::Measurement;
11+
///
12+
/// struct Cubits {
13+
/// forearms: f64
14+
/// }
15+
///
16+
/// impl Measurement for Cubits {
17+
/// fn get_base_units(&self) -> f64 {
18+
/// self.forearms
19+
/// }
20+
///
21+
/// fn from_base_units(units: f64) -> Self {
22+
/// Cubits { forearms: units }
23+
/// }
24+
/// }
25+
///
26+
/// // Invoke the macro to automatically implement Add, Sub, etc...
27+
/// implement_measurement! { Cubits }
28+
///
29+
/// // The main function here is only included to make doc tests compile.
30+
/// // You should't need it in your own code.
31+
/// fn main() { }
32+
/// ```
133
pub trait Measurement {
234
fn get_base_units(&self) -> f64;
335
fn from_base_units(units: f64) -> Self;
436
}
537

38+
/// This is a special macro that creates the code to implement
39+
/// operator and comparison overrides.
640
#[macro_export]
741
macro_rules! implement_measurement {
842
($($t:ty)*) => ($(

src/temperature.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
use super::measurement::*;
22

33
/// The `Temperature` struct can be used to deal with temperatures in a common way.
4+
///
5+
/// # Example
6+
///
7+
/// ```
8+
/// use measurements::Temperature;
9+
///
10+
/// let boiling_water = Temperature::from_celsius(100.0);
11+
/// let fahrenheit = boiling_water.as_fahrenheit();
12+
/// println!("Boiling water measures at {} degrees fahrenheit.", fahrenheit);
13+
/// ```
414
#[derive(Copy, Clone, Debug)]
515
pub struct Temperature {
616
kelvin: f64

src/weight.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
use super::measurement::*;
22

33
/// The `Weight` struct can be used to deal with weights in a common way.
4+
///
5+
/// #Example
6+
///
7+
/// ```
8+
/// use measurements::Weight;
9+
///
10+
/// let metric_ton = Weight::from_metric_tons(1.0);
11+
/// let united_states_tons = metric_ton.as_short_tons();
12+
/// let united_states_pounds = metric_ton.as_pounds();
13+
/// println!("One metric ton is {} U.S. tons - that's {} pounds!", united_states_tons, united_states_pounds);
14+
/// ```
415
#[derive(Copy, Clone, Debug)]
516
pub struct Weight {
617
kilograms: f64

0 commit comments

Comments
 (0)