use super::Error;
use core::cmp::Ordering;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ByteSlice<'a>(pub(super) &'a [u8]);
impl<'a> ByteSlice<'a> {
#[must_use]
pub fn as_slice(self) -> &'a [u8] {
self.0
}
}
#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct Bytes<const N: usize> {
pub(super) length: u64,
pub(super) data: [u8; N],
}
impl<const N: usize> Bytes<N> {
pub(super) fn copy_admitted(bytes: &[u8]) -> Self {
Self::new(bytes).expect("admission and mutations enforce the byte bound")
}
pub const EMPTY: Self = Self {
length: 0,
data: [0; N],
};
pub fn new(bytes: &[u8]) -> Result<Self, Error> {
if bytes.len() > N {
return Err(Error::Limit);
}
let mut value = Self::EMPTY;
value.data[..bytes.len()].copy_from_slice(bytes);
value.length = bytes.len() as u64;
Ok(value)
}
#[must_use]
pub fn as_slice(&self) -> &[u8] {
&self.data[..self.length as usize]
}
}
impl<const N: usize> PartialEq for Bytes<N> {
fn eq(&self, other: &Self) -> bool {
self.as_slice() == other.as_slice()
}
}
impl<const N: usize> Eq for Bytes<N> {}
impl<const N: usize> PartialOrd for Bytes<N> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl<const N: usize> Ord for Bytes<N> {
fn cmp(&self, other: &Self) -> Ordering {
self.as_slice().cmp(other.as_slice())
}
}