|
| 1 | +/// This macro is intended to be used to implement the From and TryFrom traits on specialized |
| 2 | +/// quantities. |
| 3 | +/// |
| 4 | +/// Currently two specialized quantities exist: [`MemoryQuantity`][1] and [`CpuQuantity`][2]. |
| 5 | +/// The traits are implemented by forwarding to the inner [`Quantity`][3] implementation. Both |
| 6 | +/// specialized quantities are just newtypes / wrappers around [`Quantity`][3]. |
| 7 | +/// |
| 8 | +/// [1]: super::MemoryQuantity |
| 9 | +/// [2]: super::CpuQuantity |
| 10 | +/// [3]: super::Quantity |
| 11 | +macro_rules! forward_from_impls { |
| 12 | + ($q:ty, $kq:ty, $for:ty) => { |
| 13 | + impl From<$q> for $for { |
| 14 | + fn from(quantity: $q) -> Self { |
| 15 | + Self(quantity) |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + impl TryFrom<$kq> for $for { |
| 20 | + type Error = ParseQuantityError; |
| 21 | + |
| 22 | + fn try_from(value: $kq) -> Result<Self, Self::Error> { |
| 23 | + Ok(Self(Quantity::try_from(value)?)) |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + impl TryFrom<&$kq> for $for { |
| 28 | + type Error = ParseQuantityError; |
| 29 | + |
| 30 | + fn try_from(value: &$kq) -> Result<Self, Self::Error> { |
| 31 | + Ok(Self(Quantity::try_from(value)?)) |
| 32 | + } |
| 33 | + } |
| 34 | + }; |
| 35 | +} |
| 36 | + |
| 37 | +macro_rules! forward_op_impls { |
| 38 | + ($acc:expr, $for:ty, $($on:ty),+) => { |
| 39 | + impl ::std::ops::Add for $for { |
| 40 | + type Output = $for; |
| 41 | + |
| 42 | + fn add(self, rhs: $for) -> Self::Output { |
| 43 | + Self(self.0 + rhs.0) |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + impl ::std::ops::AddAssign for $for { |
| 48 | + fn add_assign(&mut self, rhs: $for) { |
| 49 | + self.0 += rhs.0 |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + impl ::std::ops::Sub for $for { |
| 54 | + type Output = $for; |
| 55 | + |
| 56 | + fn sub(self, rhs: $for) -> Self::Output { |
| 57 | + Self(self.0 - rhs.0) |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + impl ::std::ops::SubAssign for $for { |
| 62 | + fn sub_assign(&mut self, rhs: $for) { |
| 63 | + self.0 -= rhs.0 |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + impl ::std::ops::Div for $for { |
| 68 | + type Output = f64; |
| 69 | + |
| 70 | + fn div(self, rhs: $for) -> Self::Output { |
| 71 | + self.0 / rhs.0 |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + impl ::std::iter::Sum for $for { |
| 76 | + fn sum<I: Iterator<Item = Self>>(iter: I) -> Self { |
| 77 | + iter.fold( |
| 78 | + $acc, |
| 79 | + <$for as ::std::ops::Add>::add, |
| 80 | + ) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + $( |
| 85 | + impl ::std::ops::Mul<$on> for $for { |
| 86 | + type Output = $for; |
| 87 | + |
| 88 | + fn mul(self, rhs: $on) -> Self::Output { |
| 89 | + Self(self.0 * rhs) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + impl ::std::ops::MulAssign<$on> for $for { |
| 94 | + fn mul_assign(&mut self, rhs: $on) { |
| 95 | + self.0 *= rhs |
| 96 | + } |
| 97 | + } |
| 98 | + )* |
| 99 | + }; |
| 100 | +} |
| 101 | + |
| 102 | +/// HACK: Make the macros only available in this crate. |
| 103 | +pub(crate) use forward_from_impls; |
| 104 | +pub(crate) use forward_op_impls; |
0 commit comments