| 1 | use craterust-analyzer cargo zstd 0.13.3 crate/::map_error_coderust-analyzer cargo zstd 0.13.3 map_error_code().; |
| 2 | |
| 3 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/; |
| 4 | use zstd_saferust-analyzer cargo zstd-safe 7.2.2 crate/; |
| 5 | |
| 6 | /// Allows to compress independently multiple chunks of data. |
| 7 | /// |
| 8 | /// Each job will be processed entirely in-memory without streaming, so this |
| 9 | /// is most fitting for many small jobs. To compress larger volume that don't |
| 10 | /// easily fit in memory, a streaming compression may be more appropriate. |
| 11 | /// |
| 12 | /// It is more efficient than a streaming compressor for 2 reasons: |
| 13 | /// * It re-uses the zstd context between jobs to avoid re-allocations |
| 14 | /// * It avoids copying data from a `Read` into a temporary buffer before compression. |
| 15 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)] |
| 16 | pub struct Compressorrust-analyzer cargo zstd 0.13.3 bulk/compressor/Compressor#<'a> { |
| 17 | contextrust-analyzer cargo zstd 0.13.3 bulk/compressor/Compressor#context.: zstd_saferust-analyzer cargo zstd-safe 7.2.2 crate/::CCtxrust-analyzer cargo zstd-safe 7.2.2 CCtx#<'a>, |
| 18 | } |
| 19 | |
| 20 | impl Compressorrust-analyzer cargo zstd 0.13.3 bulk/compressor/Compressor#<'static> { |
| 21 | /// Creates a new zstd compressor |
| 22 | pub fn newrust-analyzer cargo zstd 0.13.3 bulk/compressor/impl#[`Compressor<'static>`]new().(level: i32) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<Selfrust-analyzer cargo zstd 0.13.3 bulk/compressor/impl#[`Compressor<'static>`]> { |
| 23 | Selfrust-analyzer cargo zstd 0.13.3 bulk/compressor/impl#[`Compressor<'static>`]::with_dictionaryrust-analyzer cargo zstd 0.13.3 bulk/compressor/impl#[`Compressor<'static>`]with_dictionary().(level, &[]) |
| 24 | } |
| 25 | |
| 26 | /// Creates a new zstd compressor, using the given dictionary. |
| 27 | /// |
| 28 | /// Note that using a dictionary means that decompression will need to use |
| 29 | /// the same dictionary. |
| 30 | pub fn with_dictionaryrust-analyzer cargo zstd 0.13.3 bulk/compressor/impl#[`Compressor<'static>`]with_dictionary().(level: i32, dictionary: &[u8]) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<Selfrust-analyzer cargo zstd 0.13.3 bulk/compressor/impl#[`Compressor<'static>`]> { |
| 31 | let mut compressor = Selfrust-analyzer cargo zstd 0.13.3 bulk/compressor/impl#[`Compressor<'static>`]::defaultrust-analyzer cargo zstd 0.13.3 bulk/compressor/impl#[`Compressor<'a>`][Default]default().(); |
| 32 | |
| 33 | compressor.set_dictionaryrust-analyzer cargo zstd 0.13.3 bulk/compressor/impl#[`Compressor<'a>`]set_dictionary().(level, dictionary)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 34 | |
| 35 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(compressor) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | impl<'a> Compressorrust-analyzer cargo zstd 0.13.3 bulk/compressor/Compressor#<'a> { |
| 40 | /// Creates a new compressor using an existing `EncoderDictionary`. |
| 41 | /// |
| 42 | /// The compression level will be the one specified when creating the dictionary. |
| 43 | /// |
| 44 | /// Note that using a dictionary means that decompression will need to use |
| 45 | /// the same dictionary. |
| 46 | pub fn with_prepared_dictionaryrust-analyzer cargo zstd 0.13.3 bulk/compressor/impl#[`Compressor<'a>`]with_prepared_dictionary().<'b>( |
| 47 | dictionary: &'a craterust-analyzer cargo zstd 0.13.3 crate/::dictrust-analyzer cargo zstd 0.13.3 dict/::EncoderDictionaryrust-analyzer cargo zstd 0.13.3 dict/EncoderDictionary#<'b>, |
| 48 | ) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<Selfrust-analyzer cargo zstd 0.13.3 bulk/compressor/impl#[`Compressor<'a>`]> |
| 49 | where |
| 50 | 'b: 'a, |
| 51 | { |
| 52 | let mut compressor = Selfrust-analyzer cargo zstd 0.13.3 bulk/compressor/impl#[`Compressor<'a>`]::defaultrust-analyzer cargo zstd 0.13.3 bulk/compressor/impl#[`Compressor<'a>`][Default]default().(); |
| 53 | |
| 54 | compressor.set_prepared_dictionaryrust-analyzer cargo zstd 0.13.3 bulk/compressor/impl#[`Compressor<'a>`]set_prepared_dictionary().(dictionary)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 55 | |
| 56 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(compressor) |
| 57 | } |
| 58 | |
| 59 | /// Changes the compression level used by this compressor. |
| 60 | /// |
| 61 | /// *This will clear any dictionary previously registered.* |
| 62 | /// |
| 63 | /// If you want to keep the existing dictionary, you will need to pass it again to |
| 64 | /// `Self::set_dictionary` instead of using this method. |
| 65 | pub fn set_compression_levelrust-analyzer cargo zstd 0.13.3 bulk/compressor/impl#[`Compressor<'a>`]set_compression_level().(&mut self, level: i32) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()> { |
| 66 | self.set_dictionaryrust-analyzer cargo zstd 0.13.3 bulk/compressor/impl#[`Compressor<'a>`]set_dictionary().(level, &[]) |
| 67 | } |
| 68 | |
| 69 | /// Changes the dictionary and compression level used by this compressor. |
| 70 | /// |
| 71 | /// Will affect future compression jobs. |
| 72 | /// |
| 73 | /// Note that using a dictionary means that decompression will need to use |
| 74 | /// the same dictionary. |
| 75 | pub fn set_dictionaryrust-analyzer cargo zstd 0.13.3 bulk/compressor/impl#[`Compressor<'a>`]set_dictionary().( |
| 76 | &mut self, |
| 77 | level: i32, |
| 78 | dictionary: &[u8], |
| 79 | ) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()> { |
| 80 | self.contextrust-analyzer cargo zstd 0.13.3 bulk/compressor/Compressor#context. |
| 81 | .set_parameterrust-analyzer cargo zstd-safe 7.2.2 impl#[`CCtx<'a>`]set_parameter().(zstd_saferust-analyzer cargo zstd-safe 7.2.2 crate/::CParameterrust-analyzer cargo zstd-safe 7.2.2 CParameter#::CompressionLevelrust-analyzer cargo zstd-safe 7.2.2 CParameter#CompressionLevel#(level)) |
| 82 | .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(map_error_coderust-analyzer cargo zstd 0.13.3 map_error_code().)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 83 | |
| 84 | self.contextrust-analyzer cargo zstd 0.13.3 bulk/compressor/Compressor#context. |
| 85 | .load_dictionaryrust-analyzer cargo zstd-safe 7.2.2 impl#[`CCtx<'a>`]load_dictionary().(dictionary) |
| 86 | .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(map_error_coderust-analyzer cargo zstd 0.13.3 map_error_code().)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 87 | |
| 88 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 89 | } |
| 90 | |
| 91 | /// Changes the dictionary used by this compressor. |
| 92 | /// |
| 93 | /// The compression level used when preparing the dictionary will be used. |
| 94 | /// |
| 95 | /// Note that using a dictionary means that decompression will need to use |
| 96 | /// the same dictionary. |
| 97 | pub fn set_prepared_dictionaryrust-analyzer cargo zstd 0.13.3 bulk/compressor/impl#[`Compressor<'a>`]set_prepared_dictionary().<'b>( |
| 98 | &mut self, |
| 99 | dictionary: &'a craterust-analyzer cargo zstd 0.13.3 crate/::dictrust-analyzer cargo zstd 0.13.3 dict/::EncoderDictionaryrust-analyzer cargo zstd 0.13.3 dict/EncoderDictionary#<'b>, |
| 100 | ) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()> |
| 101 | where |
| 102 | 'b: 'a, |
| 103 | { |
| 104 | self.contextrust-analyzer cargo zstd 0.13.3 bulk/compressor/Compressor#context. |
| 105 | .ref_cdictrust-analyzer cargo zstd-safe 7.2.2 impl#[`CCtx<'a>`]ref_cdict().(dictionary.as_cdictrust-analyzer cargo zstd 0.13.3 dict/impl#[`EncoderDictionary<'a>`]as_cdict().()) |
| 106 | .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(map_error_coderust-analyzer cargo zstd 0.13.3 map_error_code().)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 107 | |
| 108 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 109 | } |
| 110 | |
| 111 | /// Compress a single block of data to the given destination buffer. |
| 112 | /// |
| 113 | /// Returns the number of bytes written, or an error if something happened |
| 114 | /// (for instance if the destination buffer was too small). |
| 115 | /// |
| 116 | /// A level of `0` uses zstd's default (currently `3`). |
| 117 | pub fn compress_to_bufferrust-analyzer cargo zstd 0.13.3 bulk/compressor/impl#[`Compressor<'a>`]compress_to_buffer().<C: zstd_saferust-analyzer cargo zstd-safe 7.2.2 crate/::WriteBufrust-analyzer cargo zstd-safe 7.2.2 WriteBuf# + ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#>( |
| 118 | &mut self, |
| 119 | source: &[u8], |
| 120 | destination: &mut C, |
| 121 | ) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<usize> { |
| 122 | self.contextrust-analyzer cargo zstd 0.13.3 bulk/compressor/Compressor#context. |
| 123 | .compress2rust-analyzer cargo zstd-safe 7.2.2 impl#[`CCtx<'a>`]compress2().(destination, source) |
| 124 | .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(map_error_coderust-analyzer cargo zstd 0.13.3 map_error_code().) |
| 125 | } |
| 126 | |
| 127 | /// Compresses a block of data and returns the compressed result. |
| 128 | /// |
| 129 | /// A level of `0` uses zstd's default (currently `3`). |
| 130 | pub fn compressrust-analyzer cargo zstd 0.13.3 bulk/compressor/impl#[`Compressor<'a>`]compress().(&mut self, data: &[u8]) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>> { |
| 131 | // We allocate a big buffer, slightly larger than the input data. |
| 132 | let buffer_len = zstd_saferust-analyzer cargo zstd-safe 7.2.2 crate/::compress_boundrust-analyzer cargo zstd-safe 7.2.2 compress_bound().(data.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()); |
| 133 | let mut buffer = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]with_capacity().(buffer_len); |
| 134 | |
| 135 | self.compress_to_bufferrust-analyzer cargo zstd 0.13.3 bulk/compressor/impl#[`Compressor<'a>`]compress_to_buffer().(data, &mut buffer)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 136 | |
| 137 | // Should we shrink the vec? Meh, let the user do it if he wants. |
| 138 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(buffer) |
| 139 | } |
| 140 | |
| 141 | /// Gives mutable access to the internal context. |
| 142 | pub fn context_mutrust-analyzer cargo zstd 0.13.3 bulk/compressor/impl#[`Compressor<'a>`]context_mut().(&mut self) -> &mut zstd_saferust-analyzer cargo zstd-safe 7.2.2 crate/::CCtxrust-analyzer cargo zstd-safe 7.2.2 CCtx#<'a> { |
| 143 | &mut self.contextrust-analyzer cargo zstd 0.13.3 bulk/compressor/Compressor#context. |
| 144 | } |
| 145 | |
| 146 | /// Sets a compression parameter for this compressor. |
| 147 | pub fn set_parameterrust-analyzer cargo zstd 0.13.3 bulk/compressor/impl#[`Compressor<'a>`]set_parameter().( |
| 148 | &mut self, |
| 149 | parameter: zstd_saferust-analyzer cargo zstd-safe 7.2.2 crate/::CParameterrust-analyzer cargo zstd-safe 7.2.2 CParameter#, |
| 150 | ) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()> { |
| 151 | self.contextrust-analyzer cargo zstd 0.13.3 bulk/compressor/Compressor#context. |
| 152 | .set_parameterrust-analyzer cargo zstd-safe 7.2.2 impl#[`CCtx<'a>`]set_parameter().(parameter) |
| 153 | .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(map_error_coderust-analyzer cargo zstd 0.13.3 map_error_code().)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 154 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 155 | } |
| 156 | |
| 157 | craterust-analyzer cargo zstd 0.13.3 crate/::encoder_parametersrust-analyzer cargo zstd 0.13.3 stream/encoder_parameters!!(); |
| 158 | } |
| 159 | |
| 160 | fn _assert_traitsrust-analyzer cargo zstd 0.13.3 bulk/compressor/_assert_traits().() { |
| 161 | fn _assert_sendrust-analyzer cargo zstd 0.13.3 bulk/compressor/_assert_send().<T: Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send#>(_: T) {} |
| 162 | |
| 163 | _assert_sendrust-analyzer cargo zstd 0.13.3 bulk/compressor/_assert_send().(Compressorrust-analyzer cargo zstd 0.13.3 bulk/compressor/Compressor#::newrust-analyzer cargo zstd 0.13.3 bulk/compressor/impl#[`Compressor<'static>`]new().(0)); |
| 164 | } |