| 1 | //! Train a dictionary from various sources. |
| 2 | //! |
| 3 | //! A dictionary can help improve the compression of small files. |
| 4 | //! The dictionary must be present during decompression, |
| 5 | //! but can be shared across multiple "similar" files. |
| 6 | //! |
| 7 | //! Creating a dictionary using the `zstd` C library, |
| 8 | //! using the `zstd` command-line interface, using this library, |
| 9 | //! or using the `train` binary provided, should give the same result, |
| 10 | //! and are therefore completely compatible. |
| 11 | //! |
| 12 | //! To use, see [`Encoder::with_dictionary`] or [`Decoder::with_dictionary`]. |
| 13 | //! |
| 14 | //! [`Encoder::with_dictionary`]: ../struct.Encoder.html#method.with_dictionary |
| 15 | //! [`Decoder::with_dictionary`]: ../struct.Decoder.html#method.with_dictionary |
| 16 | |
| 17 | #[cfg(feature = "zdict_builder")] |
| 18 | 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/::{selfrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/, Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#}; |
| 19 | |
| 20 | pub use zstd_saferust-analyzer cargo zstd-safe 7.2.2 crate/::{CDictrust-analyzer cargo zstd-safe 7.2.2 CDict#, DDictrust-analyzer cargo zstd-safe 7.2.2 DDict#}; |
| 21 | |
| 22 | /// Prepared dictionary for compression |
| 23 | /// |
| 24 | /// A dictionary can include its own copy of the data (if it is `'static`), or it can merely point |
| 25 | /// to a separate buffer (if it has another lifetime). |
| 26 | pub struct EncoderDictionaryrust-analyzer cargo zstd 0.13.3 dict/EncoderDictionary#<'a> { |
| 27 | cdictrust-analyzer cargo zstd 0.13.3 dict/EncoderDictionary#cdict.: CDictrust-analyzer cargo zstd-safe 7.2.2 CDict#<'a>, |
| 28 | } |
| 29 | |
| 30 | impl EncoderDictionaryrust-analyzer cargo zstd 0.13.3 dict/EncoderDictionary#<'static> { |
| 31 | /// Creates a prepared dictionary for compression. |
| 32 | /// |
| 33 | /// This will copy the dictionary internally. |
| 34 | pub fn copyrust-analyzer cargo zstd 0.13.3 dict/impl#[`EncoderDictionary<'static>`]copy().(dictionary: &[u8], level: i32) -> Selfrust-analyzer cargo zstd 0.13.3 dict/impl#[`EncoderDictionary<'static>`] { |
| 35 | Selfrust-analyzer cargo zstd 0.13.3 dict/impl#[`EncoderDictionary<'static>`] { |
| 36 | cdictrust-analyzer cargo zstd 0.13.3 dict/EncoderDictionary#cdict.: zstd_saferust-analyzer cargo zstd-safe 7.2.2 crate/::create_cdictrust-analyzer cargo zstd-safe 7.2.2 create_cdict().(dictionary, level), |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | impl<'a> EncoderDictionaryrust-analyzer cargo zstd 0.13.3 dict/EncoderDictionary#<'a> { |
| 42 | #[cfg(feature = "experimental")] |
| 43 | #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))] |
| 44 | /// Create prepared dictionary for compression |
| 45 | /// |
| 46 | /// A level of `0` uses zstd's default (currently `3`). |
| 47 | /// |
| 48 | /// Only available with the `experimental` feature. Use `EncoderDictionary::copy` otherwise. |
| 49 | pub fn newrust-analyzer cargo zstd 0.13.3 dict/impl#[`EncoderDictionary<'a>`]new().(dictionary: &'a [u8], level: i32) -> Selfrust-analyzer cargo zstd 0.13.3 dict/impl#[`EncoderDictionary<'a>`] { |
| 50 | Selfrust-analyzer cargo zstd 0.13.3 dict/impl#[`EncoderDictionary<'a>`] { |
| 51 | cdictrust-analyzer cargo zstd 0.13.3 dict/EncoderDictionary#cdict.: zstd_saferust-analyzer cargo zstd-safe 7.2.2 crate/::CDictrust-analyzer cargo zstd-safe 7.2.2 CDict#::create_by_referencerust-analyzer cargo zstd-safe 7.2.2 impl#[`CDict<'a>`]create_by_reference().(dictionary, level), |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /// Returns reference to `CDict` inner object |
| 56 | pub fn as_cdictrust-analyzer cargo zstd 0.13.3 dict/impl#[`EncoderDictionary<'a>`]as_cdict().(&self) -> &CDictrust-analyzer cargo zstd-safe 7.2.2 CDict#<'a> { |
| 57 | &self.cdictrust-analyzer cargo zstd 0.13.3 dict/EncoderDictionary#cdict. |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /// Prepared dictionary for decompression |
| 62 | pub struct DecoderDictionaryrust-analyzer cargo zstd 0.13.3 dict/DecoderDictionary#<'a> { |
| 63 | ddictrust-analyzer cargo zstd 0.13.3 dict/DecoderDictionary#ddict.: DDictrust-analyzer cargo zstd-safe 7.2.2 DDict#<'a>, |
| 64 | } |
| 65 | |
| 66 | impl DecoderDictionaryrust-analyzer cargo zstd 0.13.3 dict/DecoderDictionary#<'static> { |
| 67 | /// Create a prepared dictionary for decompression. |
| 68 | /// |
| 69 | /// This will copy the dictionary internally. |
| 70 | pub fn copyrust-analyzer cargo zstd 0.13.3 dict/impl#[`DecoderDictionary<'static>`]copy().(dictionary: &[u8]) -> Selfrust-analyzer cargo zstd 0.13.3 dict/impl#[`DecoderDictionary<'static>`] { |
| 71 | Selfrust-analyzer cargo zstd 0.13.3 dict/impl#[`DecoderDictionary<'static>`] { |
| 72 | ddictrust-analyzer cargo zstd 0.13.3 dict/DecoderDictionary#ddict.: zstd_saferust-analyzer cargo zstd-safe 7.2.2 crate/::DDictrust-analyzer cargo zstd-safe 7.2.2 DDict#::createrust-analyzer cargo zstd-safe 7.2.2 impl#[`DDict<'static>`]create().(dictionary), |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | impl<'a> DecoderDictionaryrust-analyzer cargo zstd 0.13.3 dict/DecoderDictionary#<'a> { |
| 78 | #[cfg(feature = "experimental")] |
| 79 | #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "experimental")))] |
| 80 | /// Create prepared dictionary for decompression |
| 81 | /// |
| 82 | /// Only available with the `experimental` feature. Use `DecoderDictionary::copy` otherwise. |
| 83 | pub fn newrust-analyzer cargo zstd 0.13.3 dict/impl#[`DecoderDictionary<'a>`]new().(dict: &'a [u8]) -> Selfrust-analyzer cargo zstd 0.13.3 dict/impl#[`DecoderDictionary<'a>`] { |
| 84 | Selfrust-analyzer cargo zstd 0.13.3 dict/impl#[`DecoderDictionary<'a>`] { |
| 85 | ddictrust-analyzer cargo zstd 0.13.3 dict/DecoderDictionary#ddict.: zstd_saferust-analyzer cargo zstd-safe 7.2.2 crate/::DDictrust-analyzer cargo zstd-safe 7.2.2 DDict#::create_by_referencerust-analyzer cargo zstd-safe 7.2.2 impl#[`DDict<'a>`]create_by_reference().(dict), |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /// Returns reference to `DDict` inner object |
| 90 | pub fn as_ddictrust-analyzer cargo zstd 0.13.3 dict/impl#[`DecoderDictionary<'a>`]as_ddict().(&self) -> &DDictrust-analyzer cargo zstd-safe 7.2.2 DDict#<'a> { |
| 91 | &self.ddictrust-analyzer cargo zstd 0.13.3 dict/DecoderDictionary#ddict. |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /// Train a dictionary from a big continuous chunk of data, with all samples |
| 96 | /// contiguous in memory. |
| 97 | /// |
| 98 | /// This is the most efficient way to train a dictionary, |
| 99 | /// since this is directly fed into `zstd`. |
| 100 | /// |
| 101 | /// * `sample_data` is the concatenation of all sample data. |
| 102 | /// * `sample_sizes` is the size of each sample in `sample_data`. |
| 103 | /// The sum of all `sample_sizes` should equal the length of `sample_data`. |
| 104 | /// * `max_size` is the maximum size of the dictionary to generate. |
| 105 | /// |
| 106 | /// The result is the dictionary data. You can, for example, feed it to [`CDict::create`]. |
| 107 | #[cfg(feature = "zdict_builder")] |
| 108 | #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "zdict_builder")))] |
| 109 | pub fn from_continuousrust-analyzer cargo zstd 0.13.3 dict/from_continuous().( |
| 110 | sample_data: &[u8], |
| 111 | sample_sizes: &[usize], |
| 112 | max_size: usize, |
| 113 | ) -> 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>> { |
| 114 | use craterust-analyzer cargo zstd 0.13.3 crate/::map_error_coderust-analyzer cargo zstd 0.13.3 map_error_code().; |
| 115 | |
| 116 | // Complain if the lengths don't add up to the entire data. |
| 117 | if sample_sizes.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().sumrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#sum().::<usize>() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().sample_data.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() { |
| 118 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().( |
| 119 | iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::Otherrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#Other#, |
| 120 | "sample sizes don't add up".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), |
| 121 | )); |
| 122 | } |
| 123 | |
| 124 | let mut result = 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().(max_size); |
| 125 | zstd_saferust-analyzer cargo zstd-safe 7.2.2 crate/::train_from_bufferrust-analyzer cargo zstd-safe 7.2.2 train_from_buffer().(&mut result, sample_data, sample_sizes) |
| 126 | .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().; |
| 127 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(result) |
| 128 | } |
| 129 | |
| 130 | /// Train a dictionary from multiple samples. |
| 131 | /// |
| 132 | /// The samples will internally be copied to a single continuous buffer, |
| 133 | /// so make sure you have enough memory available. |
| 134 | /// |
| 135 | /// If you need to stretch your system's limits, |
| 136 | /// [`from_continuous`] directly uses the given slice. |
| 137 | /// |
| 138 | /// [`from_continuous`]: ./fn.from_continuous.html |
| 139 | /// |
| 140 | /// * `samples` is a list of individual samples to train on. |
| 141 | /// * `max_size` is the maximum size of the dictionary to generate. |
| 142 | /// |
| 143 | /// The result is the dictionary data. You can, for example, feed it to [`CDict::create`]. |
| 144 | #[cfg(feature = "zdict_builder")] |
| 145 | #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "zdict_builder")))] |
| 146 | pub fn from_samplesrust-analyzer cargo zstd 0.13.3 dict/from_samples().<S: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[u8]>>( |
| 147 | samples: &[S], |
| 148 | max_size: usize, |
| 149 | ) -> 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>> { |
| 150 | // Pre-allocate the entire required size. |
| 151 | let total_length: usize = |
| 152 | samples.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|sample| sample.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().().lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()).sumrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#sum().(); |
| 153 | |
| 154 | let mut data = 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().(total_length); |
| 155 | |
| 156 | // Copy every sample to a big chunk of memory |
| 157 | data.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<T>`]extend().(samples.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().flat_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#flat_map().(|s| s.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().()).clonedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#cloned().()); |
| 158 | |
| 159 | let sizes: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = samples.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|s| s.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().().lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 160 | |
| 161 | from_continuousrust-analyzer cargo zstd 0.13.3 dict/from_continuous().(&data, &sizes, max_size) |
| 162 | } |
| 163 | |
| 164 | /// Train a dictionary from multiple samples. |
| 165 | /// |
| 166 | /// Unlike [`from_samples`], this does not require having a list of all samples. |
| 167 | /// It also allows running into an error when iterating through the samples. |
| 168 | /// |
| 169 | /// They will still be copied to a continuous array and fed to [`from_continuous`]. |
| 170 | /// |
| 171 | /// * `samples` is an iterator of individual samples to train on. |
| 172 | /// * `max_size` is the maximum size of the dictionary to generate. |
| 173 | /// |
| 174 | /// The result is the dictionary data. You can, for example, feed it to [`CDict::create`]. |
| 175 | /// |
| 176 | /// # Examples |
| 177 | /// |
| 178 | /// ```rust,no_run |
| 179 | /// // Train from a couple of json files. |
| 180 | /// let dict_buffer = zstd::dict::from_sample_iterator( |
| 181 | /// ["file_a.json", "file_b.json"] |
| 182 | /// .into_iter() |
| 183 | /// .map(|filename| std::fs::File::open(filename)), |
| 184 | /// 10_000, // 10kB dictionary |
| 185 | /// ).unwrap(); |
| 186 | /// ``` |
| 187 | /// |
| 188 | /// ```rust,no_run |
| 189 | /// use std::io::BufRead as _; |
| 190 | /// // Treat each line from stdin as a separate sample. |
| 191 | /// let dict_buffer = zstd::dict::from_sample_iterator( |
| 192 | /// std::io::stdin().lock().lines().map(|line: std::io::Result<String>| { |
| 193 | /// // Transform each line into a `Cursor<Vec<u8>>` so they implement Read. |
| 194 | /// line.map(String::into_bytes) |
| 195 | /// .map(std::io::Cursor::new) |
| 196 | /// }), |
| 197 | /// 10_000, // 10kB dictionary |
| 198 | /// ).unwrap(); |
| 199 | /// ``` |
| 200 | #[cfg(feature = "zdict_builder")] |
| 201 | #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "zdict_builder")))] |
| 202 | pub fn from_sample_iteratorrust-analyzer cargo zstd 0.13.3 dict/from_sample_iterator().<I, R>( |
| 203 | samples: I, |
| 204 | max_size: usize, |
| 205 | ) -> 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>> |
| 206 | where |
| 207 | I: IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#Item# = 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#<R>>, |
| 208 | R: Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#, |
| 209 | { |
| 210 | let mut data = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 211 | let mut sizes = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 212 | |
| 213 | for sample in samples { |
| 214 | let mut sample = sample?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 215 | let len = sample.read_to_endrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_to_end().(&mut data)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 216 | sizes.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(len); |
| 217 | } |
| 218 | |
| 219 | from_continuousrust-analyzer cargo zstd 0.13.3 dict/from_continuous().(&data, &sizes, max_size) |
| 220 | } |
| 221 | |
| 222 | /// Train a dict from a list of files. |
| 223 | /// |
| 224 | /// * `filenames` is an iterator of files to load. Each file will be treated as an individual |
| 225 | /// sample. |
| 226 | /// * `max_size` is the maximum size of the dictionary to generate. |
| 227 | /// |
| 228 | /// The result is the dictionary data. You can, for example, feed it to [`CDict::create`]. |
| 229 | #[cfg(feature = "zdict_builder")] |
| 230 | #[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "zdict_builder")))] |
| 231 | pub fn from_filesrust-analyzer cargo zstd 0.13.3 dict/from_files().<I, P>(filenames: I, max_size: usize) -> 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>> |
| 232 | where |
| 233 | P: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#>, |
| 234 | I: IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#Item# = P>, |
| 235 | { |
| 236 | from_sample_iteratorrust-analyzer cargo zstd 0.13.3 dict/from_sample_iterator().( |
| 237 | filenames |
| 238 | .into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#into_iter().() |
| 239 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|filename| stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]open().(filename)), |
| 240 | max_size, |
| 241 | ) |
| 242 | } |
| 243 | |
| 244 | #[cfg(test)] |
| 245 | #[cfg(feature = "zdict_builder")] |
| 246 | mod testsrust-analyzer cargo zstd 0.13.3 dict/tests/ { |
| 247 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/; |
| 248 | 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/; |
| 249 | 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/::Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#; |
| 250 | |
| 251 | use walkdirrust-analyzer cargo walkdir 2.5.0 crate/; |
| 252 | |
| 253 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 254 | fn test_dict_trainingrust-analyzer cargo zstd 0.13.3 dict/tests/test_dict_training().() { |
| 255 | // Train a dictionary |
| 256 | let paths: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = walkdirrust-analyzer cargo walkdir 2.5.0 crate/::WalkDirrust-analyzer cargo walkdir 2.5.0 WalkDir#::newrust-analyzer cargo walkdir 2.5.0 impl#[WalkDir]new().("src") |
| 257 | .into_iterrust-analyzer cargo walkdir 2.5.0 impl#[WalkDir][IntoIterator]into_iter().() |
| 258 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|entry| entry.unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()) |
| 259 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|entry| entry.into_pathrust-analyzer cargo walkdir 2.5.0 dent/impl#[DirEntry]into_path().()) |
| 260 | .filterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter().(|path| path.to_strrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]to_str().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().ends_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]ends_with().(".rs")) |
| 261 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 262 | |
| 263 | let dict = superrust-analyzer cargo zstd 0.13.3 dict/::from_filesrust-analyzer cargo zstd 0.13.3 dict/from_files().(&paths, 4000).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 264 | |
| 265 | for path in paths { |
| 266 | let mut buffer = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 267 | let mut file = fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]open().(path).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 268 | let mut content = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 269 | file.read_to_endrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File][Read]read_to_end().(&mut content).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 270 | iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::copyrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/copy/copy().( |
| 271 | &mut &content[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()., |
| 272 | &mut craterust-analyzer cargo zstd 0.13.3 crate/::streamrust-analyzer cargo zstd 0.13.3 stream/::Encoderrust-analyzer cargo zstd 0.13.3 stream/write/Encoder#::with_dictionaryrust-analyzer cargo zstd 0.13.3 stream/write/impl#[`Encoder<'static, W>`]with_dictionary().( |
| 273 | &mut buffer, |
| 274 | 1, |
| 275 | &dict, |
| 276 | ) |
| 277 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().() |
| 278 | .auto_finishrust-analyzer cargo zstd 0.13.3 stream/write/impl#[`Encoder<'a, W>`]auto_finish().(), |
| 279 | ) |
| 280 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 281 | |
| 282 | let mut result = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 283 | iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::copyrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/copy/copy().( |
| 284 | &mut craterust-analyzer cargo zstd 0.13.3 crate/::streamrust-analyzer cargo zstd 0.13.3 stream/::Decoderrust-analyzer cargo zstd 0.13.3 stream/read/Decoder#::with_dictionaryrust-analyzer cargo zstd 0.13.3 stream/read/impl#[`Decoder<'static, R>`]with_dictionary().( |
| 285 | &buffer[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()., |
| 286 | &dict[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()., |
| 287 | ) |
| 288 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), |
| 289 | &mut result, |
| 290 | ) |
| 291 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 292 | |
| 293 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&content, &result); |
| 294 | } |
| 295 | } |
| 296 | } |