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. |
6 | pub struct OutputBuffer<'a> { |
7 | slice: &'a mut [u8], |
8 | position: usize, |
9 | } |
10 | |
11 | impl<'a> OutputBuffer<'a> { |
12 | #[inline ] |
13 | pub fn from_slice_and_pos(slice: &'a mut [u8], position: usize) -> OutputBuffer<'a> { |
14 | OutputBuffer { slice, position } |
15 | } |
16 | |
17 | #[inline ] |
18 | pub const fn position(&self) -> usize { |
19 | self.position |
20 | } |
21 | |
22 | #[inline ] |
23 | pub fn set_position(&mut self, position: usize) { |
24 | self.position = position; |
25 | } |
26 | |
27 | /// Write a byte to the current position and increment |
28 | /// |
29 | /// Assumes that there is space. |
30 | #[inline ] |
31 | pub fn write_byte(&mut self, byte: u8) { |
32 | self.slice[self.position] = byte; |
33 | self.position += 1; |
34 | } |
35 | |
36 | /// Write a slice to the current position and increment |
37 | /// |
38 | /// Assumes that there is space. |
39 | #[inline ] |
40 | pub fn write_slice(&mut self, data: &[u8]) { |
41 | let len = data.len(); |
42 | self.slice[self.position..self.position + len].copy_from_slice(data); |
43 | self.position += data.len(); |
44 | } |
45 | |
46 | #[inline ] |
47 | pub const fn bytes_left(&self) -> usize { |
48 | self.slice.len() - self.position |
49 | } |
50 | |
51 | #[inline ] |
52 | pub const fn get_ref(&self) -> &[u8] { |
53 | self.slice |
54 | } |
55 | |
56 | #[inline ] |
57 | pub fn get_mut(&mut self) -> &mut [u8] { |
58 | self.slice |
59 | } |
60 | } |
61 | |