global: snapshot

This commit is contained in:
nym21
2025-05-28 15:42:55 +02:00
parent 93e01902e3
commit 9bbf3a027f
17 changed files with 1201 additions and 624 deletions

View File

@@ -22,11 +22,11 @@ use super::Dollars;
KnownLayout,
Serialize,
)]
pub struct Cents(u64);
pub struct Cents(i64);
impl From<Dollars> for Cents {
fn from(value: Dollars) -> Self {
Self((*value * 100.0).round() as u64)
Self((*value * 100.0).round() as i64)
}
}
@@ -36,15 +36,36 @@ impl From<Cents> for f64 {
}
}
impl From<i64> for Cents {
fn from(value: i64) -> Self {
Self(value)
}
}
impl From<u64> for Cents {
fn from(value: u64) -> Self {
Self(value)
Self(value as i64)
}
}
impl From<usize> for Cents {
fn from(value: usize) -> Self {
Self(value as i64)
}
}
impl From<Cents> for i64 {
fn from(value: Cents) -> Self {
value.0
}
}
impl From<Cents> for u64 {
fn from(value: Cents) -> Self {
value.0
if value.0 < 0 {
panic!("Shouldn't convert neg cents to u64")
}
value.0 as u64
}
}
@@ -58,21 +79,24 @@ impl Add for Cents {
impl Div<usize> for Cents {
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self(self.0 / rhs as u64)
Self(self.0 / rhs as i64)
}
}
impl From<u128> for Cents {
fn from(value: u128) -> Self {
if value > u64::MAX as u128 {
panic!("u128 bigger than u64")
if value > i64::MAX as u128 {
panic!("u128 bigger than i64")
}
Self(value as u64)
Self(value as i64)
}
}
impl From<Cents> for u128 {
fn from(value: Cents) -> Self {
if value.0 < 0 {
panic!("Shouldn't convert neg cents to u128")
}
value.0 as u128
}
}
@@ -87,7 +111,7 @@ impl Mul<Cents> for Cents {
impl Mul<usize> for Cents {
type Output = Cents;
fn mul(self, rhs: usize) -> Self::Output {
Self(self.0 * rhs as u64)
Self(self.0 * rhs as i64)
}
}

View File

@@ -147,10 +147,17 @@ impl Mul<Bitcoin> for Dollars {
impl Mul<StoredF32> for Dollars {
type Output = Self;
fn mul(self, rhs: StoredF32) -> Self::Output {
self * *rhs as f64
}
}
impl Mul<f64> for Dollars {
type Output = Self;
fn mul(self, rhs: f64) -> Self::Output {
if rhs.is_nan() {
self
} else {
Self::from(Cents::from(Self::from(self.0 * *rhs as f64)))
Self::from(Cents::from(self) * Cents::from(Dollars::from(rhs)))
}
}
}
@@ -204,7 +211,9 @@ impl CheckedSub for Dollars {
impl CheckedSub<usize> for Dollars {
fn checked_sub(self, rhs: usize) -> Option<Self> {
Some(Self(self.0 - rhs as f64))
Some(Dollars::from(
Cents::from(self).checked_sub(Cents::from(rhs)).unwrap(),
))
}
}

View File

@@ -1,3 +1,4 @@
use core::panic;
use std::{
cmp::Ordering,
ops::{Add, Div, Mul, Sub},
@@ -24,10 +25,19 @@ impl From<f32> for StoredF32 {
impl From<f64> for StoredF32 {
fn from(value: f64) -> Self {
if value > f32::MAX as f64 {
panic!("f64 is too big")
}
Self(value as f32)
}
}
impl From<StoredF32> for f64 {
fn from(value: StoredF32) -> Self {
value.0 as f64
}
}
impl From<StoredF64> for StoredF32 {
fn from(value: StoredF64) -> Self {
Self(*value as f32)