Skip to content

Commit 5abf3b7

Browse files
committed
Make no_std.
1 parent 237234e commit 5abf3b7

File tree

12 files changed

+74
-64
lines changed

12 files changed

+74
-64
lines changed

Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "measurements"
3-
version = "0.6.0"
3+
version = "0.7.0"
44
authors = ["James O'Cull <[email protected]>",
55
"Jonathan Pallant <[email protected]>",
66
"Hannah McLaughlin <[email protected]>"]
@@ -10,3 +10,7 @@ keywords = ["measurements", "lengths", "weights", "temperatures", "volumes"]
1010
description = "Handle metric, imperial, and other measurements with ease! Types: Length, Temperature, Weight, Volume, Pressure"
1111
license = "MIT"
1212
readme = "README.md"
13+
14+
15+
[dependencies]
16+
time = "0.1"

examples/frequency.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ fn main() {
66
let d = measurements::Distance::from_millimeters(50.0);
77
// Speed = PI * Frequency * Displacement
88
// Speed = PI * Displacement / Period
9-
let v = ::std::f64::consts::PI * d / f.as_period();
9+
let v = std::f64::consts::PI * d / f.as_period();
1010
println!(
1111
"Maximum speed of a pendulum at {:.1} with max displacement {:.1} is {:.1}",
1212
f,

src/acceleration.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@ use super::length;
99
/// # Example
1010
///
1111
/// ```
12+
/// extern crate time;
13+
/// extern crate measurements;
1214
/// use measurements::{Acceleration, Length, Speed};
13-
/// use std::time::Duration;
15+
/// use time::Duration;
1416
///
15-
/// // Standing quarter mile in 10.0 dead, at 120.0 mph
16-
/// let track = Length::from_miles(0.25);
17-
/// let finish = Speed::from_miles_per_hour(120.0);
18-
/// let time = Duration::new(10, 0);
19-
/// let accel: Acceleration = finish / time;
20-
/// println!("You accelerated over {} at an average of {}", track, accel);
17+
/// fn main() {
18+
/// // Standing quarter mile in 10.0 dead, at 120.0 mph
19+
/// let track = Length::from_miles(0.25);
20+
/// let finish = Speed::from_miles_per_hour(120.0);
21+
/// let time = Duration::seconds(10);
22+
/// let accel: Acceleration = finish / time;
23+
/// println!("You accelerated over {} at an average of {}", track, accel);
24+
///}
2125
/// ```
2226
#[derive(Copy, Clone, Debug)]
2327
pub struct Acceleration {
@@ -79,14 +83,14 @@ mod test {
7983

8084
use super::*;
8185
use test_utils::assert_almost_eq;
82-
use std::time::Duration;
86+
use time::Duration;
8387
use speed::Speed;
8488

8589
// Metric
8690
#[test]
8791
fn speed_over_time() {
8892
let s1 = Speed::from_meters_per_second(10.0);
89-
let t1 = Duration::new(5, 0);
93+
let t1 = Duration::seconds(5);
9094
let i1 = s1 / t1;
9195
let r1 = i1.as_meters_per_second_per_second();
9296
assert_almost_eq(r1, 2.0);

src/angle.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ implement_measurement! { Angle }
9595
#[cfg(test)]
9696
mod test {
9797
use angle::*;
98-
use std::f64::consts::PI;
98+
use core::f64::consts::PI;
9999
use test_utils::assert_almost_eq;
100100

101101
#[test]

src/angular_velocity.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Types and constants for handling speed of rotation (angular velocity)
22
33
use super::measurement::*;
4-
use std::f64::consts::PI;
4+
use core::f64::consts::PI;
55

66
/// The 'AngularVelocity' struct can be used to deal with angular velocities in a common way.
77
///

src/frequency.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl Frequency {
7878
}
7979

8080
/// Create a new Frequency from a floating point value of the period in seconds.
81-
pub fn from_period(period: ::std::time::Duration) -> Self {
81+
pub fn from_period(period: ::time::Duration) -> Self {
8282
Self::from_hertz(1.0 / period.as_base_units())
8383
}
8484

@@ -123,8 +123,8 @@ impl Frequency {
123123
}
124124

125125
/// Convert this Frequency to a floating point value of the period in seconds.
126-
pub fn as_period(&self) -> ::std::time::Duration {
127-
::std::time::Duration::from_base_units(1.0 / self.hertz)
126+
pub fn as_period(&self) -> ::time::Duration {
127+
::time::Duration::from_base_units(1.0 / self.hertz)
128128
}
129129
}
130130

@@ -245,7 +245,7 @@ mod test {
245245
pub fn period() {
246246
let i1 = Frequency::from_hertz(100.0);
247247
let r1 = i1.as_period().as_base_units();
248-
let i2 = Frequency::from_period(::std::time::Duration::new(100, 0));
248+
let i2 = Frequency::from_period(::time::Duration::seconds(100));
249249
let r2 = i2.as_hertz();
250250
assert_almost_eq(r1, 1e-2);
251251
assert_almost_eq(r2, 1e-2);

src/lib.rs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
//! by an Area to get a Pressure.
88
99
#![deny(warnings, missing_docs)]
10+
#![no_std]
11+
12+
extern crate time;
1013

1114
#[macro_use]
1215
mod measurement;
@@ -73,15 +76,15 @@ pub mod test_utils;
7376
/// - C = A / B
7477
macro_rules! impl_maths {
7578
($a:ty, $b:ty) => {
76-
impl ::std::ops::Mul<$b> for $b {
79+
impl ::core::ops::Mul<$b> for $b {
7780
type Output = $a;
7881

7982
fn mul(self, rhs: $b) -> Self::Output {
8083
Self::Output::from_base_units(self.as_base_units() * rhs.as_base_units())
8184
}
8285
}
8386

84-
impl ::std::ops::Div<$b> for $a {
87+
impl ::core::ops::Div<$b> for $a {
8588
type Output = $b;
8689

8790
fn div(self, rhs: $b) -> Self::Output {
@@ -91,31 +94,31 @@ macro_rules! impl_maths {
9194
};
9295

9396
($a:ty, $b:ty, $c:ty) => {
94-
impl ::std::ops::Mul<$b> for $c {
97+
impl ::core::ops::Mul<$b> for $c {
9598
type Output = $a;
9699

97100
fn mul(self, rhs: $b) -> Self::Output {
98101
Self::Output::from_base_units(self.as_base_units() * rhs.as_base_units())
99102
}
100103
}
101104

102-
impl ::std::ops::Mul<$c> for $b {
105+
impl ::core::ops::Mul<$c> for $b {
103106
type Output = $a;
104107

105108
fn mul(self, rhs: $c) -> Self::Output {
106109
Self::Output::from_base_units(self.as_base_units() * rhs.as_base_units())
107110
}
108111
}
109112

110-
impl ::std::ops::Div<$c> for $a {
113+
impl ::core::ops::Div<$c> for $a {
111114
type Output = $b;
112115

113116
fn div(self, rhs: $c) -> Self::Output {
114117
Self::Output::from_base_units(self.as_base_units() / rhs.as_base_units())
115118
}
116119
}
117120

118-
impl ::std::ops::Div<$b> for $a {
121+
impl ::core::ops::Div<$b> for $a {
119122
type Output = $c;
120123

121124
fn div(self, rhs: $b) -> Self::Output {
@@ -125,15 +128,13 @@ macro_rules! impl_maths {
125128
}
126129
}
127130

128-
impl Measurement for std::time::Duration {
131+
impl Measurement for time::Duration {
129132
fn as_base_units(&self) -> f64 {
130-
self.as_secs() as f64 + (f64::from(self.subsec_nanos()) * 1e-9)
133+
(self.num_microseconds().unwrap() as f64) / 1e6
131134
}
132135

133136
fn from_base_units(units: f64) -> Self {
134-
let subsec_nanos = ((units * 1e9) % 1e9) as u32;
135-
let secs = units as u64;
136-
std::time::Duration::new(secs, subsec_nanos)
137+
time::Duration::microseconds((units * 1e6) as i64)
137138
}
138139

139140
fn get_base_units_name(&self) -> &'static str {
@@ -142,12 +143,12 @@ impl Measurement for std::time::Duration {
142143
}
143144

144145
impl_maths!(Area, Length);
145-
impl_maths!(Energy, std::time::Duration, Power);
146+
impl_maths!(Energy, time::Duration, Power);
146147
impl_maths!(Force, Mass, Acceleration);
147148
impl_maths!(Force, Pressure, Area);
148-
impl_maths!(Length, std::time::Duration, Speed);
149+
impl_maths!(Length, time::Duration, Speed);
149150
impl_maths!(Power, Force, Speed);
150-
impl_maths!(Speed, std::time::Duration, Acceleration);
151+
impl_maths!(Speed, time::Duration, Acceleration);
151152
impl_maths!(Volume, Length, Area);
152153
impl_maths!(Power, AngularVelocity, Torque);
153154

@@ -159,31 +160,31 @@ impl_maths!(TorqueEnergy, Force, Length);
159160
// Implement the divisions manually (the above macro only implemented the
160161
// TorqueEnergy / X operations).
161162

162-
impl ::std::ops::Div<Length> for Torque {
163+
impl ::core::ops::Div<Length> for Torque {
163164
type Output = Force;
164165

165166
fn div(self, rhs: Length) -> Self::Output {
166167
Self::Output::from_base_units(self.as_base_units() / rhs.as_base_units())
167168
}
168169
}
169170

170-
impl ::std::ops::Div<Force> for Torque {
171+
impl ::core::ops::Div<Force> for Torque {
171172
type Output = Length;
172173

173174
fn div(self, rhs: Force) -> Self::Output {
174175
Self::Output::from_base_units(self.as_base_units() / rhs.as_base_units())
175176
}
176177
}
177178

178-
impl ::std::ops::Div<Length> for Energy {
179+
impl ::core::ops::Div<Length> for Energy {
179180
type Output = Force;
180181

181182
fn div(self, rhs: Length) -> Self::Output {
182183
Self::Output::from_base_units(self.as_base_units() / rhs.as_base_units())
183184
}
184185
}
185186

186-
impl ::std::ops::Div<Force> for Energy {
187+
impl ::core::ops::Div<Force> for Energy {
187188
type Output = Length;
188189

189190
fn div(self, rhs: Force) -> Self::Output {

src/measurement.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
///
44
/// # Example
55
/// ```
6+
/// #![no_std]
67
/// // Importing the `implement_measurement` macro from the external crate is important
78
/// #[macro_use]
89
/// extern crate measurements;
@@ -73,13 +74,13 @@ pub trait Measurement {
7374
}
7475

7576
/// This is a special macro that creates the code to implement
76-
/// `std::fmt::Display`.
77+
/// `core::fmt::Display`.
7778
#[macro_export]
7879
macro_rules! implement_display {
7980
($($t:ty)*) => ($(
8081

81-
impl ::std::fmt::Display for $t {
82-
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
82+
impl ::core::fmt::Display for $t {
83+
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8384
let (unit, value) = self.get_appropriate_units();
8485
value.fmt(f)?; // Value
8586
write!(f, "\u{00A0}{}", unit)
@@ -97,15 +98,15 @@ macro_rules! implement_measurement {
9798

9899
implement_display!( $t );
99100

100-
impl ::std::ops::Add for $t {
101+
impl ::core::ops::Add for $t {
101102
type Output = Self;
102103

103104
fn add(self, rhs: Self) -> Self {
104105
Self::from_base_units(self.as_base_units() + rhs.as_base_units())
105106
}
106107
}
107108

108-
impl ::std::ops::Sub for $t {
109+
impl ::core::ops::Sub for $t {
109110
type Output = Self;
110111

111112
fn sub(self, rhs: Self) -> Self {
@@ -115,7 +116,7 @@ macro_rules! implement_measurement {
115116

116117
// Dividing a `$t` by another `$t` returns a ratio.
117118
//
118-
impl ::std::ops::Div<$t> for $t {
119+
impl ::core::ops::Div<$t> for $t {
119120
type Output = f64;
120121

121122
fn div(self, rhs: Self) -> f64 {
@@ -125,7 +126,7 @@ macro_rules! implement_measurement {
125126

126127
// Dividing a `$t` by a factor returns a new portion of the measurement.
127128
//
128-
impl ::std::ops::Div<f64> for $t {
129+
impl ::core::ops::Div<f64> for $t {
129130
type Output = Self;
130131

131132
fn div(self, rhs: f64) -> Self {
@@ -135,7 +136,7 @@ macro_rules! implement_measurement {
135136

136137
// Multiplying a `$t` by a factor increases (or decreases) that
137138
// measurement a number of times.
138-
impl ::std::ops::Mul<f64> for $t {
139+
impl ::core::ops::Mul<f64> for $t {
139140
type Output = Self;
140141

141142
fn mul(self, rhs: f64) -> Self {
@@ -144,23 +145,23 @@ macro_rules! implement_measurement {
144145
}
145146

146147
// Multiplying `$t` by a factor is commutative
147-
impl ::std::ops::Mul<$t> for f64 {
148+
impl ::core::ops::Mul<$t> for f64 {
148149
type Output = $t;
149150

150151
fn mul(self, rhs: $t) -> $t {
151152
rhs * self
152153
}
153154
}
154155

155-
impl ::std::cmp::Eq for $t { }
156-
impl ::std::cmp::PartialEq for $t {
156+
impl ::core::cmp::Eq for $t { }
157+
impl ::core::cmp::PartialEq for $t {
157158
fn eq(&self, other: &Self) -> bool {
158159
self.as_base_units() == other.as_base_units()
159160
}
160161
}
161162

162-
impl ::std::cmp::PartialOrd for $t {
163-
fn partial_cmp(&self, other: &Self) -> Option<::std::cmp::Ordering> {
163+
impl ::core::cmp::PartialOrd for $t {
164+
fn partial_cmp(&self, other: &Self) -> Option<::core::cmp::Ordering> {
164165
self.as_base_units().partial_cmp(&other.as_base_units())
165166
}
166167
}

src/speed.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ implement_measurement! { Speed }
116116
mod test {
117117
use speed::*;
118118
use test_utils::assert_almost_eq;
119-
use std::time::Duration;
119+
use time::Duration;
120120
use length::Length;
121121

122122
// Metric
@@ -135,7 +135,7 @@ mod test {
135135
#[test]
136136
fn length_over_time() {
137137
let l1 = Length::from_meters(3.0);
138-
let t1 = Duration::new(1, 500_000_000);
138+
let t1 = Duration::milliseconds(1500);
139139
let i1 = l1 / t1;
140140
let r1 = i1.as_meters_per_second();
141141
assert_almost_eq(r1, 2.0);
@@ -144,10 +144,10 @@ mod test {
144144
#[test]
145145
fn acceleration() {
146146
// To get to 100 m/s at 50 m/s/s takes 2.0 seconds
147-
let s = Length::from_meters(100.0) / Duration::new(1, 0);
148-
let a = Length::from_meters(50.0) / Duration::new(1, 0) / Duration::new(1, 0);
147+
let s = Length::from_meters(100.0) / Duration::seconds(1);
148+
let a = Length::from_meters(50.0) / Duration::seconds(1) / Duration::seconds(1);
149149
let t = s / a;
150-
assert_eq!(t, Duration::new(2, 0));
150+
assert_eq!(t, Duration::seconds(2));
151151
}
152152

153153
#[test]

0 commit comments

Comments
 (0)