21 个版本 (3 个稳定版)
1.2.0 | 2024年8月3日 |
---|---|
1.1.0 | 2024年3月30日 |
1.0.0 | 2023年5月20日 |
0.5.3 | 2021年3月9日 |
0.3.2 | 2019年5月24日 |
#107 in 压缩
244 每月下载量
在 3 个crate中(通过 yukikaze)使用
88KB
2K SLoC
コンプ(compu)
Rust 带有泛型接口的压缩库
功能
默认情况下,所有功能都是关闭的。此crate需要系统分配器设置可用,以使 alloc
可用。
brotli-c
- 使用C库启用brotli
接口。brotli-rust
- 使用纯Rust库启用brotli
接口。zlib-ng
- 启用zlib-ng
接口。zlib
- 启用zlib
接口。zlib-static
- 启用具有static
功能的zlib
接口。zstd
- 启用zstd
接口。bytes
- 启用bytes
支持
用法
解码
使用Decoder的最小示例。使用 Interface
创建实例。
use compu::{Decoder, DecodeStatus, DecodeError};
fn example(decoder: &mut Decoder, input: &[u8]) -> Result<Vec<u8>, DecodeError> {
let mut output = Vec::with_capacity(1024);
loop {
let result = decoder.decode_vec(input, &mut output).status?;
match result {
DecodeStatus::NeedInput => panic!("Not enough input, incomplete data?"),
//If you need more output, please allocate spare capacity.
//API never allocates, only you allocate
DecodeStatus::NeedOutput => output.reserve(1024),
DecodeStatus::Finished => {
//Make sure to reset state, if you want to re-use decoder.
decoder.reset();
break Ok(output)
}
}
}
}
编码
使用Encoder的最小示例。使用 Interface
创建实例。
use compu::{Encoder, EncodeStatus, EncodeOp};
fn example(encoder: &mut Encoder, input: &[u8]) -> Vec<u8> {
let mut output = Vec::with_capacity(1024);
loop {
let result = encoder.encode_vec(input, &mut output, EncodeOp::Finish).status;
match result {
//This status is returned by any other `EncodeOp` except `Finish
EncodeStatus::Continue => panic!("I wanted to finish but continue!?"),
//If you need more output, please allocate spare capacity.
//API never allocates, only you allocate
EncodeStatus::NeedOutput => output.reserve(1024),
//If you have enough space, `EncodeOp::Finish` will result in this operation
EncodeStatus::Finished => {
//Make sure to reset state, if you want to re-use it.
encoder.reset();
break output;
}
//Generally can indicate internal error likely due to OOM condition.
//Note that all wrappers ensure that Rust's global allocator is used,
//so take care if you use custom one
//Generally should not happen, so it is ok to just panic
//but be a good boy and return error properly if it happens, even if it is unlikely
EncodeStatus::Error => {
panic!("unlikely")
}
}
}
}
依赖
~0–3MB
~88K SLoC