commit 3a5d4099dfa0d9782503dd03447d84de12853de1
parent 95f68d984566240e7606df863ec61460e1b8ae93
Author: Antoine A <>
Date: Wed, 18 Dec 2024 15:10:05 +0100
taler-common: improve amount memory layout
Diffstat:
3 files changed, 26 insertions(+), 17 deletions(-)
diff --git a/Cargo.lock b/Cargo.lock
@@ -2145,7 +2145,7 @@ dependencies = [
"serde_urlencoded",
"serde_with",
"sqlx",
- "thiserror 2.0.7",
+ "thiserror 2.0.8",
"url",
]
@@ -2185,11 +2185,11 @@ dependencies = [
[[package]]
name = "thiserror"
-version = "2.0.7"
+version = "2.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "93605438cbd668185516ab499d589afb7ee1859ea3d5fc8f6b0755e1c7443767"
+checksum = "08f5383f3e0071702bf93ab5ee99b52d26936be9dedd9413067cbdcddcb6141a"
dependencies = [
- "thiserror-impl 2.0.7",
+ "thiserror-impl 2.0.8",
]
[[package]]
@@ -2205,9 +2205,9 @@ dependencies = [
[[package]]
name = "thiserror-impl"
-version = "2.0.7"
+version = "2.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "e1d8749b4531af2117677a5fcd12b1348a3fe2b81e36e61ffeac5c4aa3273e36"
+checksum = "f2f357fcec90b3caef6623a099691be676d033b40a058ac95d2a6ade6fa0c943"
dependencies = [
"proc-macro2",
"quote",
diff --git a/taler-api/src/db.rs b/taler-api/src/db.rs
@@ -124,7 +124,7 @@ pub trait BindHelper {
impl<'q> BindHelper for Query<'q, Postgres, <Postgres as sqlx::Database>::Arguments<'q>> {
fn bind_amount(self, amount: &Amount) -> Self {
- self.bind_decimal(&amount.decimal)
+ self.bind_decimal(&amount.decimal())
}
fn bind_decimal(self, decimal: &Decimal) -> Self {
diff --git a/taler-common/src/types/amount.rs b/taler-common/src/types/amount.rs
@@ -216,7 +216,8 @@ impl Display for Decimal {
)]
pub struct Amount {
pub currency: Currency,
- pub decimal: Decimal,
+ pub val: u64,
+ pub frac: u32,
}
impl Amount {
@@ -237,23 +238,31 @@ impl Amount {
Self::new_decimal(currency, Decimal::zero())
}
- fn normalize(mut self) -> Option<Self> {
- self.decimal = self.decimal.normalize()?;
- Some(self)
+ pub const fn decimal(&self) -> Decimal {
+ Decimal {
+ val: self.val,
+ frac: self.frac,
+ }
}
- pub fn add(mut self, rhs: &Self) -> Option<Self> {
+ pub fn normalize(self) -> Option<Self> {
+ let decimal = self.decimal().normalize()?;
+ Some((self.currency, decimal).into())
+ }
+
+ pub fn add(self, rhs: &Self) -> Option<Self> {
assert_eq!(self.currency, rhs.currency);
- self.decimal = self.decimal.add(&rhs.decimal)?;
- self.normalize()
+ let decimal = self.decimal().add(&rhs.decimal())?.normalize()?;
+ Some((self.currency, decimal).into())
}
}
impl From<(Currency, Decimal)> for Amount {
- fn from((currency, amount): (Currency, Decimal)) -> Self {
+ fn from((currency, decimal): (Currency, Decimal)) -> Self {
Self {
currency,
- decimal: amount,
+ val: decimal.val,
+ frac: decimal.frac,
}
}
}
@@ -300,7 +309,7 @@ impl FromStr for Amount {
impl Display for Amount {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- f.write_fmt(format_args!("{}:{}", self.currency, self.decimal))
+ f.write_fmt(format_args!("{}:{}", self.currency, self.decimal()))
}
}