compression_codecs/lzma/
decoder.rs

1use crate::{Decode, Xz2Decoder};
2use compression_core::util::PartialBuffer;
3use std::{convert::TryInto, io::Result};
4
5/// Lzma decoding stream
6#[derive(Debug)]
7pub struct LzmaDecoder {
8    inner: Xz2Decoder,
9}
10
11impl From<Xz2Decoder> for LzmaDecoder {
12    fn from(inner: Xz2Decoder) -> Self {
13        Self { inner }
14    }
15}
16
17impl Default for LzmaDecoder {
18    fn default() -> Self {
19        Self {
20            inner: Xz2Decoder::new(usize::MAX.try_into().unwrap()),
21        }
22    }
23}
24
25impl LzmaDecoder {
26    pub fn new() -> Self {
27        Self::default()
28    }
29
30    pub fn with_memlimit(memlimit: u64) -> Self {
31        Self {
32            inner: Xz2Decoder::new(memlimit),
33        }
34    }
35}
36
37impl Decode for LzmaDecoder {
38    fn reinit(&mut self) -> Result<()> {
39        self.inner.reinit()
40    }
41
42    fn decode(
43        &mut self,
44        input: &mut PartialBuffer<impl AsRef<[u8]>>,
45        output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
46    ) -> Result<bool> {
47        self.inner.decode(input, output)
48    }
49
50    fn flush(
51        &mut self,
52        output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
53    ) -> Result<bool> {
54        self.inner.flush(output)
55    }
56
57    fn finish(
58        &mut self,
59        output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
60    ) -> Result<bool> {
61        self.inner.finish(output)
62    }
63}