Skip to content

Commit

Permalink
Add rudimentary docs and tests for int constants (#54)
Browse files Browse the repository at this point in the history
* Add rudimentary docs and tests for int constants

* Bump version

* Muck with travis
  • Loading branch information
paholg committed Aug 13, 2018
1 parent a57d3d2 commit c9e8bfc
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Expand Up @@ -6,6 +6,10 @@ notifications:
email:
recipients: paho@paholg.com

branches:
only:
- master

matrix:
include:
- rust: stable
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,8 @@
This project follows semantic versioning.

### Unpublished

### 0.7.0 (2018-08-12)
- [changed] ***BREAKING*** Made dimensioned work with `no_std` again, and added the default feature
`std`. Using this without `std` now requires nightly Rust and using `dimensioned` without default
features. I don't think this will actually break anything, as it was not working before, but is at
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "dimensioned"
version = "0.6.0"
version = "0.7.0"
authors = ["Paho Lurie-Gregg <paho@paholg.com>"]
documentation = "https://docs.rs/dimensioned"
repository = "https://github.com/paholg/dimensioned"
Expand Down
23 changes: 20 additions & 3 deletions src/make_units.rs
Expand Up @@ -110,11 +110,28 @@ constants in the `base` and `derived` blocks are always created with a value of
All constants are created in both `f32` and `f64` flavors, in the submodules `f32consts` and
`f64consts`, respectively.
In addition, the modules for all integer constants are created. However, these only include
constants for base and derived units. The full list of integer modules is `i8consts`, `i16consts`,
`i32consts`, `i64consts`, `isize_consts`, `u8consts`, `u16consts`, `u32consts`, `u64consts`,
`usize_consts`.
If you would like non-unary integer constants, you will have to construct them yourself, like so
```rust
# extern crate dimensioned as dim;
# use std::marker::PhantomData;
use dim::si;
const MIN: si::Second<u32> = si::SI { value_unsafe: 60, _marker: PhantomData };
# fn main() {}
```
Support for making this better is in the works.
In these submodules, the consts from the respective version of `f32prefixes` or `f64prefixes` are in
scope, hence the use of `CENTI` in the `CM` definition.
In addition, the the respective version of `core::f32::consts` or `core::f64::consts` is in scope,
which allows the use of `consts::PI` in the `PI definition.
In addition, the respective version of `core::f32::consts` or `core::f64::consts` is in scope, which
allows the use of `consts::PI` in the `PI definition.
```ignore
constants {
Expand Down Expand Up @@ -142,7 +159,7 @@ This line isn't part of the macro, but I wanted to include it as it is in all of
defined in dimensioned. It lets us use the `f64` flavor of constants much easier. E.g. we can now
type `ms::M` instead of `ms::f64consts::M`.
```igore
```ignore
pub use self::f64consts::*;
}
```
Expand Down
39 changes: 39 additions & 0 deletions tests/ints.rs
@@ -0,0 +1,39 @@
extern crate dimensioned as dim;

use dim::si;

// Just a couple simple tests to ensure that we're creating int consts.

#[test]
fn int_consts() {
use si::Meter;

assert_eq!(Meter::new(1), si::i8consts::M);
assert_eq!(Meter::new(1), si::i16consts::M);
assert_eq!(Meter::new(1), si::i32consts::M);
assert_eq!(Meter::new(1), si::i64consts::M);
assert_eq!(Meter::new(1), si::isize_consts::M);

assert_eq!(Meter::new(1), si::u8consts::M);
assert_eq!(Meter::new(1), si::u16consts::M);
assert_eq!(Meter::new(1), si::u32consts::M);
assert_eq!(Meter::new(1), si::u64consts::M);
assert_eq!(Meter::new(1), si::usize_consts::M);
}

#[test]
fn derived_int_consts() {
use si::Newton;

assert_eq!(Newton::new(1), si::i8consts::N);
assert_eq!(Newton::new(1), si::i16consts::N);
assert_eq!(Newton::new(1), si::i32consts::N);
assert_eq!(Newton::new(1), si::i64consts::N);
assert_eq!(Newton::new(1), si::isize_consts::N);

assert_eq!(Newton::new(1), si::u8consts::N);
assert_eq!(Newton::new(1), si::u16consts::N);
assert_eq!(Newton::new(1), si::u32consts::N);
assert_eq!(Newton::new(1), si::u64consts::N);
assert_eq!(Newton::new(1), si::usize_consts::N);
}

0 comments on commit c9e8bfc

Please sign in to comment.