mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-06-13 00:13:31 -07:00
global: from custom unsafe_slice to zerocopy
This commit is contained in:
@@ -1,21 +1,22 @@
|
||||
use bindex::{Addressindex, Txoutindex};
|
||||
use fjall::Slice;
|
||||
use unsafe_slice_serde::UnsafeSliceSerde;
|
||||
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Immutable, IntoBytes, KnownLayout, FromBytes)]
|
||||
pub struct AddressindexTxoutindex {
|
||||
addressindex: Addressindex,
|
||||
_padding: u32,
|
||||
txoutindex: Txoutindex,
|
||||
}
|
||||
|
||||
impl TryFrom<Slice> for AddressindexTxoutindex {
|
||||
type Error = unsafe_slice_serde::Error;
|
||||
type Error = storable_vec::Error;
|
||||
fn try_from(value: Slice) -> Result<Self, Self::Error> {
|
||||
Ok(*Self::unsafe_try_from_slice(&value)?)
|
||||
Ok(Self::read_from_bytes(&value)?)
|
||||
}
|
||||
}
|
||||
impl From<AddressindexTxoutindex> for Slice {
|
||||
fn from(value: AddressindexTxoutindex) -> Self {
|
||||
Self::new(value.unsafe_as_slice())
|
||||
Self::new(value.as_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,11 +2,30 @@ use std::ops::Add;
|
||||
|
||||
use bindex::Timestamp;
|
||||
use color_eyre::eyre::eyre;
|
||||
use derive_deref::Deref;
|
||||
use jiff::{civil::Date as _Date, tz::TimeZone, Span};
|
||||
use jiff::{civil::Date as Date_, tz::TimeZone, Span};
|
||||
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Deref)]
|
||||
pub struct Date(_Date);
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, FromBytes, Immutable, IntoBytes, KnownLayout)]
|
||||
pub struct Date {
|
||||
year: u16,
|
||||
month: u8,
|
||||
day: u8,
|
||||
}
|
||||
|
||||
impl Date {
|
||||
const INDEX_ZERO: Self = Self {
|
||||
year: 2009,
|
||||
month: 1,
|
||||
day: 3,
|
||||
};
|
||||
const INDEX_ZERO_: Date_ = Date_::constant(2009, 1, 3);
|
||||
const INDEX_ONE: Self = Self {
|
||||
year: 2009,
|
||||
month: 1,
|
||||
day: 9,
|
||||
};
|
||||
const INDEX_ONE_: Date_ = Date_::constant(2009, 1, 9);
|
||||
}
|
||||
|
||||
impl Default for Date {
|
||||
fn default() -> Self {
|
||||
@@ -14,49 +33,67 @@ impl Default for Date {
|
||||
}
|
||||
}
|
||||
|
||||
impl Date {
|
||||
const INDEX_ZERO: Self = Self(_Date::constant(2009, 1, 3));
|
||||
const INDEX_ONE: Self = Self(_Date::constant(2009, 1, 9));
|
||||
}
|
||||
|
||||
impl From<_Date> for Date {
|
||||
fn from(value: _Date) -> Self {
|
||||
Self(value)
|
||||
impl PartialOrd for Date {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&Timestamp> for Date {
|
||||
fn from(value: &Timestamp) -> Self {
|
||||
Self(_Date::from(value.to_zoned(TimeZone::UTC)))
|
||||
impl Ord for Date {
|
||||
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
||||
Date_::from(*self).cmp(&Date_::from(*other))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Date_> for Date {
|
||||
fn from(value: Date_) -> Self {
|
||||
Self {
|
||||
year: value.year() as u16,
|
||||
month: value.month() as u8,
|
||||
day: value.day() as u8,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Date> for Date_ {
|
||||
fn from(value: Date) -> Self {
|
||||
Self::new(value.year as i16, value.month as i8, value.day as i8).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Timestamp> for Date {
|
||||
fn from(value: Timestamp) -> Self {
|
||||
Self::from(Date_::from(jiff::Timestamp::from(value).to_zoned(TimeZone::UTC)))
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<Date> for usize {
|
||||
type Error = color_eyre::Report;
|
||||
fn try_from(value: Date) -> Result<Self, Self::Error> {
|
||||
if value < Date::INDEX_ZERO {
|
||||
let value_ = Date_::from(value);
|
||||
if value_ < Date::INDEX_ZERO_ {
|
||||
Err(eyre!("Date is too early"))
|
||||
} else if value == Date::INDEX_ZERO {
|
||||
Ok(0)
|
||||
} else if value < Date::INDEX_ONE {
|
||||
} else if value_ < Date::INDEX_ONE_ {
|
||||
Err(eyre!("Date is between first and second"))
|
||||
} else if value == Date::INDEX_ONE {
|
||||
Ok(1)
|
||||
} else {
|
||||
Ok(Date::INDEX_ONE.until(*value)?.get_days() as usize + 1)
|
||||
Ok(Date_::from(Date::INDEX_ONE).until(value_)?.get_days() as usize + 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<usize> for Date {
|
||||
fn from(value: usize) -> Self {
|
||||
Self(Self::INDEX_ZERO.checked_add(Span::new().days(value as i64)).unwrap())
|
||||
Self::from(Self::INDEX_ZERO_.checked_add(Span::new().days(value as i64)).unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
impl Add<usize> for Date {
|
||||
type Output = Self;
|
||||
fn add(self, rhs: usize) -> Self::Output {
|
||||
Self(self.0.checked_add(Span::new().days(rhs as i64)).unwrap())
|
||||
Self::from(Date_::from(self).checked_add(Span::new().days(rhs as i64)).unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user