computer: part 6

This commit is contained in:
nym21
2025-04-07 12:18:18 +02:00
parent 1639df5616
commit 4c7e9fbee2
27 changed files with 887 additions and 341 deletions

View File

@@ -7,8 +7,6 @@ mod bitcoin;
mod blockhash;
mod cents;
mod compressed;
mod counter_u32;
mod counter_u64;
mod date;
mod dateindex;
mod decadeindex;
@@ -22,6 +20,9 @@ mod monthindex;
mod ohlc;
mod quarterindex;
mod sats;
mod stored_u32;
mod stored_u64;
mod stored_usize;
mod timestamp;
mod txid;
mod txindex;
@@ -44,8 +45,6 @@ pub use bitcoin::*;
pub use blockhash::*;
pub use cents::*;
pub use compressed::*;
pub use counter_u32::*;
pub use counter_u64::*;
pub use date::*;
pub use dateindex::*;
pub use decadeindex::*;
@@ -59,6 +58,9 @@ pub use monthindex::*;
pub use ohlc::*;
pub use quarterindex::*;
pub use sats::*;
pub use stored_u32::*;
pub use stored_u64::*;
pub use stored_usize::*;
pub use timestamp::*;
pub use txid::*;
pub use txindex::*;

View File

@@ -21,9 +21,9 @@ use crate::CheckedSub;
KnownLayout,
Serialize,
)]
pub struct CounterU32(u32);
pub struct StoredU32(u32);
impl CounterU32 {
impl StoredU32 {
pub const ZERO: Self = Self(0);
pub fn new(counter: u32) -> Self {
@@ -31,39 +31,39 @@ impl CounterU32 {
}
}
impl From<u32> for CounterU32 {
impl From<u32> for StoredU32 {
fn from(value: u32) -> Self {
Self(value)
}
}
impl From<usize> for CounterU32 {
impl From<usize> for StoredU32 {
fn from(value: usize) -> Self {
Self(value as u32)
}
}
impl CheckedSub<CounterU32> for CounterU32 {
impl CheckedSub<StoredU32> for StoredU32 {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Div<usize> for CounterU32 {
impl Div<usize> for StoredU32 {
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self(self.0 / rhs as u32)
}
}
impl Add for CounterU32 {
impl Add for StoredU32 {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl From<f64> for CounterU32 {
impl From<f64> for StoredU32 {
fn from(value: f64) -> Self {
if value < 0.0 || value > u32::MAX as f64 {
panic!()
@@ -72,8 +72,8 @@ impl From<f64> for CounterU32 {
}
}
impl From<CounterU32> for f64 {
fn from(value: CounterU32) -> Self {
impl From<StoredU32> for f64 {
fn from(value: StoredU32) -> Self {
value.0 as f64
}
}

View File

@@ -23,9 +23,9 @@ use super::{Txinindex, Txoutindex};
KnownLayout,
Serialize,
)]
pub struct CounterU64(u64);
pub struct StoredU64(u64);
impl CounterU64 {
impl StoredU64 {
pub const ZERO: Self = Self(0);
pub fn new(counter: u64) -> Self {
@@ -33,39 +33,39 @@ impl CounterU64 {
}
}
impl From<u64> for CounterU64 {
impl From<u64> for StoredU64 {
fn from(value: u64) -> Self {
Self(value)
}
}
impl From<usize> for CounterU64 {
impl From<usize> for StoredU64 {
fn from(value: usize) -> Self {
Self(value as u64)
}
}
impl CheckedSub<CounterU64> for CounterU64 {
impl CheckedSub<StoredU64> for StoredU64 {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Div<usize> for CounterU64 {
impl Div<usize> for StoredU64 {
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self(self.0 / rhs as u64)
}
}
impl Add for CounterU64 {
impl Add for StoredU64 {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl From<f64> for CounterU64 {
impl From<f64> for StoredU64 {
fn from(value: f64) -> Self {
if value < 0.0 || value > u32::MAX as f64 {
panic!()
@@ -74,19 +74,19 @@ impl From<f64> for CounterU64 {
}
}
impl From<CounterU64> for f64 {
fn from(value: CounterU64) -> Self {
impl From<StoredU64> for f64 {
fn from(value: StoredU64) -> Self {
value.0 as f64
}
}
impl From<Txinindex> for CounterU64 {
impl From<Txinindex> for StoredU64 {
fn from(value: Txinindex) -> Self {
Self(*value)
}
}
impl From<Txoutindex> for CounterU64 {
impl From<Txoutindex> for StoredU64 {
fn from(value: Txoutindex) -> Self {
Self(*value)
}

View File

@@ -0,0 +1,73 @@
use std::ops::{Add, Div};
use derive_deref::Deref;
use serde::Serialize;
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};
use crate::CheckedSub;
#[derive(
Debug,
Deref,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
FromBytes,
Immutable,
IntoBytes,
KnownLayout,
Serialize,
)]
pub struct StoredUsize(usize);
impl StoredUsize {
pub const ZERO: Self = Self(0);
pub fn new(counter: usize) -> Self {
Self(counter)
}
}
impl From<usize> for StoredUsize {
fn from(value: usize) -> Self {
Self(value)
}
}
impl CheckedSub<StoredUsize> for StoredUsize {
fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(rhs.0).map(Self)
}
}
impl Div<usize> for StoredUsize {
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self(self.0 / rhs)
}
}
impl Add for StoredUsize {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl From<f64> for StoredUsize {
fn from(value: f64) -> Self {
if value < 0.0 || value > u32::MAX as f64 {
panic!()
}
Self(value as usize)
}
}
impl From<StoredUsize> for f64 {
fn from(value: StoredUsize) -> Self {
value.0 as f64
}
}

View File

@@ -1,8 +1,24 @@
use std::ops::{Add, Div};
use derive_deref::Deref;
use serde::Serialize;
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};
#[derive(Debug, Deref, Clone, Copy, Immutable, IntoBytes, KnownLayout, FromBytes, Serialize)]
#[derive(
Debug,
Deref,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Immutable,
IntoBytes,
KnownLayout,
FromBytes,
Serialize,
)]
pub struct Weight(u64);
impl From<bitcoin::Weight> for Weight {
@@ -16,3 +32,35 @@ impl From<Weight> for bitcoin::Weight {
Self::from_wu(*value)
}
}
impl From<usize> for Weight {
fn from(value: usize) -> Self {
Self(value as u64)
}
}
impl From<f64> for Weight {
fn from(value: f64) -> Self {
Self(value as u64)
}
}
impl From<Weight> for f64 {
fn from(value: Weight) -> Self {
value.0 as f64
}
}
impl Add for Weight {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl Div<usize> for Weight {
type Output = Self;
fn div(self, rhs: usize) -> Self::Output {
Self::from(self.0 as usize / rhs)
}
}