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

RFC: Add a zero constant to quantities based on primitive types. #250

Merged
merged 1 commit into from
Apr 14, 2021
Merged
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
22 changes: 22 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,20 @@ pub trait ConversionFactor<V>:
fn value(self) -> V;
}

/// Helper trait to identify the zero value of a type at compile time.
///
#[cfg_attr(all(feature = "si", feature = "f32"), doc = " ```rust")]
#[cfg_attr(not(all(feature = "si", feature = "f32")), doc = " ```rust,ignore")]
/// # use uom::si::f32::Length;
/// use uom::ConstZero;
///
/// const ORIGIN: (Length, Length, Length) = (Length::ZERO, Length::ZERO, Length::ZERO);
/// ```
pub trait ConstZero {
/// Constant representing the zero value.
const ZERO: Self;
}

/// Default [kind][kind] of quantities to allow addition, subtraction, multiplication, division,
/// remainder, negation, and saturating addition/subtraction.
///
Expand Down Expand Up @@ -507,6 +521,10 @@ storage_types! {
self
}
}

impl crate::ConstZero for V {
const ZERO: Self = 0.0;
}
}

storage_types! {
Expand All @@ -532,6 +550,10 @@ storage_types! {
self.to_integer()
}
}

impl crate::ConstZero for V {
const ZERO: Self = 0;
}
}

storage_types! {
Expand Down
13 changes: 13 additions & 0 deletions src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1288,6 +1288,19 @@ macro_rules! system {
}
}

impl<D, U, V> $crate::ConstZero for Quantity<D, U, V>
where
D: Dimension + ?Sized,
U: Units<V> + ?Sized,
V: $crate::num::Num + $crate::Conversion<V> + $crate::ConstZero,
{
const ZERO: Self = Self {
dimension: $crate::lib::marker::PhantomData,
units: $crate::lib::marker::PhantomData,
value: V::ZERO,
};
}

serde! {
impl<D, U, V> $crate::serde::Serialize for Quantity<D, U, V>
where
Expand Down