use crate::error::{Error, Result};
pub(crate) enum DecodeOut<'a> {
Growable(&'a mut Vec<u8>),
Fixed(FixedDst<'a>),
}
pub(crate) struct FixedDst<'a> {
buf: &'a mut [u8],
len: usize,
}
impl<'a> DecodeOut<'a> {
pub(crate) fn growable(buf: &'a mut Vec<u8>) -> Self {
DecodeOut::Growable(buf)
}
pub(crate) fn fixed(buf: &'a mut [u8]) -> Self {
DecodeOut::Fixed(FixedDst { buf, len: 0 })
}
#[inline(always)]
pub(crate) fn len(&self) -> usize {
match self {
DecodeOut::Growable(buf) => buf.len(),
DecodeOut::Fixed(fixed) => fixed.len,
}
}
#[inline(always)]
pub(crate) fn capacity(&self) -> usize {
match self {
DecodeOut::Growable(buf) => buf.capacity(),
DecodeOut::Fixed(fixed) => fixed.buf.len(),
}
}
#[inline(always)]
pub(crate) fn as_slice(&self) -> &[u8] {
match self {
DecodeOut::Growable(buf) => buf.as_slice(),
DecodeOut::Fixed(fixed) => &fixed.buf[..fixed.len],
}
}
#[inline(always)]
pub(crate) fn as_ptr(&self) -> *const u8 {
match self {
DecodeOut::Growable(buf) => buf.as_ptr(),
DecodeOut::Fixed(fixed) => fixed.buf.as_ptr(),
}
}
#[inline(always)]
pub(crate) fn as_mut_ptr(&mut self) -> *mut u8 {
match self {
DecodeOut::Growable(buf) => buf.as_mut_ptr(),
DecodeOut::Fixed(fixed) => fixed.buf.as_mut_ptr(),
}
}
#[allow(unsafe_code)]
#[inline(always)]
pub(crate) unsafe fn set_len(&mut self, len: usize) {
match self {
DecodeOut::Growable(buf) => unsafe { buf.set_len(len) },
DecodeOut::Fixed(fixed) => {
debug_assert!(len <= fixed.buf.len());
fixed.len = len;
}
}
}
#[cfg_attr(not(debug_assertions), allow(dead_code))]
pub(crate) fn is_growable(&self) -> bool {
matches!(self, DecodeOut::Growable(_))
}
pub(crate) fn clear(&mut self) {
match self {
DecodeOut::Growable(buf) => buf.clear(),
DecodeOut::Fixed(fixed) => fixed.len = 0,
}
}
pub(crate) fn try_reserve(&mut self, additional: usize) -> Result<()> {
match self {
DecodeOut::Growable(buf) => buf
.try_reserve(additional)
.map_err(|_| Error::OutputSizeOverflow),
DecodeOut::Fixed(_) => Ok(()),
}
}
#[inline(always)]
pub(crate) fn ensure_room(&mut self, additional: usize) -> Result<()> {
match self {
DecodeOut::Growable(buf) => {
if buf.capacity() - buf.len() < additional {
buf.reserve(additional);
}
Ok(())
}
DecodeOut::Fixed(fixed) => {
if fixed.buf.len() - fixed.len < additional {
return Err(Error::DstSizeTooSmall);
}
Ok(())
}
}
}
#[allow(unsafe_code)]
#[inline(always)]
pub(crate) fn append(&mut self, bytes: &[u8]) -> Result<()> {
let len = bytes.len();
if len == 0 {
return Ok(());
}
self.ensure_room(len)?;
let out_len = self.len();
unsafe {
unsafe extern "C" {
fn memcpy(
dst: *mut core::ffi::c_void,
src: *const core::ffi::c_void,
len: usize,
) -> *mut core::ffi::c_void;
}
memcpy(
self.as_mut_ptr().add(out_len).cast(),
bytes.as_ptr().cast(),
len,
);
self.set_len(out_len + len);
}
Ok(())
}
pub(crate) fn append_repeated(&mut self, byte: u8, count: usize) -> Result<()> {
if count == 0 {
return Ok(());
}
self.ensure_room(count)?;
let out_len = self.len();
match self {
DecodeOut::Growable(buf) => buf.resize(out_len + count, byte),
DecodeOut::Fixed(fixed) => {
fixed.buf[out_len..out_len + count].fill(byte);
fixed.len = out_len + count;
}
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn fixed_reports_the_write_that_does_not_fit() {
let mut backing = [0u8; 4];
let mut out = DecodeOut::fixed(&mut backing);
out.append(b"ab").expect("two bytes fit in four");
assert_eq!(out.len(), 2);
assert!(matches!(out.append(b"cdef"), Err(Error::DstSizeTooSmall)));
assert_eq!(out.len(), 2);
assert_eq!(out.as_slice(), b"ab");
out.append(b"cd").expect("the two that do fit still fit");
assert_eq!(out.as_slice(), b"abcd");
}
#[test]
fn a_hint_never_fails_on_a_fixed_destination() {
let mut backing = [0u8; 4];
let mut out = DecodeOut::fixed(&mut backing);
out.try_reserve(1 << 20).expect("a hint is advisory");
assert_eq!(out.capacity(), 4);
}
#[test]
fn repeated_bytes_fill_and_then_refuse() {
let mut backing = [0u8; 4];
let mut out = DecodeOut::fixed(&mut backing);
out.append_repeated(b'x', 3).expect("three fit");
assert_eq!(out.as_slice(), b"xxx");
assert!(matches!(
out.append_repeated(b'y', 2),
Err(Error::DstSizeTooSmall)
));
assert_eq!(out.as_slice(), b"xxx");
}
#[test]
fn growable_tracks_the_vec_it_wraps() {
let mut backing = Vec::new();
let mut out = DecodeOut::growable(&mut backing);
out.append(b"hello").expect("a Vec grows");
out.append_repeated(b'!', 3).expect("a Vec grows");
assert_eq!(out.as_slice(), b"hello!!!");
assert!(out.capacity() >= 8);
out.clear();
assert_eq!(out.len(), 0);
assert_eq!(backing, b"");
}
}