pub(crate) enum OutBuf<'a> {
Growable(&'a mut Vec<u8>),
Fixed(FixedOut<'a>),
}
pub(crate) struct FixedOut<'a> {
buf: &'a mut [u8],
stored: usize,
dropped: usize,
}
impl<'a> OutBuf<'a> {
pub(crate) fn growable(buf: &'a mut Vec<u8>) -> Self {
OutBuf::Growable(buf)
}
pub(crate) fn fixed(buf: &'a mut [u8]) -> Self {
OutBuf::Fixed(FixedOut {
buf,
stored: 0,
dropped: 0,
})
}
pub(crate) fn len(&self) -> usize {
match self {
OutBuf::Growable(buf) => buf.len(),
OutBuf::Fixed(fixed) => fixed.stored + fixed.dropped,
}
}
pub(crate) fn overflowed(&self) -> bool {
match self {
OutBuf::Growable(_) => false,
OutBuf::Fixed(fixed) => fixed.dropped > 0,
}
}
pub(crate) fn reserve(&mut self, additional: usize) {
if let OutBuf::Growable(buf) = self {
buf.reserve(additional);
}
}
pub(crate) fn push(&mut self, byte: u8) {
match self {
OutBuf::Growable(buf) => buf.push(byte),
OutBuf::Fixed(fixed) => {
if fixed.stored < fixed.buf.len() {
fixed.buf[fixed.stored] = byte;
fixed.stored += 1;
} else {
fixed.dropped += 1;
}
}
}
}
pub(crate) fn extend_from_slice(&mut self, bytes: &[u8]) {
match self {
OutBuf::Growable(buf) => buf.extend_from_slice(bytes),
OutBuf::Fixed(fixed) => {
let room = fixed.buf.len() - fixed.stored;
let fits = room.min(bytes.len());
fixed.buf[fixed.stored..fixed.stored + fits].copy_from_slice(&bytes[..fits]);
fixed.stored += fits;
fixed.dropped += bytes.len() - fits;
}
}
}
pub(crate) fn resize(&mut self, new_len: usize, value: u8) {
let current = self.len();
debug_assert!(new_len >= current, "resize is not used to shrink");
match self {
OutBuf::Growable(buf) => buf.resize(new_len, value),
OutBuf::Fixed(_) => {
for _ in current..new_len {
self.push(value);
}
}
}
}
pub(crate) fn truncate(&mut self, new_len: usize) {
match self {
OutBuf::Growable(buf) => buf.truncate(new_len),
OutBuf::Fixed(fixed) => {
if new_len >= fixed.stored + fixed.dropped {
return;
}
fixed.stored = new_len.min(fixed.buf.len());
fixed.dropped = new_len - fixed.stored;
}
}
}
pub(crate) fn write_at(&mut self, pos: usize, bytes: &[u8]) {
let end = pos + bytes.len();
match self {
OutBuf::Growable(buf) => buf[pos..end].copy_from_slice(bytes),
OutBuf::Fixed(fixed) => {
if end <= fixed.stored {
fixed.buf[pos..end].copy_from_slice(bytes);
}
}
}
}
#[cfg(test)]
fn as_slice(&self) -> &[u8] {
match self {
OutBuf::Growable(buf) => buf.as_slice(),
OutBuf::Fixed(fixed) => &fixed.buf[..fixed.stored],
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn fixed_stores_what_fits_and_counts_what_does_not() {
let mut backing = [0u8; 4];
let mut out = OutBuf::fixed(&mut backing);
out.extend_from_slice(b"ab");
assert_eq!(out.len(), 2);
assert!(!out.overflowed());
out.extend_from_slice(b"cdef");
assert_eq!(
out.len(),
6,
"len counts every byte written, not just stored"
);
assert!(out.overflowed());
assert_eq!(out.as_slice(), b"abcd");
}
#[test]
fn rolling_back_past_the_overflow_clears_it() {
let mut backing = [0u8; 4];
let mut out = OutBuf::fixed(&mut backing);
out.extend_from_slice(b"abcdefgh");
assert!(out.overflowed());
out.truncate(2);
assert!(
!out.overflowed(),
"the discarded bytes are no longer output"
);
assert_eq!(out.len(), 2);
assert_eq!(out.as_slice(), b"ab");
out.extend_from_slice(b"XY");
assert_eq!(out.as_slice(), b"abXY");
assert!(!out.overflowed());
}
#[test]
fn truncating_inside_the_dropped_region_keeps_the_deficit() {
let mut backing = [0u8; 4];
let mut out = OutBuf::fixed(&mut backing);
out.extend_from_slice(b"abcdefgh");
out.truncate(6);
assert_eq!(out.len(), 6);
assert!(out.overflowed());
assert_eq!(out.as_slice(), b"abcd");
}
#[test]
fn write_at_backfills_a_header_and_skips_a_dropped_one() {
let mut backing = [0u8; 4];
let mut out = OutBuf::fixed(&mut backing);
out.resize(3, 0);
out.extend_from_slice(b"z");
out.write_at(0, b"hdr");
assert_eq!(out.as_slice(), b"hdrz");
out.extend_from_slice(b"payload");
let past_the_end = 6;
out.write_at(past_the_end, b"hdr");
assert!(out.overflowed());
}
#[test]
fn growable_matches_vec_semantics() {
let mut backing = Vec::new();
let mut out = OutBuf::growable(&mut backing);
out.resize(3, 0);
out.extend_from_slice(b"payload");
out.write_at(0, b"hdr");
out.truncate(5);
assert!(!out.overflowed());
assert_eq!(out.as_slice(), b"hdrpa");
}
}