global: utxos part 8

This commit is contained in:
nym21
2025-05-25 00:27:18 +02:00
parent 4ab10670c9
commit 4217c22ff6
14 changed files with 1863 additions and 814 deletions

View File

@@ -1,4 +1,5 @@
use std::{
cmp::Ordering,
f64,
ops::{Add, AddAssign, Div, Mul},
};
@@ -88,35 +89,55 @@ impl Add for Dollars {
impl Div<Dollars> for Dollars {
type Output = StoredF64;
fn div(self, rhs: Dollars) -> Self::Output {
StoredF64::from(self.0 / rhs.0)
if self.is_nan() {
StoredF64::from(self.0)
} else {
StoredF64::from(self.0 / rhs.0)
}
}
}
impl Div<Close<Dollars>> for Dollars {
type Output = StoredF64;
fn div(self, rhs: Close<Dollars>) -> Self::Output {
StoredF64::from(self.0 / rhs.0)
if self.is_nan() {
StoredF64::from(self.0)
} else {
StoredF64::from(self.0 / rhs.0)
}
}
}
impl Div<Dollars> for Close<Dollars> {
type Output = StoredF64;
fn div(self, rhs: Dollars) -> Self::Output {
StoredF64::from(self.0 / rhs.0)
if self.is_nan() {
StoredF64::from(self.0)
} else {
StoredF64::from(self.0 / rhs.0)
}
}
}
impl Div<usize> for Dollars {
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self::from(Cents::from(self) / rhs)
if self.is_nan() {
self
} else {
Self::from(Cents::from(self) / rhs)
}
}
}
impl Div<Bitcoin> for Dollars {
type Output = Self;
fn div(self, rhs: Bitcoin) -> Self::Output {
Self(f64::from(self) / f64::from(rhs))
if self.is_nan() {
self
} else {
Self(f64::from(self) / f64::from(rhs))
}
}
}
@@ -125,16 +146,26 @@ impl Eq for Dollars {}
#[allow(clippy::derive_ord_xor_partial_ord)]
impl Ord for Dollars {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.0.partial_cmp(&other.0).unwrap()
match (self.0.is_nan(), other.0.is_nan()) {
(true, true) => Ordering::Equal,
(true, false) => Ordering::Less,
(false, true) => Ordering::Greater,
(false, false) => self.0.partial_cmp(&other.0).unwrap(),
}
}
}
impl Mul<Bitcoin> for Dollars {
type Output = Self;
fn mul(self, rhs: Bitcoin) -> Self::Output {
Self::from(Cents::from(
u128::from(Sats::from(rhs)) * u128::from(Cents::from(self)) / u128::from(Sats::ONE_BTC),
))
if self.is_nan() {
self
} else {
Self::from(Cents::from(
u128::from(Sats::from(rhs)) * u128::from(Cents::from(self))
/ u128::from(Sats::ONE_BTC),
))
}
}
}
@@ -142,7 +173,7 @@ impl Mul<StoredF32> for Dollars {
type Output = Self;
fn mul(self, rhs: StoredF32) -> Self::Output {
if rhs.is_nan() {
Self(f64::NAN)
self
} else {
Self::from(Cents::from(Self::from(self.0 * *rhs as f64)))
}
@@ -152,7 +183,11 @@ impl Mul<StoredF32> for Dollars {
impl Mul<usize> for Dollars {
type Output = Self;
fn mul(self, rhs: usize) -> Self::Output {
Self::from(Cents::from(self) * rhs)
if self.is_nan() {
self
} else {
Self::from(Cents::from(self) * rhs)
}
}
}
@@ -182,9 +217,13 @@ impl AddAssign for Dollars {
impl CheckedSub for Dollars {
fn checked_sub(self, rhs: Self) -> Option<Self> {
Cents::from(self)
.checked_sub(Cents::from(rhs))
.map(Dollars::from)
if self.is_nan() {
Some(self)
} else {
Cents::from(self)
.checked_sub(Cents::from(rhs))
.map(Dollars::from)
}
}
}

View File

@@ -1,4 +1,7 @@
use std::ops::{Add, Div, Mul, Sub};
use std::{
cmp::Ordering,
ops::{Add, Div, Mul, Sub},
};
use derive_deref::Deref;
use serde::Serialize;
@@ -79,7 +82,12 @@ impl Eq for StoredF32 {}
#[allow(clippy::derive_ord_xor_partial_ord)]
impl Ord for StoredF32 {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.0.partial_cmp(&other.0).unwrap()
match (self.0.is_nan(), other.0.is_nan()) {
(true, true) => Ordering::Equal,
(true, false) => Ordering::Less,
(false, true) => Ordering::Greater,
(false, false) => self.0.partial_cmp(&other.0).unwrap(),
}
}
}

View File

@@ -1,4 +1,7 @@
use std::ops::{Add, Div, Mul};
use std::{
cmp::Ordering,
ops::{Add, Div, Mul},
};
use derive_deref::Deref;
use serde::Serialize;
@@ -72,7 +75,12 @@ impl Eq for StoredF64 {}
#[allow(clippy::derive_ord_xor_partial_ord)]
impl Ord for StoredF64 {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.0.partial_cmp(&other.0).unwrap()
match (self.0.is_nan(), other.0.is_nan()) {
(true, true) => Ordering::Equal,
(true, false) => Ordering::Less,
(false, true) => Ordering::Greater,
(false, false) => self.0.partial_cmp(&other.0).unwrap(),
}
}
}