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 + From + Add + TryFromBytes + IntoBytes + Immutable + KnownLayout + Send + Sync + Printable, { fn unwrap_to_usize(self) -> usize; fn to_usize(self) -> Result; fn decremented(self) -> Option; } impl StoredIndex for I where I: Debug + Default + Copy + Clone + PartialEq + Eq + PartialOrd + Ord + TryInto + From + Add + TryFromBytes + IntoBytes + Immutable + KnownLayout + Send + Sync + Printable, { #[inline] fn unwrap_to_usize(self) -> usize { self.to_usize().unwrap() } #[inline] fn to_usize(self) -> Result { self.try_into().map_err(|_| Error::FailedKeyTryIntoUsize) } #[inline] fn decremented(self) -> Option { self.unwrap_to_usize().checked_sub(1).map(Self::from) } }