mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-04-28 08:39:59 -07:00
global: snapshot
This commit is contained in:
@@ -22,6 +22,7 @@ mod quarterindex;
|
||||
mod sats;
|
||||
mod stored_u32;
|
||||
mod stored_u64;
|
||||
mod stored_u8;
|
||||
mod stored_usize;
|
||||
mod timestamp;
|
||||
mod txid;
|
||||
@@ -58,6 +59,7 @@ pub use monthindex::*;
|
||||
pub use ohlc::*;
|
||||
pub use quarterindex::*;
|
||||
pub use sats::*;
|
||||
pub use stored_u8::*;
|
||||
pub use stored_u32::*;
|
||||
pub use stored_u64::*;
|
||||
pub use stored_usize::*;
|
||||
|
||||
79
crates/brk_core/src/structs/stored_u8.rs
Normal file
79
crates/brk_core/src/structs/stored_u8.rs
Normal file
@@ -0,0 +1,79 @@
|
||||
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 StoredU8(u8);
|
||||
|
||||
impl StoredU8 {
|
||||
pub const ZERO: Self = Self(0);
|
||||
|
||||
pub fn new(counter: u8) -> Self {
|
||||
Self(counter)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u8> for StoredU8 {
|
||||
fn from(value: u8) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<usize> for StoredU8 {
|
||||
fn from(value: usize) -> Self {
|
||||
Self(value as u8)
|
||||
}
|
||||
}
|
||||
|
||||
impl CheckedSub<StoredU8> for StoredU8 {
|
||||
fn checked_sub(self, rhs: Self) -> Option<Self> {
|
||||
self.0.checked_sub(rhs.0).map(Self)
|
||||
}
|
||||
}
|
||||
|
||||
impl Div<usize> for StoredU8 {
|
||||
type Output = Self;
|
||||
fn div(self, rhs: usize) -> Self::Output {
|
||||
Self(self.0 / rhs as u8)
|
||||
}
|
||||
}
|
||||
|
||||
impl Add for StoredU8 {
|
||||
type Output = Self;
|
||||
fn add(self, rhs: Self) -> Self::Output {
|
||||
Self(self.0 + rhs.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<f64> for StoredU8 {
|
||||
fn from(value: f64) -> Self {
|
||||
if value < 0.0 || value > u32::MAX as f64 {
|
||||
panic!()
|
||||
}
|
||||
Self(value as u8)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<StoredU8> for f64 {
|
||||
fn from(value: StoredU8) -> Self {
|
||||
value.0 as f64
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,8 @@ use derive_deref::Deref;
|
||||
use serde::Serialize;
|
||||
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};
|
||||
|
||||
use super::StoredU8;
|
||||
|
||||
#[derive(Debug, Deref, Clone, Copy, Immutable, IntoBytes, KnownLayout, FromBytes, Serialize)]
|
||||
pub struct TxVersion(i32);
|
||||
|
||||
@@ -16,3 +18,9 @@ impl From<TxVersion> for bitcoin::transaction::Version {
|
||||
Self(value.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<TxVersion> for StoredU8 {
|
||||
fn from(value: TxVersion) -> Self {
|
||||
Self::from(value.0 as u8)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user