Skip to content

Commit f0aa415

Browse files
authored
Merge pull request #250 from adamreichold/const-zero
RFC: Add a zero constant to quantities based on primitive types.
2 parents e108175 + d8cacec commit f0aa415

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/lib.rs

+22
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,20 @@ pub trait ConversionFactor<V>:
456456
fn value(self) -> V;
457457
}
458458

459+
/// Helper trait to identify the zero value of a type at compile time.
460+
///
461+
#[cfg_attr(all(feature = "si", feature = "f32"), doc = " ```rust")]
462+
#[cfg_attr(not(all(feature = "si", feature = "f32")), doc = " ```rust,ignore")]
463+
/// # use uom::si::f32::Length;
464+
/// use uom::ConstZero;
465+
///
466+
/// const ORIGIN: (Length, Length, Length) = (Length::ZERO, Length::ZERO, Length::ZERO);
467+
/// ```
468+
pub trait ConstZero {
469+
/// Constant representing the zero value.
470+
const ZERO: Self;
471+
}
472+
459473
/// Default [kind][kind] of quantities to allow addition, subtraction, multiplication, division,
460474
/// remainder, negation, and saturating addition/subtraction.
461475
///
@@ -507,6 +521,10 @@ storage_types! {
507521
self
508522
}
509523
}
524+
525+
impl crate::ConstZero for V {
526+
const ZERO: Self = 0.0;
527+
}
510528
}
511529

512530
storage_types! {
@@ -532,6 +550,10 @@ storage_types! {
532550
self.to_integer()
533551
}
534552
}
553+
554+
impl crate::ConstZero for V {
555+
const ZERO: Self = 0;
556+
}
535557
}
536558

537559
storage_types! {

src/system.rs

+13
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,19 @@ macro_rules! system {
12881288
}
12891289
}
12901290

1291+
impl<D, U, V> $crate::ConstZero for Quantity<D, U, V>
1292+
where
1293+
D: Dimension + ?Sized,
1294+
U: Units<V> + ?Sized,
1295+
V: $crate::num::Num + $crate::Conversion<V> + $crate::ConstZero,
1296+
{
1297+
const ZERO: Self = Self {
1298+
dimension: $crate::lib::marker::PhantomData,
1299+
units: $crate::lib::marker::PhantomData,
1300+
value: V::ZERO,
1301+
};
1302+
}
1303+
12911304
serde! {
12921305
impl<D, U, V> $crate::serde::Serialize for Quantity<D, U, V>
12931306
where

0 commit comments

Comments
 (0)