mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-07-14 04:28:11 -07:00
global: one big snapshot
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
use std::ops::{Add, AddAssign};
|
||||
|
||||
use brk_vecs::{CheckedSub, Printable, StoredCompressed};
|
||||
use byteview::ByteView;
|
||||
use derive_deref::{Deref, DerefMut};
|
||||
use serde::Serialize;
|
||||
use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
|
||||
|
||||
use crate::copy_first_4bytes;
|
||||
|
||||
use super::StoredU32;
|
||||
|
||||
#[derive(
|
||||
Debug,
|
||||
PartialEq,
|
||||
Eq,
|
||||
PartialOrd,
|
||||
Ord,
|
||||
Clone,
|
||||
Copy,
|
||||
Deref,
|
||||
DerefMut,
|
||||
Default,
|
||||
FromBytes,
|
||||
Immutable,
|
||||
IntoBytes,
|
||||
KnownLayout,
|
||||
Serialize,
|
||||
StoredCompressed,
|
||||
)]
|
||||
pub struct TxIndex(u32);
|
||||
|
||||
impl TxIndex {
|
||||
pub const ZERO: Self = Self(0);
|
||||
|
||||
pub fn new(txindex: u32) -> Self {
|
||||
Self(txindex)
|
||||
}
|
||||
|
||||
pub fn incremented(self) -> Self {
|
||||
Self(*self + 1)
|
||||
}
|
||||
}
|
||||
|
||||
impl Add<TxIndex> for TxIndex {
|
||||
type Output = Self;
|
||||
fn add(self, rhs: TxIndex) -> Self::Output {
|
||||
Self(self.0 + rhs.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl Add<usize> for TxIndex {
|
||||
type Output = Self;
|
||||
fn add(self, rhs: usize) -> Self::Output {
|
||||
Self(self.0 + rhs as u32)
|
||||
}
|
||||
}
|
||||
|
||||
impl AddAssign<TxIndex> for TxIndex {
|
||||
fn add_assign(&mut self, rhs: TxIndex) {
|
||||
self.0 += rhs.0
|
||||
}
|
||||
}
|
||||
|
||||
impl CheckedSub<TxIndex> for TxIndex {
|
||||
fn checked_sub(self, rhs: TxIndex) -> Option<Self> {
|
||||
self.0.checked_sub(rhs.0).map(TxIndex::from)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u32> for TxIndex {
|
||||
fn from(value: u32) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<u64> for TxIndex {
|
||||
fn from(value: u64) -> Self {
|
||||
Self(value as u32)
|
||||
}
|
||||
}
|
||||
impl From<TxIndex> for u64 {
|
||||
fn from(value: TxIndex) -> Self {
|
||||
value.0 as u64
|
||||
}
|
||||
}
|
||||
|
||||
impl From<usize> for TxIndex {
|
||||
fn from(value: usize) -> Self {
|
||||
Self(value as u32)
|
||||
}
|
||||
}
|
||||
impl From<TxIndex> for usize {
|
||||
fn from(value: TxIndex) -> Self {
|
||||
value.0 as usize
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ByteView> for TxIndex {
|
||||
fn from(value: ByteView) -> Self {
|
||||
Self(u32::from_be_bytes(copy_first_4bytes(&value).unwrap()))
|
||||
}
|
||||
}
|
||||
impl From<TxIndex> for ByteView {
|
||||
fn from(value: TxIndex) -> Self {
|
||||
Self::new(&value.to_be_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<TxIndex> for StoredU32 {
|
||||
fn from(value: TxIndex) -> Self {
|
||||
Self::from(value.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl Printable for TxIndex {
|
||||
fn to_string() -> &'static str {
|
||||
"txindex"
|
||||
}
|
||||
|
||||
fn to_possible_strings() -> &'static [&'static str] {
|
||||
&["tx", "txindex"]
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user