Skip to content

Commit

Permalink
Replace hard-coded storage types (f32, f64) with Num.
Browse files Browse the repository at this point in the history
Part of #29.
  • Loading branch information
iliekturtles committed Oct 9, 2017
1 parent 2a0fd01 commit aceab38
Show file tree
Hide file tree
Showing 11 changed files with 505 additions and 537 deletions.
10 changes: 6 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
* Add missing `#[derive(Hash)]` attributes.

### Change
* [#29](https://github.com/iliekturtles/uom/issues/29) Add additional Cargo features to control the
availability of underlying storage types: `usize`, `u8`, `u16`, `u32`, `u64`, `u128` (unstable),
`isize`, `i8`, `i16`, `i32`, `i64`, `i128` (unstable), `bigint`, `biguint`, `rational`,
`rational32`, `rational64`, `bigrational`, `f32`, and `f64`.
* [#29](https://github.com/iliekturtles/uom/issues/29) Underlying storage type now uses the `Num`
trait from the [`num`](https://crates.io/crates/num) crate instead of fixed implementations for
`f32` and `f64`. Features for all types implementing `Num` have been added and control the
availability of the type as an underlying storage type: `usize`, `u8`, `u16`, `u32`, `u64`,
`u128` (unstable), `isize`, `i8`, `i16`, `i32`, `i64`, `i128` (unstable), `bigint`, `biguint`,
`rational`, `rational32`, `rational64`, `bigrational`, `f32`, and `f64`.
* [Breaking] Macro usage and definitions have been simplified and consolidated. `quantities!`,
`replace_ty!`, and `unit!` have been consolidated as "private" match arms of their calling macro.
In order to reduce the chance of macro name collisions `$quantities!` is the only remaining
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ travis-ci = { repository = "iliekturtles/uom" }
coveralls = { repository = "iliekturtles/uom" }

[dependencies]
typenum = "1.6.0"
num = "0.1"
typenum = "1.9.0"
clippy = { version = "^0", optional = true }
compiletest_rs = { version = "^0", optional = true }

Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@
compile_error!("A least one underlying storage type must be enabled. See the features section of \
uom documentation for available underlying storage type options.");

#[doc(hidden)]
pub extern crate num;

#[doc(hidden)]
pub extern crate typenum;

Expand Down
3 changes: 2 additions & 1 deletion src/si/area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ macro_rules! test {

// TODO #17 Convert to == once PartialEq is implemented.
fn test<L: l::Unit<$V>, A: a::Unit<$V>>(_l: L, a: A) {
assert_eq!(1.0, (Length::new::<L>(1.0) * Length::new::<L>(1.0)).get(a));
ulps_eq!(1.0, (Length::new::<L>(1.0) * Length::new::<L>(1.0)).get(a),
epsilon = ::tests::$V::EPSILON);
}
}
};
Expand Down
8 changes: 4 additions & 4 deletions src/si/frequency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ macro_rules! test {

#[test]
fn check_dimension() {
let _: Time = 1.0 / Frequency::new::<f::hertz>(1.0);
let _: Frequency = 1.0 / Time::new::<t::second>(1.0);
let _: Time = Frequency::new::<f::hertz>(1.0).recip();
let _: Frequency = Time::new::<t::second>(1.0).recip();
}

#[test]
Expand Down Expand Up @@ -77,9 +77,9 @@ macro_rules! test {

// TODO #17 Convert to == once PartialEq is implemented.
fn test<T: t::Unit<$V> + Copy, F: f::Unit<$V> + Copy>(t: T, f: F) {
ulps_eq!((1.0 / Time::new::<T>(1.0)).get(f), Frequency::new::<F>(1.0).get(f),
ulps_eq!((Time::new::<T>(1.0).recip()).get(f), Frequency::new::<F>(1.0).get(f),
epsilon = ::tests::$V::EPSILON);
ulps_eq!(Time::new::<T>(1.0).get(t), (1.0 / Frequency::new::<F>(1.0)).get(t),
ulps_eq!(Time::new::<T>(1.0).get(t), (Frequency::new::<F>(1.0).recip()).get(t),
epsilon = ::tests::$V::EPSILON);
}
}
Expand Down
28 changes: 15 additions & 13 deletions src/si/length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,30 +60,32 @@ quantity! {
@rod: 5.029_21_E0; "rd", "rod", "rods";
@yard: 9.144_E-1; "yd", "yard", "yards";
}
impl {
/// Calculates the length of the hypotenuse of a right-angle triangle given the legs.
#[cfg(feature = "std")]
#[inline(always)]
pub fn hypot(self, other: Self) -> Self {
Length {
dimension: $crate::stdlib::marker::PhantomData,
units: $crate::stdlib::marker::PhantomData,
value: self.value.hypot(other.value)
}
}

impl<U, V> Length<U, V>
where
U: super::Units<Dimension, V>,
V: ::num::Float,
{
/// Calculates the length of the hypotenuse of a right-angle triangle given the legs.
#[cfg_attr(feature = "clippy", allow(inline_always))]
#[inline(always)]
pub fn hypot(self, other: Self) -> Self {
Length {
dimension: ::stdlib::marker::PhantomData,
units: ::stdlib::marker::PhantomData,
value: self.value.hypot(other.value)
}
}
}

#[cfg(test)]
macro_rules! test {
($V:ident) => {
#[cfg(feature = "std")]
use ::si::$V::*;
#[cfg(feature = "std")]
use ::si::length::meter;

quickcheck! {
#[cfg(feature = "std")]
#[allow(trivial_casts)]
fn hypot(l: $V, r: $V) -> bool {
l.hypot(r) == Length::new::<meter>(l).hypot(Length::new::<meter>(r)).get(meter)
Expand Down
5 changes: 3 additions & 2 deletions src/si/volume.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,11 @@ macro_rules! test {

// TODO #17 Convert to == once PartialEq is implemented.
fn test<L: l::Unit<$V>, V: v::Unit<$V>>(_l: L, v: V) {
assert_eq!(1.0,
ulps_eq!(1.0,
(Length::new::<L>(1.0)
* Length::new::<L>(1.0)
* Length::new::<L>(1.0)).get(v));
* Length::new::<L>(1.0)).get(v),
epsilon = ::tests::$V::EPSILON);
}
}
};
Expand Down
Loading

0 comments on commit aceab38

Please sign in to comment.