Skip to content

Commit 3eeb4f7

Browse files
committed
No-std actually works now.
Lack of float traits is a real pain!
1 parent fc4836b commit 3eeb4f7

File tree

11 files changed

+95
-66
lines changed

11 files changed

+95
-66
lines changed

Cargo.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "measurements"
3-
version = "0.9.0"
3+
version = "0.10.0"
44
authors = ["James O'Cull <[email protected]>",
55
"Jonathan Pallant <[email protected]>",
66
"Hannah McLaughlin <[email protected]>",
@@ -13,6 +13,5 @@ description = "Handle metric, imperial, and other measurements with ease! Types:
1313
license = "MIT"
1414
readme = "README.md"
1515

16-
17-
[dependencies]
18-
time = "0.1"
16+
[features]
17+
no_std = []

src/acceleration.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,15 @@ use super::length;
99
/// # Example
1010
///
1111
/// ```
12-
/// extern crate time;
1312
/// extern crate measurements;
1413
/// use measurements::{Acceleration, Length, Speed};
15-
/// use time::Duration;
14+
/// use std::time::Duration;
1615
///
1716
/// fn main() {
1817
/// // Standing quarter mile in 10.0 dead, at 120.0 mph
1918
/// let track = Length::from_miles(0.25);
2019
/// let finish = Speed::from_miles_per_hour(120.0);
21-
/// let time = Duration::seconds(10);
20+
/// let time = Duration::new(10, 0);
2221
/// let accel: Acceleration = finish / time;
2322
/// println!("You accelerated over {} at an average of {}", track, accel);
2423
///}
@@ -85,14 +84,13 @@ mod test {
8584

8685
use super::*;
8786
use test_utils::assert_almost_eq;
88-
use time::Duration;
8987
use speed::Speed;
9088

9189
// Metric
9290
#[test]
9391
fn speed_over_time() {
9492
let s1 = Speed::from_meters_per_second(10.0);
95-
let t1 = Duration::seconds(5);
93+
let t1 = ::time::Duration::new(5, 0);
9694
let i1 = s1 / t1;
9795
let r1 = i1.as_meters_per_second_per_second();
9896
assert_almost_eq(r1, 2.0);
@@ -159,5 +157,4 @@ mod test {
159157
assert_eq!(a > b, false);
160158
assert_eq!(a >= b, false);
161159
}
162-
163160
}

src/angle.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub struct Angle {
2222
impl Angle {
2323
/// Create a new Angle from a floating point value in degrees
2424
pub fn from_degrees(degrees: f64) -> Self {
25-
Angle::from_radians(degrees.to_radians())
25+
Angle::from_radians(degrees * ::PI / 180.0)
2626
}
2727

2828
/// Create a new Angle from a floating point value in radians
@@ -32,7 +32,7 @@ impl Angle {
3232

3333
/// Convert this Angle to a floating point value in degrees
3434
pub fn as_degrees(&self) -> f64 {
35-
self.radians.to_degrees()
35+
self.radians * 180.0 / ::PI
3636
}
3737

3838
/// Convert this Angle to a floating point value in radians
@@ -41,36 +41,43 @@ impl Angle {
4141
}
4242

4343
/// Calculate the cosine of this angle
44+
#[cfg(not(feature = "no_std"))]
4445
pub fn cos(&self) -> f64 {
4546
self.radians.cos()
4647
}
4748

4849
/// Calculate the sine of this angle
50+
#[cfg(not(feature = "no_std"))]
4951
pub fn sin(&self) -> f64 {
5052
self.radians.sin()
5153
}
5254

5355
/// Calculate the sine and cosine of this angle
56+
#[cfg(not(feature = "no_std"))]
5457
pub fn sin_cos(&self) -> (f64, f64) {
5558
self.radians.sin_cos()
5659
}
5760

5861
/// Calculate the tangent of this angle
62+
#[cfg(not(feature = "no_std"))]
5963
pub fn tan(&self) -> f64 {
6064
self.radians.tan()
6165
}
6266

6367
/// Calculate the arcsine of a number
68+
#[cfg(not(feature = "no_std"))]
6469
pub fn asin(num: f64) -> Self {
6570
Angle::from_radians(num.asin())
6671
}
6772

6873
/// Calculate the arccosine of a number
74+
#[cfg(not(feature = "no_std"))]
6975
pub fn acos(num: f64) -> Self {
7076
Angle::from_radians(num.acos())
7177
}
7278

7379
/// Calculate the arctangent of a number
80+
#[cfg(not(feature = "no_std"))]
7481
pub fn atan(num: f64) -> Self {
7582
Angle::from_radians(num.atan())
7683
}
@@ -95,7 +102,7 @@ implement_measurement! { Angle }
95102
#[cfg(test)]
96103
mod test {
97104
use angle::*;
98-
use core::f64::consts::PI;
105+
use std::f64::consts::PI;
99106
use test_utils::assert_almost_eq;
100107

101108
#[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 core::f64::consts::PI;
4+
use ::PI;
55

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

src/data.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,11 @@ impl Measurement for Data {
155155
let list = [
156156
("octets", 1.0),
157157
("KiB", 1024.0),
158-
("MiB", 1024.0_f64.powi(2)),
159-
("GiB", 1024.0_f64.powi(3)),
160-
("TiB", 1024.0_f64.powi(4)),
161-
("PiB", 1024.0_f64.powi(5)),
162-
("EiB", 1024.0_f64.powi(6)),
158+
("MiB", 1024.0 * 1024.0),
159+
("GiB", 1024.0 * 1024.0 * 1024.0),
160+
("TiB", 1024.0 * 1024.0 * 1024.0 * 1024.0),
161+
("PiB", 1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0),
162+
("EiB", 1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0 * 1024.0),
163163
];
164164
self.pick_appropriate_units(&list)
165165
}

src/frequency.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Types and constants for handling frequencies.
22
33
use super::measurement::*;
4+
use ::time;
45

56
/// Number of nanohertz in a Hz
67
pub const HERTZ_NANOHERTZ_FACTOR: f64 = 1e9;
@@ -78,7 +79,7 @@ impl Frequency {
7879
}
7980

8081
/// Create a new Frequency from a floating point value of the period in seconds.
81-
pub fn from_period(period: ::time::Duration) -> Self {
82+
pub fn from_period(period: time::Duration) -> Self {
8283
Self::from_hertz(1.0 / period.as_base_units())
8384
}
8485

@@ -123,8 +124,8 @@ impl Frequency {
123124
}
124125

125126
/// Convert this Frequency to a floating point value of the period in seconds.
126-
pub fn as_period(&self) -> ::time::Duration {
127-
::time::Duration::from_base_units(1.0 / self.hertz)
127+
pub fn as_period(&self) -> time::Duration {
128+
time::Duration::from_base_units(1.0 / self.hertz)
128129
}
129130
}
130131

@@ -163,6 +164,7 @@ implement_measurement! { Frequency }
163164
mod test {
164165
use super::*;
165166
use test_utils::assert_almost_eq;
167+
use ::time;
166168

167169
#[test]
168170
pub fn hertz() {
@@ -245,7 +247,7 @@ mod test {
245247
pub fn period() {
246248
let i1 = Frequency::from_hertz(100.0);
247249
let r1 = i1.as_period().as_base_units();
248-
let i2 = Frequency::from_period(::time::Duration::seconds(100));
250+
let i2 = Frequency::from_period(time::Duration::new(100, 0));
249251
let r2 = i2.as_hertz();
250252
assert_almost_eq(r1, 1e-2);
251253
assert_almost_eq(r2, 1e-2);

src/lib.rs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,18 @@
77
//! by an Area to get a Pressure.
88
99
#![deny(warnings, missing_docs)]
10-
#![no_std]
1110

12-
extern crate time;
11+
#![cfg_attr(feature="no_std", no_std)]
12+
13+
#[cfg(feature = "no_std")]
14+
use core as std;
15+
#[cfg(feature = "no_std")]
16+
use core::time as time;
17+
18+
#[cfg(not(feature = "no_std"))]
19+
use std::time as time;
20+
21+
use std::f64::consts::PI as PI;
1322

1423
#[macro_use]
1524
mod measurement;
@@ -76,6 +85,8 @@ mod torque_energy;
7685
pub use torque_energy::TorqueEnergy;
7786

7887
pub mod prelude;
88+
89+
#[cfg(test)]
7990
pub mod test_utils;
8091

8192
/// For given types A, B and C, implement, using base units:
@@ -85,15 +96,15 @@ pub mod test_utils;
8596
/// - C = A / B
8697
macro_rules! impl_maths {
8798
($a:ty, $b:ty) => {
88-
impl ::core::ops::Mul<$b> for $b {
99+
impl std::ops::Mul<$b> for $b {
89100
type Output = $a;
90101

91102
fn mul(self, rhs: $b) -> Self::Output {
92103
Self::Output::from_base_units(self.as_base_units() * rhs.as_base_units())
93104
}
94105
}
95106

96-
impl ::core::ops::Div<$b> for $a {
107+
impl std::ops::Div<$b> for $a {
97108
type Output = $b;
98109

99110
fn div(self, rhs: $b) -> Self::Output {
@@ -103,31 +114,31 @@ macro_rules! impl_maths {
103114
};
104115

105116
($a:ty, $b:ty, $c:ty) => {
106-
impl ::core::ops::Mul<$b> for $c {
117+
impl std::ops::Mul<$b> for $c {
107118
type Output = $a;
108119

109120
fn mul(self, rhs: $b) -> Self::Output {
110121
Self::Output::from_base_units(self.as_base_units() * rhs.as_base_units())
111122
}
112123
}
113124

114-
impl ::core::ops::Mul<$c> for $b {
125+
impl std::ops::Mul<$c> for $b {
115126
type Output = $a;
116127

117128
fn mul(self, rhs: $c) -> Self::Output {
118129
Self::Output::from_base_units(self.as_base_units() * rhs.as_base_units())
119130
}
120131
}
121132

122-
impl ::core::ops::Div<$c> for $a {
133+
impl std::ops::Div<$c> for $a {
123134
type Output = $b;
124135

125136
fn div(self, rhs: $c) -> Self::Output {
126137
Self::Output::from_base_units(self.as_base_units() / rhs.as_base_units())
127138
}
128139
}
129140

130-
impl ::core::ops::Div<$b> for $a {
141+
impl std::ops::Div<$b> for $a {
131142
type Output = $c;
132143

133144
fn div(self, rhs: $b) -> Self::Output {
@@ -139,11 +150,13 @@ macro_rules! impl_maths {
139150

140151
impl Measurement for time::Duration {
141152
fn as_base_units(&self) -> f64 {
142-
(self.num_microseconds().unwrap() as f64) / 1e6
153+
self.as_secs() as f64 + (f64::from(self.subsec_nanos()) * 1e-9)
143154
}
144155

145156
fn from_base_units(units: f64) -> Self {
146-
time::Duration::microseconds((units * 1e6) as i64)
157+
let subsec_nanos = ((units * 1e9) % 1e9) as u32;
158+
let secs = units as u64;
159+
time::Duration::new(secs, subsec_nanos)
147160
}
148161

149162
fn get_base_units_name(&self) -> &'static str {
@@ -171,34 +184,35 @@ impl_maths!(TorqueEnergy, Force, Length);
171184
// Implement the divisions manually (the above macro only implemented the
172185
// TorqueEnergy / X operations).
173186

174-
impl ::core::ops::Div<Length> for Torque {
187+
impl std::ops::Div<Length> for Torque {
175188
type Output = Force;
176189

177190
fn div(self, rhs: Length) -> Self::Output {
178191
Self::Output::from_base_units(self.as_base_units() / rhs.as_base_units())
179192
}
180193
}
181194

182-
impl ::core::ops::Div<Force> for Torque {
195+
impl std::ops::Div<Force> for Torque {
183196
type Output = Length;
184197

185198
fn div(self, rhs: Force) -> Self::Output {
186199
Self::Output::from_base_units(self.as_base_units() / rhs.as_base_units())
187200
}
188201
}
189202

190-
impl ::core::ops::Div<Length> for Energy {
203+
impl std::ops::Div<Length> for Energy {
191204
type Output = Force;
192205

193206
fn div(self, rhs: Length) -> Self::Output {
194207
Self::Output::from_base_units(self.as_base_units() / rhs.as_base_units())
195208
}
196209
}
197210

198-
impl ::core::ops::Div<Force> for Energy {
211+
impl std::ops::Div<Force> for Energy {
199212
type Output = Length;
200213

201214
fn div(self, rhs: Force) -> Self::Output {
202215
Self::Output::from_base_units(self.as_base_units() / rhs.as_base_units())
203216
}
204217
}
218+

0 commit comments

Comments
 (0)