Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added torque as an alias of energy #124

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
348 changes: 209 additions & 139 deletions src/quantity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,25 @@ macro_rules! quantity {
}
}
};
(
$(#[$quantity_attr:meta])* quantity: $quantity:ident; $description:expr;
alias_of: $alias_of:ident;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why add the alias? In my first brief look at the code it looks like it allows the alias's units to be used?

Quantities with the same dimension and kind are already type aliases although I'm they can't share units currently.

type Energy = Quantity<m^2 * kg * s^-2>;
type Torque = Quantity<m^2 * kg * s^-2>;

Copy link
Author

@dunmatt dunmatt Apr 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's so the Conversion can extend the Unit of both the original type and the alias. Or to take it at a deeper level it's because "I don't fully understand how this library hangs together, but I had to do this in order to get test to compile."

$(#[$dim_attr:meta])* dimension: $system:ident<$($dimension:ident),+>;
units {
$($(#[$unit_attr:meta])* @$unit:ident: $($conversion:expr),+;
$abbreviation:expr, $singular:expr, $plural:expr;)+
}
) => {
quantity! {
$(#[$quantity_attr])* quantity: $quantity; $description;
alias_of: $alias_of;
$(#[$dim_attr])* dimension: $system<$($dimension),+>;
kind: $crate::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),+>;
Expand All @@ -122,15 +141,14 @@ macro_rules! quantity {
$singular:expr, $plural:expr;)+
}
) => {
$(#[$dim_attr])*
pub type Dimension = super::$system<$($crate::typenum::$dimension),+, $kind>;

$(#[$quantity_attr])*
pub type $quantity<U, V> = super::Quantity<Dimension, U, V>;

/// Marker trait to identify measurement units for the quantity. See
/// [`Unit`](../trait.Unit.html).
pub trait Unit: super::Unit {}
quantity! { @common
$(#[$quantity_attr])* quantity: $quantity; $description;
$(#[$dim_attr])* dimension: $system<$($dimension),+>;
kind: $kind;
units {
$($(#[$unit_attr])* @$unit: $($conversion),+; $abbreviation, $singular, $plural;)+
}
}

/// Trait to identify [units][units] which have a [conversion factor][factor] for the
/// `Quantity`. See [`Conversion<V>`](../../trait.Conversion.html).
Expand All @@ -143,136 +161,6 @@ macro_rules! quantity {
{
}

$(quantity!(@unit $(#[$unit_attr])* @$unit);

impl super::Unit for $unit {
#[inline(always)]
fn abbreviation() -> &'static str {
$abbreviation
}

#[inline(always)]
fn singular() -> &'static str {
$singular
}

#[inline(always)]
fn plural() -> &'static str {
$plural
}
}

impl Unit for $unit {})+

storage_types! {
types: Float;

$(impl $crate::Conversion<V> for super::$unit {
type T = V;

#[inline(always)]
fn coefficient() -> Self::T {
quantity!(@coefficient $($conversion),+)
}

#[inline(always)]
fn constant() -> Self::T {
quantity!(@constant $($conversion),+)
}
}

impl super::Conversion<V> for super::$unit {})+
}

storage_types! {
types: PrimInt, BigInt;
pub type T = $crate::num::rational::Ratio<V>;

#[inline(always)]
fn from_f64(value: f64) -> T {
<T as $crate::num::FromPrimitive>::from_f64(value).unwrap()
}

$(impl $crate::Conversion<V> for super::$unit {
type T = T;

#[inline(always)]
fn coefficient() -> Self::T {
from_f64(quantity!(@coefficient $($conversion),+))
}

#[inline(always)]
fn constant() -> Self::T {
from_f64(quantity!(@constant $($conversion),+))
}
}

impl super::Conversion<V> for super::$unit {})+
}

storage_types! {
types: BigUint;
pub type T = $crate::num::rational::Ratio<V>;

#[inline(always)]
fn from_f64(value: f64) -> T {
use $crate::num::FromPrimitive;

let c = $crate::num::rational::Ratio::<$crate::num::BigInt>::from_f64(value)
.unwrap();

T::new(c.numer().to_biguint().unwrap(), c.denom().to_biguint().unwrap())
}

$(impl $crate::Conversion<V> for super::$unit {
type T = T;

#[inline(always)]
fn coefficient() -> Self::T {
from_f64(quantity!(@coefficient $($conversion),+))
}

#[inline(always)]
fn constant() -> Self::T {
from_f64(quantity!(@constant $($conversion),+))
}
}

impl super::Conversion<V> for super::$unit {})+
}

storage_types! {
types: Ratio;

#[inline(always)]
fn from_f64(value: f64) -> V {
<V as $crate::num::FromPrimitive>::from_f64(value).unwrap()
}

$(impl $crate::Conversion<V> for super::$unit {
type T = V;

#[inline(always)]
fn coefficient() -> Self::T {
from_f64(quantity!(@coefficient $($conversion),+))
}

#[inline(always)]
fn constant() -> Self::T {
from_f64(quantity!(@constant $($conversion),+))
}
}

impl super::Conversion<V> for super::$unit {})+
}

/// Quantity description.
#[allow(dead_code)]
#[inline(always)]
pub fn description() -> &'static str {
$description
}

impl<U, V> $quantity<U, V>
where
U: super::Units<V> + ?Sized,
Expand Down Expand Up @@ -478,6 +366,188 @@ macro_rules! quantity {
}
}
};
(
$(#[$quantity_attr:meta])* quantity: $quantity:ident; $description:expr;
alias_of: $alias_of:ident;
$(#[$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;)+
}
) => {
quantity! { @common
$(#[$quantity_attr])* quantity: $quantity; $description;
$(#[$dim_attr])* dimension: $system<$($dimension),+>;
kind: $kind;
units {
$($(#[$unit_attr])* @$unit: $($conversion),+; $abbreviation, $singular, $plural;)+
}
}

/// Trait to identify [units][units] which have a [conversion factor][factor] for the
/// `Quantity`. See [`Conversion<V>`](../../trait.Conversion.html).
///
/// [units]: http://jcgm.bipm.org/vim/en/1.13.html
/// [factor]: https://jcgm.bipm.org/vim/en/1.24.html
pub trait Conversion<V>: Unit + $crate::Conversion<V, T = <V as $crate::Conversion<V>>::T>
+ super::$alias_of::Unit
where
V: $crate::Conversion<V>,
{
}

$(impl super::$alias_of::Unit for $unit {})+
};
(@common
$(#[$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),+, $kind>;

$(#[$quantity_attr])*
pub type $quantity<U, V> = super::Quantity<Dimension, U, V>;

/// Marker trait to identify measurement units for the quantity. See
/// [`Unit`](../trait.Unit.html).
pub trait Unit: super::Unit {}

$(quantity!(@unit $(#[$unit_attr])* @$unit);

impl super::Unit for $unit {
#[inline(always)]
fn abbreviation() -> &'static str {
$abbreviation
}

#[inline(always)]
fn singular() -> &'static str {
$singular
}

#[inline(always)]
fn plural() -> &'static str {
$plural
}
}

impl Unit for $unit {})+

storage_types! {
types: Float;

$(impl $crate::Conversion<V> for super::$unit {
type T = V;

#[inline(always)]
fn coefficient() -> Self::T {
quantity!(@coefficient $($conversion),+)
}

#[inline(always)]
fn constant() -> Self::T {
quantity!(@constant $($conversion),+)
}
}

impl super::Conversion<V> for super::$unit {})+
}

storage_types! {
types: PrimInt, BigInt;
pub type T = $crate::num::rational::Ratio<V>;

#[inline(always)]
fn from_f64(value: f64) -> T {
<T as $crate::num::FromPrimitive>::from_f64(value).unwrap()
}

$(impl $crate::Conversion<V> for super::$unit {
type T = T;

#[inline(always)]
fn coefficient() -> Self::T {
from_f64(quantity!(@coefficient $($conversion),+))
}

#[inline(always)]
fn constant() -> Self::T {
from_f64(quantity!(@constant $($conversion),+))
}
}

impl super::Conversion<V> for super::$unit {})+
}

storage_types! {
types: BigUint;
pub type T = $crate::num::rational::Ratio<V>;

#[inline(always)]
fn from_f64(value: f64) -> T {
use $crate::num::FromPrimitive;

let c = $crate::num::rational::Ratio::<$crate::num::BigInt>::from_f64(value)
.unwrap();

T::new(c.numer().to_biguint().unwrap(), c.denom().to_biguint().unwrap())
}

$(impl $crate::Conversion<V> for super::$unit {
type T = T;

#[inline(always)]
fn coefficient() -> Self::T {
from_f64(quantity!(@coefficient $($conversion),+))
}

#[inline(always)]
fn constant() -> Self::T {
from_f64(quantity!(@constant $($conversion),+))
}
}

impl super::Conversion<V> for super::$unit {})+
}

storage_types! {
types: Ratio;

#[inline(always)]
fn from_f64(value: f64) -> V {
<V as $crate::num::FromPrimitive>::from_f64(value).unwrap()
}

$(impl $crate::Conversion<V> for super::$unit {
type T = V;

#[inline(always)]
fn coefficient() -> Self::T {
from_f64(quantity!(@coefficient $($conversion),+))
}

#[inline(always)]
fn constant() -> Self::T {
from_f64(quantity!(@constant $($conversion),+))
}
}

impl super::Conversion<V> for super::$unit {})+
}

/// Quantity description.
#[allow(dead_code)]
#[inline(always)]
pub fn description() -> &'static str {
$description
}
};
(@unit $(#[$unit_attr:meta])+ @$unit:ident) => {
$(#[$unit_attr])*
#[allow(non_camel_case_types)]
Expand Down
1 change: 1 addition & 0 deletions src/si/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ system! {
temperature_interval::TemperatureInterval,
thermodynamic_temperature::ThermodynamicTemperature,
time::Time,
torque::Torque,
velocity::Velocity,
volume::Volume,
volume_rate::VolumeRate,
Expand Down
Loading