Skip to content

Commit 102d4b6

Browse files
committed
fix: Scale rhs in math ops
1 parent 910a65e commit 102d4b6

File tree

4 files changed

+94
-125
lines changed

4 files changed

+94
-125
lines changed
Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
use std::{
2-
ops::{Deref, DerefMut},
3-
str::FromStr,
2+
iter::Sum,
3+
ops::{Add, Deref},
44
};
55

66
use k8s_openapi::apimachinery::pkg::api::resource::Quantity as K8sQuantity;
77

8-
use crate::quantity::{
9-
macros::{forward_from_impls, forward_op_impls},
10-
DecimalByteMultiple, ParseQuantityError, Quantity, Suffix,
11-
};
8+
use crate::quantity::{macros::forward_quantity_impls, DecimalMultiple, Quantity, Suffix};
129

1310
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
1411
pub struct CpuQuantity(Quantity);
@@ -21,44 +18,35 @@ impl Deref for CpuQuantity {
2118
}
2219
}
2320

24-
impl DerefMut for CpuQuantity {
25-
fn deref_mut(&mut self) -> &mut Self::Target {
26-
&mut self.0
27-
}
28-
}
29-
30-
impl FromStr for CpuQuantity {
31-
type Err = ParseQuantityError;
32-
33-
fn from_str(input: &str) -> Result<Self, Self::Err> {
34-
let quantity = Quantity::from_str(input)?;
35-
Ok(Self(quantity))
36-
}
37-
}
38-
3921
impl From<CpuQuantity> for K8sQuantity {
4022
fn from(value: CpuQuantity) -> Self {
4123
K8sQuantity(value.to_string())
4224
}
4325
}
4426

45-
forward_from_impls!(Quantity, K8sQuantity, CpuQuantity);
46-
forward_op_impls!(
47-
CpuQuantity(Quantity {
48-
value: 0.0,
49-
suffix: None,
50-
}),
51-
CpuQuantity,
52-
usize,
53-
f32,
54-
f64
55-
);
27+
impl Sum for CpuQuantity {
28+
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
29+
iter.fold(
30+
CpuQuantity(Quantity {
31+
value: 0.0,
32+
suffix: Suffix::DecimalMultiple(DecimalMultiple::Empty),
33+
}),
34+
CpuQuantity::add,
35+
)
36+
}
37+
}
38+
39+
forward_quantity_impls!(CpuQuantity, K8sQuantity, usize, f32, f64);
5640

5741
impl CpuQuantity {
5842
pub fn from_millis(value: u32) -> Self {
5943
CpuQuantity(Quantity {
60-
suffix: Some(Suffix::DecimalByteMultiple(DecimalByteMultiple::Milli)),
44+
suffix: Suffix::DecimalMultiple(DecimalMultiple::Milli),
6145
value: value.into(),
6246
})
6347
}
48+
49+
pub fn scale_to(self, suffix: Suffix) -> Self {
50+
Self(self.0.scale_to(suffix))
51+
}
6452
}

crates/stackable-operator/src/quantity/macros.rs

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,37 @@
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
1+
macro_rules! forward_quantity_impls {
2+
($for:ty, $kq:ty, $($on:ty),+) => {
3+
$crate::quantity::macros::forward_from_impls!($for, $kq);
4+
$crate::quantity::macros::forward_op_impls!($for, $($on),*);
5+
};
6+
}
7+
118
macro_rules! forward_from_impls {
12-
($q:ty, $kq:ty, $for:ty) => {
13-
impl From<$q> for $for {
14-
fn from(quantity: $q) -> Self {
9+
($for:ty, $kq:ty) => {
10+
impl ::std::str::FromStr for $for {
11+
type Err = $crate::quantity::ParseQuantityError;
12+
13+
fn from_str(input: &str) -> Result<Self, Self::Err> {
14+
let quantity = $crate::quantity::Quantity::from_str(input)?;
15+
Ok(Self(quantity))
16+
}
17+
}
18+
19+
impl From<$crate::quantity::Quantity> for $for {
20+
fn from(quantity: $crate::quantity::Quantity) -> Self {
1521
Self(quantity)
1622
}
1723
}
1824

1925
impl TryFrom<$kq> for $for {
20-
type Error = ParseQuantityError;
26+
type Error = $crate::quantity::ParseQuantityError;
2127

2228
fn try_from(value: $kq) -> Result<Self, Self::Error> {
23-
Ok(Self(Quantity::try_from(value)?))
29+
Ok(Self($crate::quantity::Quantity::try_from(value)?))
2430
}
2531
}
2632

2733
impl TryFrom<&$kq> for $for {
28-
type Error = ParseQuantityError;
34+
type Error = $crate::quantity::ParseQuantityError;
2935

3036
fn try_from(value: &$kq) -> Result<Self, Self::Error> {
3137
Ok(Self(Quantity::try_from(value)?))
@@ -35,7 +41,7 @@ macro_rules! forward_from_impls {
3541
}
3642

3743
macro_rules! forward_op_impls {
38-
($acc:expr, $for:ty, $($on:ty),+) => {
44+
($for:ty, $($on:ty),+) => {
3945
impl ::std::ops::Add for $for {
4046
type Output = $for;
4147

@@ -72,15 +78,6 @@ macro_rules! forward_op_impls {
7278
}
7379
}
7480

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-
8481
$(
8582
impl ::std::ops::Mul<$on> for $for {
8683
type Output = $for;
@@ -99,6 +96,7 @@ macro_rules! forward_op_impls {
9996
};
10097
}
10198

102-
/// HACK: Make the macros only available in this crate.
99+
// HACK: Make the macros only available in this crate.
103100
pub(crate) use forward_from_impls;
104101
pub(crate) use forward_op_impls;
102+
pub(crate) use forward_quantity_impls;
Lines changed: 37 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,11 @@
11
use std::{
2-
ops::{Deref, DerefMut},
3-
str::FromStr,
2+
iter::Sum,
3+
ops::{Add, Deref},
44
};
55

66
use k8s_openapi::apimachinery::pkg::api::resource::Quantity as K8sQuantity;
77

8-
use crate::quantity::{
9-
macros::{forward_from_impls, forward_op_impls},
10-
BinaryByteMultiple, ParseQuantityError, Quantity, Suffix,
11-
};
12-
13-
pub trait JavaHeap {
14-
// TODO (@Techassi): Add proper error type
15-
/// Formats the [`MemoryQuantity`] so that it can be used as a Java heap value.
16-
///
17-
/// This function can fail, because the [`Quantity`] has to be scaled down to at most
18-
fn to_java_heap_string(&self) -> Result<String, String>;
19-
}
8+
use crate::quantity::{macros::forward_quantity_impls, BinaryMultiple, Quantity, Suffix};
209

2110
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
2211
pub struct MemoryQuantity(Quantity);
@@ -29,53 +18,57 @@ impl Deref for MemoryQuantity {
2918
}
3019
}
3120

32-
impl DerefMut for MemoryQuantity {
33-
fn deref_mut(&mut self) -> &mut Self::Target {
34-
&mut self.0
35-
}
36-
}
37-
38-
impl FromStr for MemoryQuantity {
39-
type Err = ParseQuantityError;
40-
41-
fn from_str(input: &str) -> Result<Self, Self::Err> {
42-
let quantity = Quantity::from_str(input)?;
43-
Ok(Self(quantity))
44-
}
45-
}
46-
4721
impl From<MemoryQuantity> for K8sQuantity {
4822
fn from(value: MemoryQuantity) -> Self {
4923
K8sQuantity(value.to_string())
5024
}
5125
}
5226

53-
forward_from_impls!(Quantity, K8sQuantity, MemoryQuantity);
54-
forward_op_impls!(
55-
MemoryQuantity(Quantity {
56-
value: 0.0,
57-
// TODO (@Techassi): This needs to be talked about. The previous implementation used Kibi
58-
// here. Code which relies on that fact (for later scaling) will thus break.
59-
suffix: None,
60-
}),
61-
MemoryQuantity,
62-
usize,
63-
f32,
64-
f64
65-
);
27+
impl Sum for MemoryQuantity {
28+
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
29+
iter.fold(
30+
MemoryQuantity(Quantity {
31+
value: 0.0,
32+
suffix: Suffix::BinaryMultiple(BinaryMultiple::Kibi),
33+
}),
34+
MemoryQuantity::add,
35+
)
36+
}
37+
}
38+
39+
forward_quantity_impls!(MemoryQuantity, K8sQuantity, usize, f32, f64);
6640

6741
impl MemoryQuantity {
6842
pub const fn from_gibi(value: f64) -> Self {
6943
MemoryQuantity(Quantity {
70-
suffix: Some(Suffix::BinaryByteMultiple(BinaryByteMultiple::Gibi)),
44+
suffix: Suffix::BinaryMultiple(BinaryMultiple::Gibi),
7145
value,
7246
})
7347
}
7448

7549
pub const fn from_mebi(value: f64) -> Self {
7650
MemoryQuantity(Quantity {
77-
suffix: Some(Suffix::BinaryByteMultiple(BinaryByteMultiple::Mebi)),
51+
suffix: Suffix::BinaryMultiple(BinaryMultiple::Mebi),
7852
value,
7953
})
8054
}
55+
56+
pub fn scale_to(self, suffix: Suffix) -> Self {
57+
Self(self.0.scale_to(suffix))
58+
}
59+
60+
pub fn ceil(self) -> Self {
61+
Self(Quantity {
62+
value: self.value.ceil(),
63+
suffix: self.suffix,
64+
})
65+
}
66+
}
67+
68+
pub trait JavaHeap {
69+
// TODO (@Techassi): Add proper error type
70+
/// Formats the [`MemoryQuantity`] so that it can be used as a Java heap value.
71+
///
72+
/// This function can fail, because the [`Quantity`] has to be scaled down to at most
73+
fn to_java_heap_string(&self) -> Result<String, String>;
8174
}

crates/stackable-operator/src/quantity/ops.rs

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
use std::{
2-
iter::Sum,
3-
ops::{Add, AddAssign, Div, Mul, MulAssign, Sub, SubAssign},
4-
};
1+
use std::ops::{Add, AddAssign, Div, Mul, MulAssign, Sub, SubAssign};
52

63
use crate::quantity::Quantity;
74

85
impl Add for Quantity {
96
type Output = Quantity;
107

118
fn add(self, rhs: Quantity) -> Self::Output {
9+
let rhs = rhs.scale_to(self.suffix);
10+
1211
Self {
1312
value: self.value + rhs.value,
1413
..self
@@ -18,14 +17,16 @@ impl Add for Quantity {
1817

1918
impl AddAssign for Quantity {
2019
fn add_assign(&mut self, rhs: Quantity) {
21-
self.value += rhs.value
20+
*self = self.add(rhs)
2221
}
2322
}
2423

2524
impl Sub for Quantity {
2625
type Output = Quantity;
2726

2827
fn sub(self, rhs: Quantity) -> Self::Output {
28+
let rhs = rhs.scale_to(self.suffix);
29+
2930
Self {
3031
value: self.value - rhs.value,
3132
..self
@@ -35,7 +36,7 @@ impl Sub for Quantity {
3536

3637
impl SubAssign for Quantity {
3738
fn sub_assign(&mut self, rhs: Self) {
38-
self.value -= rhs.value
39+
*self = self.sub(rhs)
3940
}
4041
}
4142

@@ -52,7 +53,7 @@ impl Mul<usize> for Quantity {
5253

5354
impl MulAssign<usize> for Quantity {
5455
fn mul_assign(&mut self, rhs: usize) {
55-
self.value *= rhs as f64
56+
*self = self.mul(rhs)
5657
}
5758
}
5859

@@ -69,7 +70,7 @@ impl Mul<f32> for Quantity {
6970

7071
impl MulAssign<f32> for Quantity {
7172
fn mul_assign(&mut self, rhs: f32) {
72-
self.value *= rhs as f64
73+
*self = self.mul(rhs)
7374
}
7475
}
7576

@@ -86,26 +87,15 @@ impl Mul<f64> for Quantity {
8687

8788
impl MulAssign<f64> for Quantity {
8889
fn mul_assign(&mut self, rhs: f64) {
89-
self.value *= rhs
90+
*self = self.mul(rhs)
9091
}
9192
}
9293

9394
impl Div for Quantity {
9495
type Output = f64;
9596

9697
fn div(self, rhs: Self) -> Self::Output {
98+
let rhs = rhs.scale_to(self.suffix);
9799
self.value / rhs.value
98100
}
99101
}
100-
101-
impl Sum for Quantity {
102-
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
103-
iter.fold(
104-
Quantity {
105-
value: 0.0,
106-
suffix: None,
107-
},
108-
Quantity::add,
109-
)
110-
}
111-
}

0 commit comments

Comments
 (0)