mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-05-18 22:04:47 -07:00
68 lines
1.3 KiB
Rust
68 lines
1.3 KiB
Rust
use std::{fmt::Debug, ops::Add};
|
|
|
|
use brk_core::{Error, Printable, Result};
|
|
use zerocopy::{Immutable, IntoBytes, KnownLayout, TryFromBytes};
|
|
|
|
pub trait StoredIndex
|
|
where
|
|
Self: Debug
|
|
+ Default
|
|
+ Copy
|
|
+ Clone
|
|
+ PartialEq
|
|
+ Eq
|
|
+ PartialOrd
|
|
+ Ord
|
|
+ TryInto<usize>
|
|
+ From<usize>
|
|
+ Add<usize, Output = Self>
|
|
+ TryFromBytes
|
|
+ IntoBytes
|
|
+ Immutable
|
|
+ KnownLayout
|
|
+ Send
|
|
+ Sync
|
|
+ Printable,
|
|
{
|
|
fn unwrap_to_usize(self) -> usize;
|
|
fn to_usize(self) -> Result<usize>;
|
|
fn decremented(self) -> Option<Self>;
|
|
}
|
|
|
|
impl<I> StoredIndex for I
|
|
where
|
|
I: Debug
|
|
+ Default
|
|
+ Copy
|
|
+ Clone
|
|
+ PartialEq
|
|
+ Eq
|
|
+ PartialOrd
|
|
+ Ord
|
|
+ TryInto<usize>
|
|
+ From<usize>
|
|
+ Add<usize, Output = Self>
|
|
+ TryFromBytes
|
|
+ IntoBytes
|
|
+ Immutable
|
|
+ KnownLayout
|
|
+ Send
|
|
+ Sync
|
|
+ Printable,
|
|
{
|
|
#[inline]
|
|
fn unwrap_to_usize(self) -> usize {
|
|
self.to_usize().unwrap()
|
|
}
|
|
|
|
#[inline]
|
|
fn to_usize(self) -> Result<usize> {
|
|
self.try_into().map_err(|_| Error::FailedKeyTryIntoUsize)
|
|
}
|
|
|
|
#[inline]
|
|
fn decremented(self) -> Option<Self> {
|
|
self.unwrap_to_usize().checked_sub(1).map(Self::from)
|
|
}
|
|
}
|