pub struct Writer<'a>(&'a mut [u8]);
impl<'a> Writer<'a> {
pub fn new(bytes: &'a mut [u8]) -> Self {
Self(bytes)
}
pub fn u8(&mut self, byte: u8) {
self.0[..1].copy_from_slice(&byte.to_le_bytes());
let mut swap: &'a mut [u8] = &mut [];
core::mem::swap(&mut self.0, &mut swap);
let mut swap = swap.split_at_mut(1).1;
core::mem::swap(&mut self.0, &mut swap);
}
pub fn u16(&mut self, half: u16) {
self.0[..2].copy_from_slice(&half.to_le_bytes());
let mut swap: &'a mut [u8] = &mut [];
core::mem::swap(&mut self.0, &mut swap);
let mut swap = swap.split_at_mut(2).1;
core::mem::swap(&mut self.0, &mut swap);
}
pub fn u32(&mut self, word: u32) {
self.0[..4].copy_from_slice(&word.to_le_bytes());
let mut swap: &'a mut [u8] = &mut [];
core::mem::swap(&mut self.0, &mut swap);
let mut swap = swap.split_at_mut(4).1;
core::mem::swap(&mut self.0, &mut swap);
}
pub fn u64(&mut self, long: u64) {
self.0[..8].copy_from_slice(&long.to_le_bytes());
let mut swap: &'a mut [u8] = &mut [];
core::mem::swap(&mut self.0, &mut swap);
let mut swap = swap.split_at_mut(8).1;
core::mem::swap(&mut self.0, &mut swap);
}
pub fn str(&mut self, utf8: &str) {
let len = utf8.len();
self.0[..len].copy_from_slice(utf8.as_bytes());
let mut swap: &'a mut [u8] = &mut [];
core::mem::swap(&mut self.0, &mut swap);
let mut swap = swap.split_at_mut(len).1;
core::mem::swap(&mut self.0, &mut swap);
}
}
pub struct Reader<'a>(&'a [u8]);
impl<'a> Reader<'a> {
pub fn new(bytes: &'a [u8]) -> Self {
Self(bytes)
}
pub fn u8(&mut self) -> u8 {
let bytes = self.0[..1].try_into().expect("Out of bounds read");
self.0 = &self.0[1..];
u8::from_le_bytes(bytes)
}
pub fn u16(&mut self) -> u16 {
let bytes = self.0[..2].try_into().expect("Out of bounds read");
self.0 = &self.0[2..];
u16::from_le_bytes(bytes)
}
pub fn u32(&mut self) -> u32 {
let bytes = self.0[..4].try_into().expect("Out of bounds read");
self.0 = &self.0[4..];
u32::from_le_bytes(bytes)
}
pub fn u64(&mut self) -> u64 {
let bytes = self.0[..8].try_into().expect("Out of bounds read");
self.0 = &self.0[8..];
u64::from_le_bytes(bytes)
}
pub fn str(&mut self) -> Result<&str, core::str::Utf8Error> {
let bytes = self.0.get(0..).expect("Out of bounds read");
self.0 = &[];
core::str::from_utf8(bytes)
}
}