1/// A wrapper for the output slice used when decompressing.
2///
3/// Using this rather than `Cursor` lets us implement the writing methods directly on
4/// the buffer and lets us use a usize rather than u64 for the position which helps with
5/// performance on 32-bit systems.
6pub struct OutputBuffer<'a> {
7 slice: &'a mut [u8],
8 position: usize,
9}
1011impl<'a> OutputBuffer<'a> {
12#[inline]
13pub fn from_slice_and_pos(slice: &'a mut [u8], position: usize) -> OutputBuffer<'a> {
14 OutputBuffer { slice, position }
15 }
1617#[inline]
18pub fn position(&self) -> usize {
19self.position
20 }
2122#[inline]
23pub fn set_position(&mut self, position: usize) {
24self.position = position;
25 }
2627/// Write a byte to the current position and increment
28 ///
29 /// Assumes that there is space.
30#[inline]
31pub fn write_byte(&mut self, byte: u8) {
32self.slice[self.position] = byte;
33self.position += 1;
34 }
3536/// Write a slice to the current position and increment
37 ///
38 /// Assumes that there is space.
39#[inline]
40pub fn write_slice(&mut self, data: &[u8]) {
41let len = data.len();
42self.slice[self.position..self.position + len].copy_from_slice(data);
43self.position += data.len();
44 }
4546#[inline]
47pub fn bytes_left(&self) -> usize {
48self.slice.len() - self.position
49 }
5051#[inline]
52pub fn get_ref(&self) -> &[u8] {
53self.slice
54 }
5556#[inline]
57pub fn get_mut(&mut self) -> &mut [u8] {
58self.slice
59 }
60}