Skip to content

Commit

Permalink
Add features for all types implementing Num.
Browse files Browse the repository at this point in the history
usize, u8, u16, u32, u64, u128 (unstable), isize, i8, i16, i32, i64,
i128 (unstable), bigint, biguint, rational, rational32, rational64,
bigrational, f32, f64. Only f32 and f64 are enabled by default for
compile time reasons. All other types are not yet implemented. Part
of #29.
  • Loading branch information
iliekturtles committed Nov 5, 2017
1 parent 8623762 commit d4d395a
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 54 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
* 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`, `isize`, `i8`,
`i16`, `i32`, `i64`, `bigint`, `biguint`, `rational`, `rational32`, `rational64`, `bigrational`,
`f32`, and `f64`. For compile time reasons only `f32` and `f64` are enabled by default.
* [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
16 changes: 16 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@ approx = "0.1.1"

[features]
default = ["f32", "f64", "si", "std"]
usize = []
u8 = []
u16 = []
u32 = []
u64 = []
isize = []
i8 = []
i16 = []
i32 = []
i64 = []
bigint = []
biguint = []
rational = []
rational32 = []
rational64 = []
bigrational = []
f32 = []
f64 = []
si = []
Expand Down
44 changes: 31 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,42 @@ fn main() {

See the [examples](examples) directory for more advanced usage:

* [si.rs](examples/si.rs) -- Example showing how to use the pre-built SI system.
* [base.rs](examples/base.rs) -- Example showing how to create a set of `Quantity` type aliases
for a different set of base units.
* [mks.rs](examples/mks.rs) -- Example showing how to create a custom system of quantities.
* [si.rs](examples/si.rs) -- Shows how to use the pre-built SI system.
* [base.rs](examples/base.rs) -- Shows how to create a set of `Quantity` type aliases for a
different set of base units.
* [mks.rs](examples/mks.rs) -- Shows how to create a custom system of quantities.

## Features
`uom` has four `Cargo` features: `f32`, `f64`, `si`, and `std`. The features are described below
and are enabled by default. Features can be cherry-picked by using the `--no-default-features` and
`--features "..."` flags when compiling `uom` or specifying features in Cargo.toml:
`uom` has multiple `Cargo` features for controlling available underlying storage types, the
inclusion of the pre-built [International System of Units][si] (SI), and `no_std` functionality. The
features are described below. `f32`, `f64`, `std`, and `si` are enabled by default. Features can be
cherry-picked by using the `--no-default-features` and `--features "..."` flags when compiling `uom`
or specifying features in Cargo.toml:

```toml
[dependencies]
uom = { version = "0.15.0", default-features = false, features = ["f32", "f64", "si", "std"] }
uom = {
version = "0.15.0",
default-features = false,
features = [
"usize", "u8", "u16", "u32", "u64", # Unsigned integer storage types.
"isize", "i8", "i16", "i32", "i64", # Signed interger storage types.
"bigint", "biguint", # Arbitrary width integer storage types.
"rational", "rational32", "rational64", "bigrational", # Integer ratio storage types.
"f32", "f64", # Floating point storage types.
"si", "std", # Built-in SI system and std library support.
]
}
```

* `f32`, `f64` -- Features to enable underlying storage types. At least one of these features must
be enabled.
* `si` -- Feature to include the pre-built [International System of Units][si] (SI).
* `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.
* `si` -- Feature to include the pre-built [International System of Units][si] (SI). Enabled by
default.
* `std` -- Feature to compile with standard library support. Disabling this feature compiles `uom`
with `no_std`. Note that some functions such as `sqrt` require `std` to be enabled.
with `no_std`. Enabled by default.

[si]: http://jcgm.bipm.org/vim/en/1.16.html

Expand All @@ -89,7 +105,9 @@ using the raw 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 both `f32` and `f64` as the underlying storage type.
(`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.

1. Once codegen bug [#38269](https://github.com/rust-lang/rust/issues/38269) is resolved.

Expand Down
60 changes: 42 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,11 @@
//! The simple example below shows how to use quantities and units as well as how `uom` stops
//! invalid operations:
//!
#![cfg_attr(feature = "si", doc = " ```rust")]
#![cfg_attr(not(feature = "si"), doc = " ```rust,ignore")]
#![cfg_attr(all(feature = "si", feature = "f32"), doc = " ```rust")]
#![cfg_attr(not(any(feature = "si", feature = "f32")), doc = " ```rust,ignore")]
//! extern crate uom;
//!
#![cfg_attr(feature = "f32", doc = " use uom::si::f32::*;")]
#![cfg_attr(not(feature = "f32"), doc = " use uom::si::f64::*;")]
//! use uom::si::f32::*;
//! use uom::si::length::kilometer;
//! use uom::si::time::second;
//!
Expand All @@ -48,23 +47,40 @@
//! ```
//!
//! See examples provided with the source for more advanced usage including how to create `Quantity`
//! type aliases for a different set of base units and how to create an entirely new system.
//! type aliases for a different set of base units and how to create an entirely new system of
//! quantities.
//!
//! ## Features
//! `uom` has four `Cargo` features: `f32`, `f64`, `si`, and `std`. The features are described below
//! and are enabled by default. Features can be cherry-picked by using the `--no-default-features`
//! and `--features "..."` flags when compiling `uom` or specifying features in Cargo.toml:
//! `uom` has multiple `Cargo` features for controlling available underlying storage types, the
//! inclusion of the pre-built [International System of Units][si] (SI), and `no_std` functionality.
//! The features are described below. `f32`, `f64`, `std`, and `si` are enabled by default. Features
//! can be cherry-picked by using the `--no-default-features` and `--features "..."` flags when
//! compiling `uom` or specifying features in Cargo.toml:
//!
//! ```toml
//! [dependencies]
//! uom = { version = "0.15.0", default-features = false, features = ["f32", "f64", "si", "std"] }
//! uom = {
//! version = "0.15.0",
//! default-features = false,
//! features = [
//! "usize", "u8", "u16", "u32", "u64", # Unsigned integer storage types.
//! "isize", "i8", "i16", "i32", "i64", # Signed interger storage types.
//! "bigint", "biguint", # Arbitrary width integer storage types.
//! "rational", "rational32", "rational64", "bigrational", # Integer ratio storage types.
//! "f32", "f64", # Floating point storage types.
//! "si", "std", # Built-in SI system and std library support.
//! ]
//! }
//! ```
//!
//! * `f32`, `f64` -- Features to enable underlying storage types. At least one of these features
//! must be enabled.
//! * `si` -- Feature to include the pre-built [International System of Units][si] (SI).
//! * `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.
//! * `si` -- Feature to include the pre-built [International System of Units][si] (SI). Enabled by
//! default.
//! * `std` -- Feature to compile with standard library support. Disabling this feature compiles
//! `uom` with `no_std`. Note that some functions such as `sqrt` require `std` to be enabled.
//! `uom` with `no_std`. Enabled by default.
//!
//! [si]: http://jcgm.bipm.org/vim/en/1.16.html
//!
Expand All @@ -78,8 +94,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 both `f32` and `f64` as the underlying storage
//! type.
//! 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.
//!
//! 1. Once codegen bug [#38269](https://github.com/rust-lang/rust/issues/38269) is resolved.
//!
Expand Down Expand Up @@ -108,9 +125,6 @@
//! [si]: http://jcgm.bipm.org/vim/en/1.16.html
//! [nist811]: https://www.nist.gov/pml/nist-guide-si-appendix-b9-factors-units-listed-kind-quantity-or-field-science
// Disable the entire crate if at least one of `f32` or `f64` features is not specified.
#![cfg(any(feature = "f32", feature = "f64"))]

// Compile with `no_std` when the `std` feature is not specified.
#![cfg_attr(not(feature = "std"), no_std)]

Expand All @@ -130,6 +144,16 @@
#![cfg_attr(feature = "clippy", feature(plugin))]
#![cfg_attr(feature = "clippy", plugin(clippy))]

// Fail to compile if no underlying storage type features are specified.
#[cfg(not(any(
feature = "usize", feature = "u8", feature = "u16", feature = "u32", feature = "u64",
feature = "isize", feature = "i8", feature = "i16", feature = "i32", feature = "i64",
feature = "bigint", feature = "biguint",
feature = "rational", feature = "rational32", feature = "rational64", feature = "bigrational",
feature = "f32", feature = "f64", )))]
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 typenum;

Expand Down
45 changes: 22 additions & 23 deletions src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
/// ```
/// #[macro_use]
/// extern crate uom;
///
///
/// # fn main() { }
/// # mod mks {
/// # #[macro_use]
Expand Down Expand Up @@ -169,9 +169,9 @@ macro_rules! system {
/// which is generic over the input unit and accepts the input value as it's only
/// parameter.
///
/// ```
#[cfg_attr(feature = "f32", doc = " # use uom::si::f32::*;")]
#[cfg_attr(not(feature = "f32"), doc = " # use uom::si::f64::*;")]
#[cfg_attr(all(feature = "si", feature = "f32"), doc = " ```rust")]
#[cfg_attr(not(any(feature = "si", feature = "f32")), doc = " ```rust,ignore")]
/// # use uom::si::f32::*;
/// # use uom::si::length::meter;
/// // Create a length of 1 meter.
/// let L = Length::new::<meter>(1.0);
Expand All @@ -181,17 +181,16 @@ macro_rules! system {
/// of non-named `Quantity`s. This functionality will be deprecated and subsequently removed
/// once the `const fn` feature is stabilized.
///
/// ```
#[cfg_attr(all(feature = "si", feature = "f32"), doc = " ```rust")]
#[cfg_attr(not(any(feature = "si", feature = "f32")), doc = " ```rust,ignore")]
/// # use uom::si::{Quantity, ISQ, SI};
#[cfg_attr(feature = "f32", doc = " # use uom::si::f32::*;")]
#[cfg_attr(not(feature = "f32"), doc = " # use uom::si::f64::*;")]
/// # use uom::si::f32::*;
/// # use uom::stdlib::marker::PhantomData;
/// # use uom::typenum::{P2, Z0};
/// // Create a `const` length of 1 meter.
/// const L: Length = Length { dimension: PhantomData, units: PhantomData, value: 1.0, };
/// // Create a `const` area of 1 square meter explicitly without using the `Area` alias.
#[cfg_attr(feature = "f32", doc = " const A: Quantity<ISQ<P2, Z0, Z0, Z0, Z0, Z0, Z0>, SI<f32>, f32> =")]
#[cfg_attr(not(feature = "f32"), doc = " const A: Quantity<ISQ<P2, Z0, Z0, Z0, Z0, Z0, Z0>, SI<f64>, f64> =")]
/// const A: Quantity<ISQ<P2, Z0, Z0, Z0, Z0, Z0, Z0>, SI<f32>, f32> =
/// Quantity { dimension: PhantomData, units: PhantomData, value: 1.0, };
/// ```
///
Expand Down Expand Up @@ -507,9 +506,9 @@ macro_rules! system {

/// Takes the cubic root of a number.
///
/// ```
#[cfg_attr(feature = "f32", doc = " # use uom::si::f32::*;")]
#[cfg_attr(not(feature = "f32"), doc = " # use uom::si::f64::*;")]
#[cfg_attr(all(feature = "si", feature = "f32"), doc = " ```rust")]
#[cfg_attr(not(any(feature = "si", feature = "f32")), doc = " ```rust,ignore")]
/// # use uom::si::f32::*;
/// # use uom::si::volume::cubic_meter;
/// let l: Length = Volume::new::<cubic_meter>(8.0).cbrt();
/// ```
Expand Down Expand Up @@ -604,9 +603,9 @@ macro_rules! system {

/// Takes the reciprocal (inverse) of a number, `1/x`.
///
/// ```
#[cfg_attr(feature = "f32", doc = " # use uom::si::f32::*;")]
#[cfg_attr(not(feature = "f32"), doc = " # use uom::si::f64::*;")]
#[cfg_attr(all(feature = "si", feature = "f32"), doc = " ```rust")]
#[cfg_attr(not(any(feature = "si", feature = "f32")), doc = " ```rust,ignore")]
/// # use uom::si::f32::*;
/// # use uom::si::time::second;
/// let f: Frequency = Time::new::<second>(1.0).recip();
/// ```
Expand All @@ -627,9 +626,9 @@ macro_rules! system {

/// Raises a quantity to an integer power.
///
/// ```
#[cfg_attr(feature = "f32", doc = " # use uom::si::f32::*;")]
#[cfg_attr(not(feature = "f32"), doc = " # use uom::si::f64::*;")]
#[cfg_attr(all(feature = "si", feature = "f32"), doc = " ```rust")]
#[cfg_attr(not(any(feature = "si", feature = "f32")), doc = " ```rust,ignore")]
/// # use uom::si::f32::*;
/// # use uom::si::length::meter;
/// let a: Area = Length::new::<meter>(3.0).powi(::uom::typenum::P2::new());
/// ```
Expand All @@ -653,9 +652,9 @@ macro_rules! system {
/// Takes the square root of a number. Returns `NAN` if `self` is a negative
/// number.
///
/// ```
#[cfg_attr(feature = "f32", doc = " # use uom::si::f32::*;")]
#[cfg_attr(not(feature = "f32"), doc = " # use uom::si::f64::*;")]
#[cfg_attr(all(feature = "si", feature = "f32"), doc = " ```rust")]
#[cfg_attr(not(any(feature = "si", feature = "f32")), doc = " ```rust,ignore")]
/// # use uom::si::f32::*;
/// # use uom::si::area::square_meter;
/// let l: Length = Area::new::<square_meter>(4.0).sqrt();
/// ```
Expand Down Expand Up @@ -799,7 +798,7 @@ macro_rules! system {
/// ```ignore
/// #[macro_use]
/// extern crate uom;
///
///
/// # fn main() { }
/// # mod mks {
/// # #[macro_use]
Expand Down Expand Up @@ -916,7 +915,7 @@ macro_rules! system {
/// ```ignore
/// #[macro_use]
/// extern crate uom;
///
///
/// # fn main() { }
/// # mod mks {
/// #[macro_use]
Expand Down

0 comments on commit d4d395a

Please sign in to comment.