use crate::buffer::{write, write::consume};
#[must_use = "commit the staged suffix after editing the prefix"]
#[repr(C)]
pub struct Edit<'a, const N: usize> {
prefix: &'a mut [u8; N],
pub(in crate::buffer::write) spare: write::raw::Spare<'a>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Error {
pub offset: usize,
pub width: usize,
pub len: usize,
}
impl Error {
pub(in crate::buffer) const fn new(offset: usize, width: usize, len: usize) -> Self {
Self { offset, width, len }
}
}
impl<'a, const N: usize> Edit<'a, N> {
pub fn try_new(spare: write::raw::Spare<'a>, prefix: &[u8; N]) -> Result<Self, consume::Error> {
write::raw::edit(spare, prefix)
}
pub(in crate::buffer) fn new(prefix: &'a mut [u8; N], spare: write::raw::Spare<'a>) -> Self {
Self { prefix, spare }
}
pub fn spare(&mut self) -> write::raw::Spare<'_> {
write::SpareExt::spare(&mut self.spare)
}
pub fn prefix(&mut self) -> &mut [u8; N] {
self.prefix
}
pub fn parts(&mut self) -> (&mut [u8; N], write::raw::Spare<'_>) {
(&mut *self.prefix, write::SpareExt::spare(&mut self.spare))
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn len(&self) -> usize {
self.spare.len()
}
pub fn commit(self) {
write::Commit::commit(self.spare);
}
}
const _: () = assert!(size_of::<Edit<'static, 1>>() == 4 * size_of::<usize>());
const _: () = assert!(size_of::<Error>() == 3 * size_of::<usize>());