Skip to content

Commit

Permalink
Add support for i128/u128 underlying storage types.
Browse files Browse the repository at this point in the history
Resolves #85.
  • Loading branch information
iliekturtles committed Jun 29, 2019
1 parent 0dd0630 commit e2d5f20
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ matrix:
cargo test --verbose --no-default-features --features "f32 si"
cargo test --verbose --no-default-features --features "autoconvert f32 si use_serde"
cargo test --verbose --no-run --no-default-features --features "autoconvert usize isize bigint bigrational si std use_serde"
cargo test --verbose --no-run --no-default-features --features "autoconvert usize u8 u16 u32 u64 isize i8 i16 i32 i64 bigint biguint rational rational32 rational64 bigrational f32 f64 std use_serde"
cargo test --verbose --no-run --no-default-features --features "autoconvert usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 bigint biguint rational rational32 rational64 bigrational f32 f64 std use_serde"
- name: Rustfmt
rust: 1.35.0
install:
Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ u8 = ["rational-support"]
u16 = ["rational-support"]
u32 = ["rational-support"]
u64 = ["rational-support"]
u128 = ["rational-support"]
isize = ["rational-support"]
i8 = ["rational-support"]
i16 = ["rational-support"]
i32 = ["rational-support"]
i64 = ["rational-support"]
i128 = ["rational-support"]
bigint = ["bigint-support"]
biguint = ["bigint-support"]
rational = ["rational-support"]
Expand Down
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ uom = {
default-features = false,
features = [
"autoconvert", # automatic base unit conversion.
"usize", "u8", "u16", "u32", "u64", # Unsigned integer storage types.
"isize", "i8", "i16", "i32", "i64", # Signed integer storage types.
"usize", "u8", "u16", "u32", "u64", "u128", # Unsigned integer storage types.
"isize", "i8", "i16", "i32", "i64", "i128", # Signed integer storage types.
"bigint", "biguint", # Arbitrary width integer storage types.
"rational", "rational32", "rational64", "bigrational", # Integer ratio storage types.
"f32", "f64", # Floating point storage types.
Expand All @@ -96,10 +96,10 @@ uom = {
Disabling the feature only allows for quantities with the same base units to directly interact.
The feature exists to account for compiler limitations where zero-cost code is not generated for
non-floating point underlying storage types.
* `usize`, `u8`, `u16`, `u32`, `u64`, `isize`, `i8`, `i16`, `i32`, `i64`, `bigint`, `biguint`,
`rational`, `rational32`, `rational64`, `bigrational`, `f32`, `f64` -- Features to enable
underlying storage types. At least one of these features must be enabled. `f32` and `f64` are
enabled by default. See the [Design](#design) section for implications of choosing different
* `usize`, `u8`, `u16`, `u32`, `u64`, `u128`, `isize`, `i8`, `i16`, `i32`, `i64`, `i128`, `bigint`,
`biguint`, `rational`, `rational32`, `rational64`, `bigrational`, `f32`, `f64` -- Features to
enable underlying storage types. At least one of these features must be enabled. `f32` and `f64`
are enabled by default. See the [Design](#design) section for implications of choosing different
underlying storage types.
* `si` -- Feature to include the pre-built [International System of Units][si] (SI). Enabled by
default.
Expand All @@ -121,9 +121,9 @@ storage type (e.g. `f32`).

`uom` normalizes values to the [base unit](http://jcgm.bipm.org/vim/en/1.10.html) for the quantity.
Alternative base units can be used by executing the macro defined for the system of quantities
(`ISQ!` for the SI). `uom` supports `usize`, `u8`, `u16`, `u32`, `u64`, `isize`, `i8`, `i16`, `i32`,
`i64`, `bigint`, `biguint`, `rational`, `rational32`, `rational64`, `bigrational`, `f32`, and `f64`
as the underlying storage type.
(`ISQ!` for the SI). `uom` supports `usize`, `u8`, `u16`, `u32`, `u64`, `u128`, `isize`, `i8`,
`i16`, `i32`, `i64`, `i128`, `bigint`, `biguint`, `rational`, `rational32`, `rational64`,
`bigrational`, `f32`, and `f64` as the underlying storage type.

A consequence of normalizing values to the base unit is that some values may not be able to be
represented or can't be precisely represented for floating point and rational underlying storage
Expand Down
21 changes: 12 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@
//! default-features = false,
//! features = [
//! "autoconvert", # automatic base unit conversion.
//! "usize", "u8", "u16", "u32", "u64", # Unsigned integer storage types.
//! "isize", "i8", "i16", "i32", "i64", # Signed integer storage types.
//! "usize", "u8", "u16", "u32", "u64", "u128", # Unsigned integer storage types.
//! "isize", "i8", "i16", "i32", "i64", "i128", # Signed integer storage types.
//! "bigint", "biguint", # Arbitrary width integer storage types.
//! "rational", "rational32", "rational64", "bigrational", # Integer ratio storage types.
//! "f32", "f64", # Floating point storage types.
Expand All @@ -85,10 +85,11 @@
//! operators. Disabling the feature only allows for quantities with the same base units to
//! directly interact. The feature exists to account for compiler limitations where zero-cost
//! code is not generated for non-floating point underlying storage types.
//! * `usize`, `u8`, `u16`, `u32`, `u64`, `isize`, `i8`, `i16`, `i32`, `i64`, `bigint`, `biguint`,
//! `rational`, `rational32`, `rational64`, `bigrational`, `f32`, `f64` -- Features to enable
//! underlying storage types. At least one of these features must be enabled. `f32` and `f64` are
//! enabled by default. See the [Design](#design) section for implications of choosing different
//! * `usize`, `u8`, `u16`, `u32`, `u64`, `u128`, `isize`, `i8`, `i16`, `i32`, `i64`, `i128`,
//! `bigint`, `biguint`, `rational`, `rational32`, `rational64`, `bigrational`, `f32`, `f64` --
//! Features to enable underlying storage types. At least one of these features must be enabled.
//! `f32` and `f64` are enabled by default. See the [Design](#design) section for implications
//! of choosing different
//! underlying storage types.
//! * `si` -- Feature to include the pre-built [International System of Units][si] (SI). Enabled by
//! default.
Expand All @@ -110,9 +111,9 @@
//!
//! `uom` normalizes values to the [base unit](http://jcgm.bipm.org/vim/en/1.10.html) for the
//! quantity. Alternative base units can be used by executing the macro defined for the system of
//! quantities (`ISQ!` for the SI). `uom` supports `usize`, `u8`, `u16`, `u32`, `u64`, `isize`,
//! `i8`, `i16`, `i32`, `i64`, `bigint`, `biguint`, `rational`, `rational32`, `rational64`,
//! `bigrational`, `f32`, and `f64` as the underlying storage type.
//! quantities (`ISQ!` for the SI). `uom` supports `usize`, `u8`, `u16`, `u32`, `u64`, `u128`,
//! `isize`, `i8`, `i16`, `i32`, `i64`, `i128`, `bigint`, `biguint`, `rational`, `rational32`,
//! `rational64`, `bigrational`, `f32`, and `f64` as the underlying storage type.
//!
//! A consequence of normalizing values to the base unit is that some values may not be able to be
//! represented or can't be precisely represented for floating point and rational underlying
Expand Down Expand Up @@ -179,7 +180,9 @@
#[cfg_attr(rustfmt, rustfmt_skip)]
#[cfg(not(any(
feature = "usize", feature = "u8", feature = "u16", feature = "u32", feature = "u64",
feature = "u128",
feature = "isize", feature = "i8", feature = "i16", feature = "i32", feature = "i64",
feature = "i128",
feature = "bigint", feature = "biguint",
feature = "rational", feature = "rational32", feature = "rational64", feature = "bigrational",
feature = "f32", feature = "f64", )))]
Expand Down
32 changes: 24 additions & 8 deletions src/storage_types.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
/// Macro to duplicate code a per-storage type basis. The given code is duplicated in new modules
/// named for each storage type. A type alias, `V`, is generated that code can use for the type.
/// `@...` match arms are considered private.
/// Macro to duplicate code on a per-storage type basis. The given code is duplicated in new
/// modules named for each storage type. A type alias, `V`, is generated that code can use for the
/// type. `@...` match arms are considered private.
///
/// * `$attr`: Module attributes. Generally used to set documentation comments for storage type
/// modules generated by the macro.
/// * `$T`: Types to generate a module for. Accepts all underlying storage types along with a number
/// of different categories:
/// * `All`: `usize`, `u8`, `u16`, `u32`, `u64`, `isize`, `i8`, `i16`, `i32`, `i64`, `BigInt`,
/// `BigUint`, `Rational`, `Rational32`, `Rational64`, `BigRational`, `f32`, and `f64`.
/// * `PrimInt`: `usize`, `u8`, `u16`, `u32`, `u64`, `isize`, `i8`, `i16`, `i32`, `i64`.
/// * `All`: `usize`, `u8`, `u16`, `u32`, `u64`, `u128`, `isize`, `i8`, `i16`, `i32`, `i64`,
/// `i128`, `BigInt`, `BigUint`, `Rational`, `Rational32`, `Rational64`, `BigRational`, `f32`,
/// and `f64`.
/// * `PrimInt`: `usize`, `u8`, `u16`, `u32`, `u64`, `u128`, `isize`, `i8`, `i16`, `i32`, `i64`,
/// and `i128`.
/// * `Ratio`: `Rational`, `Rational32`, `Rational64`, and `BigRational`.
/// * `Float`: `f32` and `f64`.
/// * `Signed`: `isize`, `i8`, `i16`, `i32`, `i64`, `BigInt`, `Rational`, `Rational32`,
/// * `Signed`: `isize`, `i8`, `i16`, `i32`, `i64`, `i128`, `BigInt`, `Rational`, `Rational32`,
/// `Rational64`, `BigRational`, `f32`, and `f64`.
/// * `Unsigned`: `usize`, `u8`, `u16`, `u32`, `u64`, and `BigUint`.
/// * `Unsigned`: `usize`, `u8`, `u16`, `u32`, `u64`, `u128`, and `BigUint`.
/// * `$tt`: Code to place into each storage type module.
///
#[cfg_attr(all(feature = "f32", feature = "f64"), doc = " ```rust")]
Expand Down Expand Up @@ -59,6 +61,9 @@ macro_rules! storage_types {
(@type ($(#[$attr:meta])*) @$M:ident u64 ($($tt:tt)*)) => {
storage_type_u64!(($(#[$attr])*) @$M ($($tt)*));
};
(@type ($(#[$attr:meta])*) @$M:ident u128 ($($tt:tt)*)) => {
storage_type_u128!(($(#[$attr])*) @$M ($($tt)*));
};
(@type ($(#[$attr:meta])*) @$M:ident isize ($($tt:tt)*)) => {
storage_type_isize!(($(#[$attr])*) @$M ($($tt)*));
};
Expand All @@ -74,6 +79,9 @@ macro_rules! storage_types {
(@type ($(#[$attr:meta])*) @$M:ident i64 ($($tt:tt)*)) => {
storage_type_i64!(($(#[$attr])*) @$M ($($tt)*));
};
(@type ($(#[$attr:meta])*) @$M:ident i128 ($($tt:tt)*)) => {
storage_type_i128!(($(#[$attr])*) @$M ($($tt)*));
};
(@type ($(#[$attr:meta])*) @$M:ident BigInt ($($tt:tt)*)) => {
storage_type_bigint!(($(#[$attr])*) @$M ($($tt)*));
};
Expand Down Expand Up @@ -104,11 +112,13 @@ macro_rules! storage_types {
storage_types!(@type ($(#[$attr])*) @$M u16 ($($tt)*));
storage_types!(@type ($(#[$attr])*) @$M u32 ($($tt)*));
storage_types!(@type ($(#[$attr])*) @$M u64 ($($tt)*));
storage_types!(@type ($(#[$attr])*) @$M u128 ($($tt)*));
storage_types!(@type ($(#[$attr])*) @$M isize ($($tt)*));
storage_types!(@type ($(#[$attr])*) @$M i8 ($($tt)*));
storage_types!(@type ($(#[$attr])*) @$M i16 ($($tt)*));
storage_types!(@type ($(#[$attr])*) @$M i32 ($($tt)*));
storage_types!(@type ($(#[$attr])*) @$M i64 ($($tt)*));
storage_types!(@type ($(#[$attr])*) @$M i128 ($($tt)*));
storage_types!(@type ($(#[$attr])*) @$M BigInt ($($tt)*));
storage_types!(@type ($(#[$attr])*) @$M BigUint ($($tt)*));
storage_types!(@type ($(#[$attr])*) @$M Rational ($($tt)*));
Expand All @@ -124,11 +134,13 @@ macro_rules! storage_types {
storage_types!(@type ($(#[$attr])*) @$M u16 ($($tt)*));
storage_types!(@type ($(#[$attr])*) @$M u32 ($($tt)*));
storage_types!(@type ($(#[$attr])*) @$M u64 ($($tt)*));
storage_types!(@type ($(#[$attr])*) @$M u128 ($($tt)*));
storage_types!(@type ($(#[$attr])*) @$M isize ($($tt)*));
storage_types!(@type ($(#[$attr])*) @$M i8 ($($tt)*));
storage_types!(@type ($(#[$attr])*) @$M i16 ($($tt)*));
storage_types!(@type ($(#[$attr])*) @$M i32 ($($tt)*));
storage_types!(@type ($(#[$attr])*) @$M i64 ($($tt)*));
storage_types!(@type ($(#[$attr])*) @$M i128 ($($tt)*));
};
(@type ($(#[$attr:meta])*) @$M:ident Ratio ($($tt:tt)*)) => {
storage_types!(@type ($(#[$attr])*) @$M Rational ($($tt)*));
Expand All @@ -146,6 +158,7 @@ macro_rules! storage_types {
storage_types!(@type ($(#[$attr])*) @$M i16 ($($tt)*));
storage_types!(@type ($(#[$attr])*) @$M i32 ($($tt)*));
storage_types!(@type ($(#[$attr])*) @$M i64 ($($tt)*));
storage_types!(@type ($(#[$attr])*) @$M i128 ($($tt)*));
storage_types!(@type ($(#[$attr])*) @$M BigInt ($($tt)*));
storage_types!(@type ($(#[$attr])*) @$M Rational ($($tt)*));
storage_types!(@type ($(#[$attr])*) @$M Rational32 ($($tt)*));
Expand All @@ -160,6 +173,7 @@ macro_rules! storage_types {
storage_types!(@type ($(#[$attr])*) @$M u16 ($($tt)*));
storage_types!(@type ($(#[$attr])*) @$M u32 ($($tt)*));
storage_types!(@type ($(#[$attr])*) @$M u64 ($($tt)*));
storage_types!(@type ($(#[$attr])*) @$M u128 ($($tt)*));
storage_types!(@type ($(#[$attr])*) @$M BigUint ($($tt)*));
};
(@mod ($(#[$attr:meta])*) $M:ident, $V:ty; ($($tt:tt)*)) => {
Expand Down Expand Up @@ -218,11 +232,13 @@ storage_type_types! {
storage_type_u16!("u16", u16, u16);
storage_type_u32!("u32", u32, u32);
storage_type_u64!("u64", u64, u64);
storage_type_u128!("u128", u128, u128);
storage_type_isize!("isize", isize, isize);
storage_type_i8!("i8", i8, i8);
storage_type_i16!("i16", i16, i16);
storage_type_i32!("i32", i32, i32);
storage_type_i64!("i64", i64, i64);
storage_type_i128!("i128", i128, i128);
storage_type_bigint!("bigint", bigint, $crate::num::BigInt);
storage_type_biguint!("biguint", biguint, $crate::num::BigUint);
storage_type_rational!("rational", rational, $crate::num::Rational);
Expand Down
5 changes: 3 additions & 2 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ impl<V> ::lib::ops::Deref for A<V> {

mod a_struct {
storage_types! {
types: Float, PrimInt;
// Quickcheck 0.8 required for i128/u128 support. Use PrimInt after upgrade.
types: Float, usize, u8, u16, u32, u64, isize, i8, i16, i32, i64;

use super::super::A;

Expand All @@ -155,7 +156,7 @@ mod a_struct {
}

storage_types! {
types: BigInt, BigUint, Ratio;
types: BigInt, BigUint, Ratio, i128, u128;

use num::FromPrimitive;
use super::super::A;
Expand Down

0 comments on commit e2d5f20

Please sign in to comment.