Impl PopFrom traits for u8 arrays

This commit is contained in:
Lucas Schumacher 2024-09-22 17:21:22 -04:00
parent f5af130f2d
commit 5f238c7bc4

View File

@ -236,6 +236,48 @@ flt_impl_pop_non_ne!(f32);
core_impl_pop_ne!(f64); core_impl_pop_ne!(f64);
flt_impl_pop_non_ne!(f64); flt_impl_pop_non_ne!(f64);
impl<const SIZE: usize> PopFromNE for [u8; SIZE] {
fn pop_ne_from(source: &mut &[u8]) -> Result<Self>
where
Self: Sized,
{
if source.len() < SIZE {
anyhow::bail!("Buffer to small to pop [u8; {SIZE}]");
}
let value = source[..SIZE].try_into()?;
*source = &source[SIZE..];
Ok(value)
}
fn push_ne_into<T: Pushable>(&self, dest: &mut T) -> Result<()> {
dest.push_slice(self)
}
}
impl<const SIZE: usize> PopFromLE for [u8; SIZE] {
fn pop_le_from(source: &mut &[u8]) -> Result<Self>
where
Self: Sized,
{
Self::pop_ne_from(source)
}
fn push_le_into<T: Pushable>(&self, dest: &mut T) -> Result<()> {
self.push_ne_into(dest)
}
}
impl<const SIZE: usize> PopFromBE for [u8; SIZE] {
fn pop_be_from(source: &mut &[u8]) -> Result<Self>
where
Self: Sized,
{
Self::pop_ne_from(source)
}
fn push_be_into<T: Pushable>(&self, dest: &mut T) -> Result<()> {
self.push_ne_into(dest)
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;