global: multiple fixes

This commit is contained in:
nym21
2025-06-07 09:30:42 +02:00
parent cc0f9c42df
commit 51bcbeb48f
28 changed files with 591 additions and 560 deletions

View File

@@ -35,9 +35,10 @@ impl Mul<usize> for Bitcoin {
}
impl Div<Bitcoin> for Bitcoin {
type Output = Self;
type Output = StoredF64;
fn div(self, rhs: Bitcoin) -> Self::Output {
Self::from(Sats::from(self) / Sats::from(rhs))
StoredF64::from(self.0 / rhs.0)
// Self::from(Sats::from(self) / Sats::from(rhs))
}
}

View File

@@ -24,6 +24,12 @@ use super::Dollars;
)]
pub struct Cents(i64);
impl Cents {
pub const fn mint(value: i64) -> Self {
Self(value)
}
}
impl From<Dollars> for Cents {
fn from(value: Dollars) -> Self {
Self((*value * 100.0).round() as i64)
@@ -48,6 +54,15 @@ impl From<u64> for Cents {
}
}
impl From<Cents> for usize {
fn from(value: Cents) -> Self {
if value.0 < 0 {
panic!()
}
value.0 as usize
}
}
impl From<usize> for Cents {
fn from(value: usize) -> Self {
Self(value as i64)
@@ -76,6 +91,13 @@ impl Add for Cents {
}
}
impl Div<Cents> for Cents {
type Output = Self;
fn div(self, rhs: Self) -> Self::Output {
Self(self.0 / rhs.0)
}
}
impl Div<usize> for Cents {
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {

View File

@@ -4,9 +4,10 @@ use std::{
ops::{Add, AddAssign, Div, Mul},
};
use bincode::{Decode, Encode};
use byteview::ByteView;
use derive_deref::Deref;
use serde::Serialize;
use serde::{Deserialize, Serialize};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::{CheckedSub, copy_first_8bytes};
@@ -14,7 +15,19 @@ use crate::{CheckedSub, copy_first_8bytes};
use super::{Bitcoin, Cents, Close, High, Sats, StoredF32, StoredF64};
#[derive(
Debug, Default, Clone, Copy, Deref, FromBytes, Immutable, IntoBytes, KnownLayout, Serialize,
Debug,
Default,
Clone,
Copy,
Deref,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
Deserialize,
Encode,
Decode,
)]
pub struct Dollars(f64);
@@ -85,10 +98,10 @@ impl Add for Dollars {
impl Div<Dollars> for Dollars {
type Output = StoredF64;
fn div(self, rhs: Dollars) -> Self::Output {
if self.is_nan() {
StoredF64::from(self.0)
if self.is_nan() || rhs == Dollars::ZERO {
StoredF64::NAN
} else {
StoredF64::from(self.0 / rhs.0)
StoredF64::from(f64::from(self) / f64::from(rhs))
}
}
}
@@ -96,10 +109,10 @@ impl Div<Dollars> for Dollars {
impl Div<Close<Dollars>> for Dollars {
type Output = StoredF64;
fn div(self, rhs: Close<Dollars>) -> Self::Output {
if self.is_nan() {
StoredF64::from(self.0)
if self.is_nan() || *rhs == Dollars::ZERO {
StoredF64::NAN
} else {
StoredF64::from(self.0 / rhs.0)
StoredF64::from(f64::from(self) / f64::from(*rhs))
}
}
}
@@ -107,10 +120,10 @@ impl Div<Close<Dollars>> for Dollars {
impl Div<Dollars> for Close<Dollars> {
type Output = StoredF64;
fn div(self, rhs: Dollars) -> Self::Output {
if self.is_nan() {
StoredF64::from(self.0)
if self.is_nan() || rhs == Dollars::ZERO {
StoredF64::NAN
} else {
StoredF64::from(self.0 / rhs.0)
StoredF64::from(f64::from(*self) / f64::from(rhs))
}
}
}
@@ -118,8 +131,8 @@ impl Div<Dollars> for Close<Dollars> {
impl Div<usize> for Dollars {
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
if self.is_nan() {
self
if self.is_nan() || rhs == 0 {
Dollars::NAN
} else {
Self::from(Cents::from(self) / rhs)
}
@@ -158,6 +171,13 @@ impl Mul<Dollars> for Close<Dollars> {
}
}
impl Mul<usize> for Close<Dollars> {
type Output = Dollars;
fn mul(self, rhs: usize) -> Self::Output {
Dollars::from(Cents::from(*self) * rhs)
}
}
impl Mul<Bitcoin> for Dollars {
type Output = Self;
fn mul(self, rhs: Bitcoin) -> Self::Output {

View File

@@ -3,9 +3,10 @@ use std::{
ops::{Add, AddAssign, Div, Mul, SubAssign},
};
use bincode::{Decode, Encode};
use bitcoin::Amount;
use byteview::ByteView;
use serde::Serialize;
use serde::{Deserialize, Serialize};
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::{CheckedSub, copy_first_8bytes};
@@ -26,6 +27,9 @@ use super::{Bitcoin, Cents, Dollars, Height};
IntoBytes,
KnownLayout,
Serialize,
Deserialize,
Encode,
Decode,
)]
pub struct Sats(u64);

View File

@@ -76,6 +76,12 @@ impl From<StoredF32> for f32 {
}
}
impl From<Dollars> for StoredF32 {
fn from(value: Dollars) -> Self {
StoredF32::from(f64::from(value))
}
}
impl Div<Dollars> for StoredF32 {
type Output = Self;
fn div(self, rhs: Dollars) -> Self::Output {

View File

@@ -1,5 +1,6 @@
use std::{
cmp::Ordering,
f64,
ops::{Add, Div, Mul},
};
@@ -7,13 +8,17 @@ use derive_deref::Deref;
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::{Bitcoin, CheckedSub, Sats};
use crate::{Bitcoin, CheckedSub, Dollars};
#[derive(
Debug, Deref, Default, Clone, Copy, FromBytes, Immutable, IntoBytes, KnownLayout, Serialize,
)]
pub struct StoredF64(f64);
impl StoredF64 {
pub const NAN: Self = Self(f64::NAN);
}
impl From<f64> for StoredF64 {
fn from(value: f64) -> Self {
Self(value)
@@ -59,6 +64,12 @@ impl From<StoredF64> for f64 {
}
}
impl From<Dollars> for StoredF64 {
fn from(value: Dollars) -> Self {
Self(f64::from(value))
}
}
impl CheckedSub<usize> for StoredF64 {
fn checked_sub(self, rhs: usize) -> Option<Self> {
Some(Self(self.0 - rhs as f64))
@@ -97,12 +108,6 @@ impl Ord for StoredF64 {
}
}
impl From<Sats> for StoredF64 {
fn from(value: Sats) -> Self {
Self(u64::from(value) as f64)
}
}
impl From<Bitcoin> for StoredF64 {
fn from(value: Bitcoin) -> Self {
Self(f64::from(value))