/rust/registry/src/index.crates.io-1949cf8c6b5b557f/zstd-0.14.3/src/lib.rs
Line | Count | Source |
1 | | //! Rust binding to the [zstd library][zstd]. |
2 | | //! |
3 | | //! This crate provides: |
4 | | //! |
5 | | //! * An [encoder](stream/write/struct.Encoder.html) to compress data using zstd |
6 | | //! and send the output to another write. |
7 | | //! * A [decoder](stream/read/struct.Decoder.html) to read input data from a `Read` |
8 | | //! and decompress it. |
9 | | //! * Convenient functions for common tasks. |
10 | | //! |
11 | | //! # Example |
12 | | //! |
13 | | //! ```no_run |
14 | | //! use std::io; |
15 | | //! |
16 | | //! // Uncompress input and print the result. |
17 | | //! zstd::stream::copy_decode(io::stdin(), io::stdout()).unwrap(); |
18 | | //! ``` |
19 | | //! |
20 | | //! # Cargo features |
21 | | //! |
22 | | //! Enabled by default: `legacy`, `arrays`, `zdict_builder`. |
23 | | //! |
24 | | //! | Feature | Default | Description | |
25 | | //! | --- | --- | --- | |
26 | | //! | `arrays` | yes | Use fixed-size arrays (`[u8; N]`) as output buffers. | |
27 | | //! | `legacy` | yes | Decode frames written by zstd versions older than 0.8. | |
28 | | //! | `zdict_builder` | yes | Train new dictionaries. *Using* a dictionary always works. | |
29 | | //! | `experimental` | | Expose zstd's experimental API, which has no stability guarantees. | |
30 | | //! | `zstdmt` | | Multi-threaded compression inside the C library. | |
31 | | //! | `thin` | | Build a smaller C library, at some cost in speed and error reporting. | |
32 | | //! | `debug` | | Enable zstd's debug logs. | |
33 | | //! | `no_asm` | | Do not build the x86-64 assembly. | |
34 | | //! | `fat-lto`, `thin-lto` | | Cross-language LTO. Only works if `clang` builds the C library. | |
35 | | //! | `bindgen` | | Generate the bindings at build time rather than using the pre-generated ones. | |
36 | | //! | `pkg-config` | | Link against a system-installed libzstd instead of building the bundled source. | |
37 | | //! | `vendored` | | Always build the bundled source, even when `pkg-config` is also enabled. | |
38 | | //! | `cmake` | | Build the C library with zstd's own CMake files instead of the `cc` crate. | |
39 | | //! |
40 | | //! See the [readme] for more about the build-related ones. |
41 | | //! |
42 | | //! [zstd]: https://github.com/facebook/zstd |
43 | | //! [readme]: https://github.com/gyscos/zstd-rs#cargo-features |
44 | | #![deny(missing_docs)] |
45 | | #![cfg_attr(feature = "doc-cfg", feature(doc_cfg))] |
46 | | |
47 | | // Re-export the zstd-safe crate. |
48 | | pub use zstd_safe; |
49 | | |
50 | | pub mod bulk; |
51 | | pub mod dict; |
52 | | |
53 | | #[macro_use] |
54 | | pub mod stream; |
55 | | |
56 | | use std::io; |
57 | | |
58 | | /// Default compression level. |
59 | | pub use zstd_safe::CLEVEL_DEFAULT as DEFAULT_COMPRESSION_LEVEL; |
60 | | |
61 | | /// The accepted range of compression levels. |
62 | 0 | pub fn compression_level_range( |
63 | 0 | ) -> std::ops::RangeInclusive<zstd_safe::CompressionLevel> { |
64 | 0 | zstd_safe::min_c_level()..=zstd_safe::max_c_level() |
65 | 0 | } |
66 | | |
67 | | /// How large a buffer a compressed block might need, at worst. |
68 | | /// |
69 | | /// This is what to reserve before [`bulk::compress_to_buffer()`], which writes |
70 | | /// into a destination you provide rather than allocating one. |
71 | | /// |
72 | | /// Re-exported from `zstd_safe`, where it lives alongside the rest of the |
73 | | /// direct bindings. |
74 | | pub use zstd_safe::compress_bound; |
75 | | |
76 | | #[doc(no_inline)] |
77 | | pub use crate::stream::{ |
78 | | compress_file, compress_file_into, compress_sized, compress_sized_into, |
79 | | decode_all, decompressed_size, encode_all, Decoder, Encoder, |
80 | | }; |
81 | | |
82 | | /// Returns the error message as io::Error based on error_code. |
83 | 7 | fn map_error_code(code: usize) -> io::Error { |
84 | 7 | let msg = zstd_safe::get_error_name(code); |
85 | 7 | io::Error::new(io::ErrorKind::Other, msg.to_string()) |
86 | 7 | } |
87 | | |
88 | | // Some helper functions to write full-cycle tests. |
89 | | |
90 | | #[cfg(test)] |
91 | | fn test_cycle<F, G>(data: &[u8], f: F, g: G) |
92 | | where |
93 | | F: Fn(&[u8]) -> Vec<u8>, |
94 | | G: Fn(&[u8]) -> Vec<u8>, |
95 | | { |
96 | | let mid = f(data); |
97 | | let end = g(&mid); |
98 | | assert_eq!(data, &end[..]); |
99 | | } |
100 | | |
101 | | #[cfg(test)] |
102 | | fn test_cycle_unwrap<F, G>(data: &[u8], f: F, g: G) |
103 | | where |
104 | | F: Fn(&[u8]) -> io::Result<Vec<u8>>, |
105 | | G: Fn(&[u8]) -> io::Result<Vec<u8>>, |
106 | | { |
107 | | test_cycle(data, |data| f(data).unwrap(), |data| g(data).unwrap()) |
108 | | } |
109 | | |
110 | | #[test] |
111 | | fn default_compression_level_in_range() { |
112 | | assert!(compression_level_range().contains(&DEFAULT_COMPRESSION_LEVEL)); |
113 | | } |