Skip to content

Commit

Permalink
Make new function const (#80)
Browse files Browse the repository at this point in the history
We also bump to edition 2021, and fixup tests.
  • Loading branch information
paholg authored Apr 29, 2022
1 parent 2eed90f commit 7863686
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 69 deletions.
11 changes: 6 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
This project follows semantic versioning.

### Unpublished

### 0.8.0 (2022-04-28)
- [fixed] A compilation error with the `rand` feature.
- [added] The optional `auto-args` feature to derive argument parsing.
- [added] The static method `to_string`.

### 0.8.0 (2019-03-13)
- [changed] ***BREAKING*** The `new` method is now const. The `_marker` field is
now private.
- [changed] ***BREAKING*** Reduced requirement of nightly for use in `no_std`
(as introduced in 0.7.0) down to just unit system conversions and
`Sqrt`/`Root` traits. Everything else now works with `no_std` on stable!
Expand All @@ -17,9 +19,8 @@ This project follows semantic versioning.
before on stable now are working, but is at least a break in the intended way
to use `dimensioned` without `std`. Again, once `sqrt`, `powf`, and `cbrt` are
usable without std on stable, the nightly requirement can be removed.
- [changed] ***BREAKING*** Updated dependency `rand` from "0.5.4" to "0.6.5".
- [changed] ***BREAKING*** Updated dependency `quickcheck` from "0.6.2" to "0.8.2".
- [changed] ***BREAKING*** Updated dependency `generic-array` from "0.11.0" to "0.12.0".
- [changed] ***BREAKING*** Updated dependency `rand` from "0.5.4" to "0.8.5".
- [changed] ***BREAKING*** Updated dependency `generic-array` from "0.11.0" to "0.14.0".

### 0.7.0 (2018-08-12)
- [changed] ***BREAKING*** Made dimensioned work with `no_std` again, and added the default feature
Expand Down
14 changes: 8 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "dimensioned"
version = "0.8.0"
edition = "2018"
edition = "2021"
authors = ["Paho Lurie-Gregg <[email protected]>"]
documentation = "https://docs.rs/dimensioned"
repository = "https://github.com/paholg/dimensioned"
Expand All @@ -27,16 +27,18 @@ Never again should you need to specify units in a comment!"""
spec = []
std = []
nightly = []
test = [ "approx", "clapme", "quickcheck", "serde", "serde_test", "rand"]
test = [ "approx", "clapme", "serde", "serde_test", "rand"]

[dependencies]
approx = { version = "0.3.0", optional = true, default-features = false }
approx = { version = "0.5.1", optional = true, default-features = false }
clapme = { version = "0.1.1", optional = true }
auto-args = { version = "0.2.4", optional = true }
generic-array = "0.12.0"
generic-array = "0.14.0"
num-traits = { version = "0.2.5", default-features = false }
quickcheck = { version = "0.8.2", optional = true }
serde = { version = "1.0.0", optional = true }
serde_test = { version = "1.0.0", optional = true }
rand = { version = "0.6.5", optional = true }
rand = { version = "0.8.5", optional = true }
typenum = "1.6.0"

[dev-dependencies]
quickcheck = { version = "0.8.2" }
33 changes: 27 additions & 6 deletions src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ macro_rules! base_units {
constant: stringify!($constant),
token: stringify!($token),
dim: stringify!($($dim)*),
}),*];
}),*]
);
}

Expand All @@ -397,7 +397,7 @@ macro_rules! derived_units {
constant: stringify!($constant),
expression: stringify!($e),
dim: stringify!($($dim)*),
}),*];
}),*]
);
}

Expand All @@ -408,7 +408,7 @@ macro_rules! constants {
constant: stringify!($constant),
value: stringify!($e),
name: $name,
}),*];
}),*]
);
}

Expand Down Expand Up @@ -545,14 +545,35 @@ fn make_conversion_tests(systems: &[System], out_dir: &str) -> Result<(), std::i
"
extern crate dimensioned as dim;
#[macro_use] extern crate approx;
mod test {{
use approx::UlpsEq;
use dim::Dimensioned;
use std::fmt;
#[path=\"../../../../../src/build/test.rs\"]
mod test;
pub trait CmpConsts<B> {{
fn test_eq(self, b: B);
}}
#[cfg(feature = \"spec\")]
impl<A, B> CmpConsts<B> for A {{
default fn test_eq(self, _: B) {{}}
}}
impl<A, B> CmpConsts<B> for A
where
A: From<B> + fmt::Debug + UlpsEq,
A::Epsilon: Dimensioned<Value = f64>,
{{
fn test_eq(self, b: B) {{
assert_ulps_eq!(self, b.into(), epsilon = A::Epsilon::new(0.0), max_ulps = 2);
}}
}}
}}
#[cfg(test)]
mod constant_conversion {{
use dim::unit_systems::*;
use test::CmpConsts;
use crate::test::CmpConsts;
"
)?;

Expand Down
22 changes: 0 additions & 22 deletions src/build/test.rs

This file was deleted.

28 changes: 14 additions & 14 deletions src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
mod ucum_to_si {
// From UCUM
use crate::f64prefixes::*;
use crate::si::SI;
use crate::ucum;
use core::convert::From;
use core::ops::{Add, Mul};
use crate::f64prefixes::*;
use num_traits::float::FloatCore;
use crate::si::SI;
use typenum::{Integer, Prod, Sum, Z0};
use crate::ucum;

impl<V, Meter, Second, Gram, Kelvin, Coulomb, Candela>
From<ucum::UCUM<V, tarr![Meter, Second, Gram, Z0, Kelvin, Coulomb, Candela]>>
Expand Down Expand Up @@ -61,13 +61,13 @@ mod ucum_to_si {

mod si_to_ucum {
// From SI
use crate::f64prefixes::*;
use crate::si;
use crate::ucum::UCUM;
use core::convert::From;
use core::ops::{Mul, Sub};
use crate::f64prefixes::*;
use num_traits::float::FloatCore;
use crate::si;
use typenum::{Diff, Integer, Prod, Z0};
use crate::ucum::UCUM;

impl<V, Meter, Kilogram, Second, Ampere, Kelvin, Candela>
From<si::SI<V, tarr![Meter, Kilogram, Second, Ampere, Kelvin, Candela, Z0]>>
Expand Down Expand Up @@ -101,12 +101,12 @@ mod mks_to_cgs {
use crate::cgs::CGS;

// From MKS
use core::convert::From;
use core::ops::Mul;
use crate::f64prefixes::*;
use crate::mks;
use num_traits::float::FloatCore;
use crate::traits::Sqrt;
use core::convert::From;
use core::ops::Mul;
use num_traits::float::FloatCore;
use typenum::{Integer, Prod};

impl<V, SqrtMeter, SqrtKilogram, Second>
Expand Down Expand Up @@ -139,9 +139,9 @@ mod mks_to_cgs {
mod si_to_cgs {
use crate::cgs::CGS;

use crate::mks;
use core::convert::From;
use core::ops::{Add, Mul};
use crate::mks;
use typenum::{Integer, Prod, Sum};

// From SI
Expand Down Expand Up @@ -179,12 +179,12 @@ mod si_to_cgs {

#[cfg(any(feature = "std", feature = "nightly"))]
mod cgs_to_mks {
use core::convert::From;
use core::ops::Mul;
use crate::f64prefixes::*;
use crate::mks::MKS;
use num_traits::float::FloatCore;
use crate::traits::Sqrt;
use core::convert::From;
use core::ops::Mul;
use num_traits::float::FloatCore;
use typenum::{Integer, Prod};

// From CGS
Expand Down Expand Up @@ -217,9 +217,9 @@ mod cgs_to_mks {
}

mod si_to_mks {
use crate::mks::MKS;
use core::convert::From;
use core::ops::{Add, Mul};
use crate::mks::MKS;
use typenum::{Integer, Prod, Sum};

// From SI
Expand Down
19 changes: 3 additions & 16 deletions src/make_units.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ If you would like non-unary integer constants, you will have to construct them y
# extern crate dimensioned as dim;
# use std::marker::PhantomData;
use dim::si;
const MIN: si::Second<u32> = si::SI { value_unsafe: 60, _marker: PhantomData };
const MIN: si::Second<u32> = si::Second::new(60);
# fn main() {}
```
Expand Down Expand Up @@ -203,27 +203,14 @@ macro_rules! make_units {
/// calculation in a dimensionally-safe interface.
pub value_unsafe: V,

/// This member is only temporarily public and so its use is considered unstable.
/// Right now, the only way to create a `const` with units is with this pattern:
///
/// ```rust
/// extern crate dimensioned as dim;
/// use dim::si;
///
/// const x: si::Meter<f64> = si::Meter { value_unsafe: 3.4, _marker: std::marker::PhantomData };
/// # fn main() {}
/// ```
///
/// Once `const_fns` is stabilized, that will be able to be replaced with a call to
/// `Meter::new` and `_marker` will be made private.
pub _marker: PhantomData<U>,
_marker: PhantomData<U>,
}

impl<V, U> $System<V, U> {

/// Create a new quantity in the $System unit system
#[inline]
pub fn new(v: V) -> Self {
pub const fn new(v: V) -> Self {
$System { value_unsafe: v, _marker: PhantomData }
}
}
Expand Down

0 comments on commit 7863686

Please sign in to comment.