qtty_core/macros.rs
1//! Macros for defining units and conversions.
2
3/// Generates `From` trait implementations for all pairs of units within a dimension.
4#[macro_export]
5macro_rules! impl_unit_conversions {
6 // Base case: single unit, no conversions needed
7 ($unit:ty) => {};
8
9 // Recursive case: implement conversions from first to all others, then recurse
10 ($first:ty, $($rest:ty),+ $(,)?) => {
11 $(
12 impl From<$crate::Quantity<$first>> for $crate::Quantity<$rest> {
13 fn from(value: $crate::Quantity<$first>) -> Self {
14 value.to::<$rest>()
15 }
16 }
17
18 impl From<$crate::Quantity<$rest>> for $crate::Quantity<$first> {
19 fn from(value: $crate::Quantity<$rest>) -> Self {
20 value.to::<$first>()
21 }
22 }
23 )+
24
25 // Recurse with the rest of the units
26 $crate::impl_unit_conversions!($($rest),+);
27 };
28}