use std::borrow::Cow;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Bound {
Unbounded,
Bounded {
max_size: u32,
is_fixed_size: bool,
},
}
impl Bound {
pub(crate) const fn accepts_length(self, length: usize) -> bool {
match self {
Self::Unbounded => true,
Self::Bounded {
max_size,
is_fixed_size,
} => {
let maximum = max_size as usize;
length <= maximum && (!is_fixed_size || length == maximum)
}
}
}
pub(crate) const fn check(self, bytes: &[u8]) {
if !self.accepts_length(bytes.len()) {
panic!("record violates codec bound");
}
}
}
pub trait Storable: Sized {
const BOUND: Bound;
fn to_bytes(&self) -> Cow<'_, [u8]>;
fn into_bytes(self) -> Vec<u8> {
self.to_bytes().into_owned()
}
fn from_bytes(bytes: Cow<'_, [u8]>) -> Self;
const CMP_ENCODED: Option<fn(&Self, &[u8]) -> std::cmp::Ordering> = None;
fn check_bounds(bytes: &[u8]) {
Self::BOUND.check(bytes)
}
fn to_bytes_checked(&self) -> Cow<'_, [u8]> {
let bytes = self.to_bytes();
Self::BOUND.check(&bytes);
bytes
}
fn into_bytes_checked(self) -> Vec<u8> {
let bytes = self.into_bytes();
Self::BOUND.check(&bytes);
bytes
}
}
macro_rules! integer {
($($ty:ty),* $(,)?) => {$ (
impl Storable for $ty {
const BOUND: Bound = Bound::Bounded { max_size: size_of::<Self>() as u32, is_fixed_size: true };
fn to_bytes(&self) -> Cow<'_, [u8]> { Cow::Owned(self.to_le_bytes().to_vec()) }
fn from_bytes(bytes: Cow<'_, [u8]>) -> Self { Self::from_le_bytes(bytes.as_ref().try_into().expect("integer width")) }
}
)*};
}
integer!(u16, u32, u64, u128, i8, i16, i32, i64, i128, f32, f64);
impl Storable for u8 {
const BOUND: Bound = Bound::Bounded {
max_size: 1,
is_fixed_size: true,
};
fn to_bytes(&self) -> Cow<'_, [u8]> {
Cow::Borrowed(std::slice::from_ref(self))
}
fn from_bytes(bytes: Cow<'_, [u8]>) -> Self {
Self::from_le_bytes(bytes.as_ref().try_into().expect("integer width"))
}
}
impl Storable for () {
const BOUND: Bound = Bound::Bounded {
max_size: 0,
is_fixed_size: true,
};
fn to_bytes(&self) -> Cow<'_, [u8]> {
Cow::Borrowed(&[])
}
fn from_bytes(bytes: Cow<'_, [u8]>) -> Self {
Self::check_bounds(&bytes)
}
}
impl Storable for bool {
const BOUND: Bound = Bound::Bounded {
max_size: 1,
is_fixed_size: true,
};
fn to_bytes(&self) -> Cow<'_, [u8]> {
Cow::Borrowed(if *self { &[1] } else { &[0] })
}
fn from_bytes(bytes: Cow<'_, [u8]>) -> Self {
match bytes.as_ref() {
[0] => false,
[1] => true,
_ => panic!("invalid boolean encoding"),
}
}
}
impl Storable for Vec<u8> {
const CMP_ENCODED: Option<fn(&Self, &[u8]) -> std::cmp::Ordering> =
Some(|key, stored| key.as_slice().cmp(stored));
const BOUND: Bound = Bound::Unbounded;
fn to_bytes(&self) -> Cow<'_, [u8]> {
Cow::Borrowed(self)
}
fn into_bytes(self) -> Vec<u8> {
self
}
fn from_bytes(bytes: Cow<'_, [u8]>) -> Self {
bytes.into_owned()
}
}
impl Storable for String {
const CMP_ENCODED: Option<fn(&Self, &[u8]) -> std::cmp::Ordering> = Some(|key, stored| {
key.as_str()
.cmp(std::str::from_utf8(stored).expect("invalid UTF-8 record"))
});
const BOUND: Bound = Bound::Unbounded;
fn to_bytes(&self) -> Cow<'_, [u8]> {
Cow::Borrowed(self.as_bytes())
}
fn into_bytes(self) -> Vec<u8> {
self.into_bytes()
}
fn from_bytes(bytes: Cow<'_, [u8]>) -> Self {
Self::from_utf8(bytes.into_owned()).expect("invalid UTF-8 record")
}
}
impl<const N: usize> Storable for [u8; N] {
const BOUND: Bound = Bound::Bounded {
max_size: {
assert!(N <= u32::MAX as usize);
N as u32
},
is_fixed_size: true,
};
fn to_bytes(&self) -> Cow<'_, [u8]> {
Cow::Borrowed(self)
}
fn from_bytes(bytes: Cow<'_, [u8]>) -> Self {
bytes.as_ref().try_into().expect("invalid byte array width")
}
}
impl<T: Storable> Storable for Option<T> {
const BOUND: Bound = match T::BOUND {
Bound::Unbounded => Bound::Unbounded,
Bound::Bounded { max_size, .. } => Bound::Bounded {
max_size: max_size.checked_add(1).expect("optional bound overflow"),
is_fixed_size: false,
},
};
fn to_bytes(&self) -> Cow<'_, [u8]> {
let mut bytes = vec![u8::from(self.is_some())];
if let Some(value) = self {
bytes.extend_from_slice(&value.to_bytes_checked());
}
Cow::Owned(bytes)
}
fn from_bytes(bytes: Cow<'_, [u8]>) -> Self {
match bytes.as_ref().split_first() {
Some((0, [])) => None,
Some((1, payload)) => {
T::check_bounds(payload);
Some(T::from_bytes(Cow::Borrowed(payload)))
}
_ => panic!("invalid optional record tag"),
}
}
}
const fn pair_bound(left: Bound, right: Bound) -> Bound {
match (left, right) {
(
Bound::Bounded {
max_size: a,
is_fixed_size: af,
},
Bound::Bounded {
max_size: b,
is_fixed_size: bf,
},
) => {
let width = a as u64 + b as u64 + 4;
if width <= u32::MAX as u64 {
Bound::Bounded {
max_size: width as u32,
is_fixed_size: af && bf,
}
} else {
Bound::Unbounded
}
}
_ => Bound::Unbounded,
}
}
fn push_component(bytes: &mut Vec<u8>, component: &[u8]) {
let length = u32::try_from(component.len()).expect("tuple component exceeds u32");
bytes.extend_from_slice(&length.to_le_bytes());
bytes.extend_from_slice(component);
}
fn take_component<T: Storable>(bytes: &mut &[u8]) -> T {
let (prefix, payload) = bytes.split_at(4);
let length = u32::from_le_bytes(prefix.try_into().unwrap()) as usize;
let (component, rest) = payload.split_at(length);
*bytes = rest;
decode_component(component)
}
fn decode_component<T: Storable>(bytes: &[u8]) -> T {
T::BOUND.check(bytes);
T::from_bytes(Cow::Borrowed(bytes))
}
impl<A: Storable, B: Storable> Storable for (A, B) {
const BOUND: Bound = pair_bound(A::BOUND, B::BOUND);
fn to_bytes(&self) -> Cow<'_, [u8]> {
let left = self.0.to_bytes();
let right = self.1.to_bytes();
A::BOUND.check(&left);
B::BOUND.check(&right);
let mut bytes = Vec::with_capacity(4 + left.len() + right.len());
push_component(&mut bytes, &left);
bytes.extend_from_slice(&right);
Cow::Owned(bytes)
}
fn from_bytes(bytes: Cow<'_, [u8]>) -> Self {
let mut payload = bytes.as_ref();
(take_component(&mut payload), decode_component(payload))
}
}
impl<A: Storable, B: Storable, C: Storable> Storable for (A, B, C) {
const BOUND: Bound = pair_bound(A::BOUND, pair_bound(B::BOUND, C::BOUND));
fn to_bytes(&self) -> Cow<'_, [u8]> {
let a = self.0.to_bytes();
let b = self.1.to_bytes();
let c = self.2.to_bytes();
A::BOUND.check(&a);
B::BOUND.check(&b);
C::BOUND.check(&c);
let mut bytes = Vec::with_capacity(8 + a.len() + b.len() + c.len());
push_component(&mut bytes, &a);
push_component(&mut bytes, &b);
bytes.extend_from_slice(&c);
Cow::Owned(bytes)
}
fn from_bytes(bytes: Cow<'_, [u8]>) -> Self {
let mut payload = bytes.as_ref();
(
take_component(&mut payload),
take_component(&mut payload),
decode_component(payload),
)
}
}