use core::ops::{Deref, DerefMut};
mod array;
#[cfg(feature = "alloc")]
mod vec;
pub use array::WriteArrayBuffer;
#[cfg(feature = "alloc")]
pub use vec::WriteVecBuffer;
#[derive(Debug)]
pub struct BytesSlice<'a, const HEADER_SIZE: usize = 0, const TRAILER_SIZE: usize = 0>(
pub(crate) &'a mut [u8],
);
impl<'a, const HEADER_SIZE: usize, const TRAILER_SIZE: usize>
BytesSlice<'a, HEADER_SIZE, TRAILER_SIZE>
{
#[inline]
pub(crate) fn new(slice: &'a mut [u8]) -> Self {
Self(slice)
}
#[inline]
pub fn header(&mut self) -> &mut [u8] {
&mut self.0[..HEADER_SIZE]
}
#[inline]
pub fn trailer(&mut self) -> &mut [u8] {
let len = self.0.len();
&mut self.0[len - TRAILER_SIZE..]
}
#[inline]
pub fn frame(&self) -> &[u8] {
self.0
}
#[inline]
pub fn frame_mut(&mut self) -> &mut [u8] {
self.0
}
}
impl<const HEADER_SIZE: usize, const TRAILER_SIZE: usize> Deref
for BytesSlice<'_, HEADER_SIZE, TRAILER_SIZE>
{
type Target = [u8];
#[inline]
fn deref(&self) -> &Self::Target {
&self.0[HEADER_SIZE..self.0.len() - TRAILER_SIZE]
}
}
impl<const HEADER_SIZE: usize, const TRAILER_SIZE: usize> DerefMut
for BytesSlice<'_, HEADER_SIZE, TRAILER_SIZE>
{
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
let len = self.0.len();
&mut self.0[HEADER_SIZE..len - TRAILER_SIZE]
}
}
pub trait WriteBytesSlice {
fn size(&self) -> usize;
fn write(self, slice: &mut [u8]);
}
impl WriteBytesSlice for &[u8] {
#[inline]
fn size(&self) -> usize {
self.len()
}
#[inline]
fn write(self, slice: &mut [u8]) {
slice.copy_from_slice(self.as_ref());
}
}
impl<F> WriteBytesSlice for (usize, F)
where
F: FnOnce(&mut [u8]),
{
#[inline]
fn size(&self) -> usize {
self.0
}
#[inline]
fn write(self, slice: &mut [u8]) {
self.1(slice);
}
}