From a798d201069ae35c37019d96eb8bcb167a37f501 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Rodrigues=20Miguel?= Date: Wed, 3 Nov 2021 12:16:20 -0300 Subject: [PATCH] extension: Use hardcoded slices instead of `Vecs` when creating an `Extension` --- src/commands.rs | 4 ++-- src/extension.rs | 36 +++++++++++++++++++----------------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/commands.rs b/src/commands.rs index c9010d49d..9a8309f68 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -128,7 +128,7 @@ pub fn run(args: Opts, question_policy: QuestionPolicy) -> crate::Result<()> { } else if inp_ext .compression_formats .iter() - .zip(&out_ext.compression_formats) + .zip(out_ext.compression_formats.iter()) .all(|(inp, out)| inp == out) { let new_ext = Extension::new( @@ -348,7 +348,7 @@ fn decompress_file( // in-memory decompression/copying first. // // Any other Zip decompression done can take up the whole RAM and freeze ouch. - if formats.len() == 1 && *formats[0].compression_formats.as_slice() == [Zip] { + if formats.len() == 1 && *formats[0].compression_formats == [Zip] { utils::create_dir_if_non_existent(output_dir)?; let zip_archive = zip::ZipArchive::new(reader)?; let _files = crate::archive::zip::unpack_archive(zip_archive, output_dir, question_policy)?; diff --git a/src/extension.rs b/src/extension.rs index 610eec322..24465c03b 100644 --- a/src/extension.rs +++ b/src/extension.rs @@ -8,7 +8,7 @@ use self::CompressionFormat::*; #[derive(Debug, Clone, PartialEq, Eq)] pub struct Extension { /// One extension like "tgz" can be made of multiple CompressionFormats ([Tar, Gz]) - pub compression_formats: Vec, + pub compression_formats: &'static [CompressionFormat], /// The input text for this extension, like "tgz", "tar" or "xz" pub display_text: String, } @@ -16,8 +16,7 @@ pub struct Extension { impl Extension { /// # Panics: /// Will panic if `formats` is empty - pub fn new(formats: impl Into>, text: impl Into) -> Self { - let formats = formats.into(); + pub fn new(formats: &'static [CompressionFormat], text: impl Into) -> Self { assert!(!formats.is_empty()); Self { compression_formats: formats, display_text: text.into() } } @@ -110,21 +109,24 @@ pub fn separate_known_extensions_from_name(mut path: &Path) -> (&Path, Vec Extension::new([Tar], extension), - "tgz" => Extension::new([Tar, Gzip], extension), - "tbz" | "tbz2" => Extension::new([Tar, Bzip], extension), - "tlz4" => Extension::new([Tar, Lz4], extension), - "txz" | "tlz" | "tlzma" => Extension::new([Tar, Lzma], extension), - "tzst" => Extension::new([Tar, Zstd], ".tzst"), - "zip" => Extension::new([Zip], extension), - "bz" | "bz2" => Extension::new([Bzip], extension), - "gz" => Extension::new([Gzip], extension), - "lz4" => Extension::new([Lz4], extension), - "xz" | "lzma" | "lz" => Extension::new([Lzma], extension), - "zst" => Extension::new([Zstd], extension), + let formats: &[CompressionFormat] = match extension { + "tar" => &[Tar], + "tgz" => &[Tar, Gzip], + "tbz" | "tbz2" => &[Tar, Bzip], + "tlz4" => &[Tar, Lz4], + "txz" | "tlz" | "tlzma" => &[Tar, Lzma], + "tzst" => &[Tar, Zstd], + "zip" => &[Zip], + "bz" | "bz2" => &[Bzip], + "gz" => &[Gzip], + "lz4" => &[Lz4], + "xz" | "lzma" | "lz" => &[Lzma], + "zst" => &[Zstd], _ => break, - }); + }; + + let extension = Extension::new(formats, extension); + extensions.push(extension); // Update for the next iteration path = if let Some(stem) = path.file_stem() { Path::new(stem) } else { Path::new("") };