Skip to content

Commit 7f29068

Browse files
committed
feat: Implement DerefMut to access scale_to and ceil functions
1 parent 0582bdf commit 7f29068

File tree

3 files changed

+52
-13
lines changed

3 files changed

+52
-13
lines changed

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use std::{ops::Deref, str::FromStr};
1+
use std::{
2+
ops::{Deref, DerefMut},
3+
str::FromStr,
4+
};
25

36
use k8s_openapi::apimachinery::pkg::api::resource::Quantity as K8sQuantity;
47

@@ -18,6 +21,12 @@ impl Deref for CpuQuantity {
1821
}
1922
}
2023

24+
impl DerefMut for CpuQuantity {
25+
fn deref_mut(&mut self) -> &mut Self::Target {
26+
&mut self.0
27+
}
28+
}
29+
2130
impl FromStr for CpuQuantity {
2231
type Err = ParseQuantityError;
2332

@@ -27,6 +36,12 @@ impl FromStr for CpuQuantity {
2736
}
2837
}
2938

39+
impl From<CpuQuantity> for K8sQuantity {
40+
fn from(value: CpuQuantity) -> Self {
41+
K8sQuantity(value.to_string())
42+
}
43+
}
44+
3045
forward_from_impls!(Quantity, K8sQuantity, CpuQuantity);
3146
forward_op_impls!(
3247
CpuQuantity(Quantity {

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use std::{ops::Deref, str::FromStr};
1+
use std::{
2+
ops::{Deref, DerefMut},
3+
str::FromStr,
4+
};
25

36
use k8s_openapi::apimachinery::pkg::api::resource::Quantity as K8sQuantity;
47

@@ -26,6 +29,12 @@ impl Deref for MemoryQuantity {
2629
}
2730
}
2831

32+
impl DerefMut for MemoryQuantity {
33+
fn deref_mut(&mut self) -> &mut Self::Target {
34+
&mut self.0
35+
}
36+
}
37+
2938
impl FromStr for MemoryQuantity {
3039
type Err = ParseQuantityError;
3140

@@ -35,6 +44,12 @@ impl FromStr for MemoryQuantity {
3544
}
3645
}
3746

47+
impl From<MemoryQuantity> for K8sQuantity {
48+
fn from(value: MemoryQuantity) -> Self {
49+
K8sQuantity(value.to_string())
50+
}
51+
}
52+
3853
forward_from_impls!(Quantity, K8sQuantity, MemoryQuantity);
3954
forward_op_impls!(
4055
MemoryQuantity(Quantity {

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

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,24 +115,33 @@ impl TryFrom<&K8sQuantity> for Quantity {
115115
}
116116

117117
impl Quantity {
118+
// TODO (@Techassi): Discuss if this should consume or mutate in place. Consumption requires us
119+
// to add these function on specialized quantities (which then forward). If we mutate in place,
120+
// we could leverage the Deref impl instead.
121+
118122
/// Optionally scales up or down to the provided `suffix`.
119123
///
120-
/// It additionally returns `true` if the suffix was scaled, and `false` if it was not. This can
121-
/// be the case if the suffixes already match, the quantity has no suffix, or if the value is 0.
122-
pub fn scale_to(self, suffix: Suffix) -> (Self, bool) {
123-
match (self.value, &self.suffix) {
124-
(_, None) | (0.0, _) => (self, false),
125-
(_, Some(s)) if *s == suffix => (self, false),
124+
/// It additionally returns `true` if the suffix was scaled, and `false` in the following cases:
125+
///
126+
/// - the suffixes already match
127+
/// - the quantity has no suffix, in which case the suffix will be added without scaling
128+
/// - the value is 0
129+
pub fn scale_to(&mut self, suffix: Suffix) -> bool {
130+
match (&mut self.value, &mut self.suffix) {
131+
(0.0, _) => false,
132+
(_, Some(s)) if *s == suffix => false,
133+
(_, None) => {
134+
self.suffix = Some(suffix);
135+
false
136+
}
126137
(v, Some(s)) => {
127138
let factor = (s.base() as f64).powf(s.exponent())
128139
/ (suffix.base() as f64).powf(suffix.exponent());
129140

130-
let quantity = Self {
131-
value: v * factor,
132-
suffix: Some(suffix),
133-
};
141+
*v = *v * factor;
142+
*s = suffix;
134143

135-
(quantity, true)
144+
false
136145
}
137146
}
138147
}

0 commit comments

Comments
 (0)