1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use crate::map_error_code;
use std::io;
use zstd_safe;
#[derive(Default)]
pub struct Decompressor {
context: zstd_safe::DCtx<'static>,
dict: Vec<u8>,
}
impl Decompressor {
pub fn new() -> Self {
Decompressor::with_dict(Vec::new())
}
pub fn with_dict(dict: Vec<u8>) -> Self {
Decompressor {
context: zstd_safe::create_dctx(),
dict,
}
}
pub fn decompress_to_buffer(
&mut self,
source: &[u8],
destination: &mut [u8],
) -> io::Result<usize> {
zstd_safe::decompress_using_dict(
&mut self.context,
destination,
source,
&self.dict,
)
.map_err(map_error_code)
}
pub fn decompress(
&mut self,
data: &[u8],
capacity: usize,
) -> io::Result<Vec<u8>> {
let mut buffer = Vec::with_capacity(capacity);
unsafe {
buffer.set_len(capacity);
let len = self.decompress_to_buffer(data, &mut buffer[..])?;
buffer.set_len(len);
}
Ok(buffer)
}
}
fn _assert_traits() {
fn _assert_send<T: Send>(_: T) {}
_assert_send(Decompressor::new());
}