global: wip

This commit is contained in:
nym21
2025-05-31 20:45:59 +02:00
parent cfc3081e8a
commit f976f672cf
34 changed files with 2109 additions and 497 deletions

View File

@@ -4,11 +4,12 @@ use std::{
ops::{Add, AddAssign, Div, Mul},
};
use byteview::ByteView;
use derive_deref::Deref;
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::CheckedSub;
use crate::{CheckedSub, Error, copy_first_8bytes};
use super::{Bitcoin, Cents, Close, Sats, StoredF32, StoredF64};
@@ -133,12 +134,18 @@ impl Div<Bitcoin> for Dollars {
impl Mul<Bitcoin> for Dollars {
type Output = Self;
fn mul(self, rhs: Bitcoin) -> Self::Output {
self * Sats::from(rhs)
}
}
impl Mul<Sats> for Dollars {
type Output = Self;
fn mul(self, rhs: Sats) -> Self::Output {
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),
u128::from(rhs) * u128::from(Cents::from(self)) / u128::from(Sats::ONE_BTC),
))
}
}
@@ -248,3 +255,23 @@ impl Ord for Dollars {
}
}
}
impl TryFrom<ByteView> for Dollars {
type Error = Error;
fn try_from(value: ByteView) -> Result<Self, Self::Error> {
let bytes = copy_first_8bytes(&value)?;
Ok(Self::from(f64::from_be_bytes(bytes)))
}
}
impl From<Dollars> for ByteView {
fn from(value: Dollars) -> Self {
Self::from(&value)
}
}
impl From<&Dollars> for ByteView {
fn from(value: &Dollars) -> Self {
Self::new(&value.to_be_bytes())
}
}

View File

@@ -4,10 +4,11 @@ use std::{
};
use bitcoin::Amount;
use byteview::ByteView;
use serde::Serialize;
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::CheckedSub;
use crate::{CheckedSub, Error, copy_first_8bytes};
use super::{Bitcoin, Cents, Dollars, Height};
@@ -187,3 +188,23 @@ impl From<Sats> for u128 {
value.0 as u128
}
}
impl TryFrom<ByteView> for Sats {
type Error = Error;
fn try_from(value: ByteView) -> Result<Self, Self::Error> {
let bytes = copy_first_8bytes(&value)?;
Ok(Self::from(u64::from_be_bytes(bytes)))
}
}
impl From<&Sats> for ByteView {
fn from(value: &Sats) -> Self {
Self::new(&value.0.to_be_bytes())
}
}
impl From<Sats> for ByteView {
fn from(value: Sats) -> Self {
Self::from(&value)
}
}