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
71
72
73
74
75
76
77
78
79
80
use crate::map_error_code;
use std::io;
use zstd_safe;
#[derive(Default)]
pub struct Compressor {
context: zstd_safe::CCtx<'static>,
dict: Vec<u8>,
}
impl Compressor {
pub fn new() -> Self {
Compressor::with_dict(Vec::new())
}
pub fn with_dict(dict: Vec<u8>) -> Self {
Compressor {
context: zstd_safe::create_cctx(),
dict,
}
}
pub fn compress_to_buffer(
&mut self,
source: &[u8],
destination: &mut [u8],
level: i32,
) -> io::Result<usize> {
zstd_safe::compress_using_dict(
&mut self.context,
destination,
source,
&self.dict[..],
level,
)
.map_err(map_error_code)
}
pub fn compress(
&mut self,
data: &[u8],
level: i32,
) -> io::Result<Vec<u8>> {
let buffer_len = zstd_safe::compress_bound(data.len());
let mut buffer = Vec::with_capacity(buffer_len);
unsafe {
buffer.set_len(buffer_len);
let len = self.compress_to_buffer(data, &mut buffer[..], level)?;
buffer.set_len(len);
}
Ok(buffer)
}
}
fn _assert_traits() {
fn _assert_send<T: Send>(_: T) {}
_assert_send(Compressor::new());
}