use alloc::vec::Vec;
use super::error::CoreError;
pub(crate) trait Output {
fn prepare(&mut self, position: usize, needed: usize) -> Result<&mut [u8], CoreError>;
}
pub(crate) struct SliceOutput<'a>(pub(crate) &'a mut [u8]);
impl Output for SliceOutput<'_> {
#[inline]
fn prepare(&mut self, position: usize, _needed: usize) -> Result<&mut [u8], CoreError> {
self.0.get_mut(position..).ok_or(CoreError::DstSizeTooSmall)
}
}
pub(crate) struct VecOutput<'a> {
pub(crate) buffer: &'a mut Vec<u8>,
pub(crate) start: usize,
pub(crate) limit: usize,
}
impl Output for VecOutput<'_> {
fn prepare(&mut self, position: usize, needed: usize) -> Result<&mut [u8], CoreError> {
let position = self
.start
.checked_add(position)
.ok_or(CoreError::DstSizeTooSmall)?;
if position > self.limit {
return Err(CoreError::DstSizeTooSmall);
}
let end = position.saturating_add(needed).min(self.limit);
if end > self.buffer.len() {
self.buffer.resize(end, 0);
}
Ok(&mut self.buffer[position..])
}
}