pub trait EncodePlan {
#[must_use]
fn encoded_len(&self) -> usize;
fn write_into(&self, output: &mut [u8]);
}
pub trait PreparedLayout {
type ViewMut<'output>;
#[must_use]
fn encoded_len(&self) -> usize;
fn commit_into<'output>(
self,
output: &'output mut [u8],
) -> Result<(Self::ViewMut<'output>, &'output mut [u8]), OutputTooShortError>;
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct OutputTooShortError {
pub required: usize,
pub available: usize,
}
impl core::fmt::Display for OutputTooShortError {
fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
formatter,
"output too short: need {} bytes, got {}",
self.required, self.available
)
}
}
impl core::error::Error for OutputTooShortError {}
impl<const N: usize> EncodePlan for [u8; N] {
#[inline]
fn encoded_len(&self) -> usize {
N
}
#[inline]
fn write_into(&self, output: &mut [u8]) {
output.copy_from_slice(self);
}
}
impl EncodePlan for &[u8] {
#[inline]
fn encoded_len(&self) -> usize {
self.len()
}
#[inline]
fn write_into(&self, output: &mut [u8]) {
output.copy_from_slice(self);
}
}