MCPcopy Create free account
hub / github.com/archlinux/alpm / create_zstd_encoder

Function create_zstd_encoder

alpm-compress/src/compression/encoder.rs:31–75  ·  view source on GitHub ↗

Creates and configures an [`Encoder`]. Uses a dedicated `compression_level` and amount of `threads` to construct and configure an encoder for zstd compression. The `settings` are merely used for additional context in cases of error. # Errors Returns an error if - the encoder cannot be created using the `file` and `compression_level`, - the encoder cannot be configured to use checksums at the e

(
    file: File,
    compression_level: &ZstdCompressionLevel,
    threads: &ZstdThreads,
    settings: &CompressionSettings,
)

Source from the content-addressed store, hash-verified

29/// - the amount of physical CPU cores can not be turned into a `u32`,
30/// - or multithreading can not be enabled based on the provided `threads` settings.
31fn create_zstd_encoder(
32 file: File,
33 compression_level: &ZstdCompressionLevel,
34 threads: &ZstdThreads,
35 settings: &CompressionSettings,
36) -> Result<Encoder<'static, File>, Error> {
37 let mut encoder = Encoder::new(file, compression_level.into()).map_err(|source| {
38 Error::CreateZstandardEncoder {
39 context: t!("error-create-zstd-encoder-init"),
40 compression_settings: settings.clone(),
41 source,
42 }
43 })?;
44
45 // Include a context checksum at the end of each frame.
46 encoder
47 .include_checksum(true)
48 .map_err(|source| Error::CreateZstandardEncoder {
49 context: t!("error-create-zstd-encoder-set-checksum"),
50 compression_settings: settings.clone(),
51 source,
52 })?;
53
54 // Get amount of threads to use.
55 let threads = match threads {
56 // Use available physical CPU cores if the special value `0` is used.
57 // NOTE: For the zstd executable `0` means "use all available threads", while for the zstd
58 // crate this means "disable multithreading".
59 ZstdThreads(0) => {
60 u32::try_from(num_cpus::get_physical()).map_err(Error::IntegerConversion)?
61 }
62 ZstdThreads(threads) => *threads,
63 };
64
65 // Use multi-threading if it is available.
66 encoder
67 .multithread(threads)
68 .map_err(|source| Error::CreateZstandardEncoder {
69 context: t!("error-create-zstd-encoder-set-threads"),
70 compression_settings: settings.clone(),
71 source,
72 })?;
73
74 Ok(encoder)
75}
76
77/// Encoder for compression which supports multiple backends.
78///

Callers 1

newMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected