Skip to content

Commit

Permalink
Add a Kind associated type to Dimensions.
Browse files Browse the repository at this point in the history
The new `Kind` associated type, defaulting to `()`, allows for multiple
quantities that have the same dimensions to be defined. Quantities of
different kind are not comparable. Resolves #78.
  • Loading branch information
iliekturtles committed May 20, 2018
1 parent c99c192 commit 3b82692
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
23 changes: 22 additions & 1 deletion src/quantity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
/// * `$system`: System of quantities type (e.g. `ISQ`).
/// * `$dimension`: Power of a factor for each base quantity in the system. Power should be
/// represented as a `typenum` type-level integer (e.g. `N1`, `Z0`, `P1`, `P2`, ...).
/// * `$kind`: [Kind][kind] of the quantity. Optional. This variable should only be specified when
/// defining a quantity that has the same dimensions as another quantity but isn't comparable.
/// * `$unit`: Unit name (e.g. `meter`, `foot`).
/// * `$conversion`: Conversion from the unit to the base unit of the quantity (e.g. `3.048E-1` to
/// convert `foot` to `meter`).
Expand Down Expand Up @@ -87,6 +89,7 @@
///
/// [quantity]: http://jcgm.bipm.org/vim/en/1.1.html
/// [measurement]: http://jcgm.bipm.org/vim/en/1.9.html
/// [kind]: https://jcgm.bipm.org/vim/en/1.2.html
#[macro_export]
macro_rules! quantity {
(
Expand All @@ -96,9 +99,27 @@ macro_rules! quantity {
$($(#[$unit_attr:meta])* @$unit:ident: $conversion:expr;
$abbreviation:expr, $singular:expr, $plural:expr;)+
}
) => {
quantity! {
$(#[$quantity_attr])* quantity: $quantity; $description;
$(#[$dim_attr])* dimension: $system<$($dimension),+>;
kind: ();
units {
$($(#[$unit_attr])* @$unit: $conversion; $abbreviation, $singular, $plural;)+
}
}
};
(
$(#[$quantity_attr:meta])* quantity: $quantity:ident; $description:expr;
$(#[$dim_attr:meta])* dimension: $system:ident<$($dimension:ident),+>;
kind: $kind:ty;
units {
$($(#[$unit_attr:meta])* @$unit:ident: $conversion:expr; $abbreviation:expr,
$singular:expr, $plural:expr;)+
}
) => {
$(#[$dim_attr])*
pub type Dimension = super::$system<$($crate::typenum::$dimension),+>;
pub type Dimension = super::$system<$($crate::typenum::$dimension),+, $kind>;

$(#[$quantity_attr])*
pub type $quantity<U, V> = super::Quantity<Dimension, U, V>;
Expand Down
5 changes: 5 additions & 0 deletions src/si/thermodynamic_temperature.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
//! Thermodynamic temperature (base unit kelvin, K<sup>1</sup>).
/// Kind of thermodynamic temperature.
#[derive(Clone, Copy, Debug, Hash)]
pub struct Temperature;

quantity! {
/// Thermodynamic temperature (base unit kelvin, K<sup>1</sup>).
quantity: ThermodynamicTemperature; "thermodynamic temperature";
Expand All @@ -12,6 +16,7 @@ quantity! {
P1, // thermodynamic temperature
Z0, // amount of substance
Z0>; // luminous intensity
kind: Temperature;
units {
@yottakelvin: prefix!(yotta); "YK", "yottakelvin", "yottakelvins";
@zettakelvin: prefix!(zetta); "ZK", "zettakelvin", "zettakelvins";
Expand Down
9 changes: 7 additions & 2 deletions src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ macro_rules! system {
pub trait Dimension: Send + Sync {
$(/// Quantity dimension.
type $symbol: $crate::typenum::Integer;)+
/// [Kind][kind] of the quantity. Quantities of the same dimension but differing kinds
/// are not comparable.
///
/// [kind]: https://jcgm.bipm.org/vim/en/1.2.html
type Kind;
}

/// Marker trait to identify a [system of units][units] based on a set of [base units][base]
Expand Down Expand Up @@ -236,7 +241,7 @@ macro_rules! system {
}

// Type alias for dimensions where all exponents of the factors are the given value.
type DN<N> = Dimension<$($symbol = system!(@replace $symbol N)),+>;
type DN<N> = Dimension<$($symbol = system!(@replace $symbol N),)+ Kind = ()>;

/// Type alias for [dimension one][one] for which all the exponents of the factors
/// corresponding to the [base quantities][base] are zero.
Expand All @@ -247,7 +252,7 @@ macro_rules! system {
pub type DimensionOne = DN<$crate::typenum::Z0>;

$(#[$quantities_attr])*
pub type $quantities<$($symbol),+> = Dimension<$($symbol = $symbol),+>;
pub type $quantities<$($symbol,)+ _K = ()> = Dimension<$($symbol = $symbol,)+ Kind = _K>;

$(#[$units_attr])*
#[allow(unused_qualifications)]
Expand Down

0 comments on commit 3b82692

Please sign in to comment.