From c6cbf6e15762b1ecbefeaeca956f893a50795fdf Mon Sep 17 00:00:00 2001 From: Talison Fabio <54823205+talis-fb@users.noreply.github.com> Date: Mon, 7 Apr 2025 21:15:22 -0300 Subject: [PATCH] perf: replace `.count()` from iterators to more performative operations --- src/archive/rar.rs | 2 +- src/archive/tar.rs | 2 +- src/archive/zip.rs | 2 +- src/commands/decompress.rs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/archive/rar.rs b/src/archive/rar.rs index c223192..ec2d1e6 100644 --- a/src/archive/rar.rs +++ b/src/archive/rar.rs @@ -18,7 +18,7 @@ pub fn unpack_archive( password: Option<&[u8]>, quiet: bool, ) -> crate::Result { - assert!(output_folder.read_dir().expect("dir exists").count() == 0); + assert!(output_folder.read_dir().expect("dir exists").next().is_none()); let archive = match password { Some(password) => Archive::with_password(archive_path, password), diff --git a/src/archive/tar.rs b/src/archive/tar.rs index fda05eb..3416078 100644 --- a/src/archive/tar.rs +++ b/src/archive/tar.rs @@ -24,7 +24,7 @@ use crate::{ /// Unpacks the archive given by `archive` into the folder given by `into`. /// Assumes that output_folder is empty pub fn unpack_archive(reader: Box, output_folder: &Path, quiet: bool) -> crate::Result { - assert!(output_folder.read_dir().expect("dir exists").count() == 0); + assert!(output_folder.read_dir().expect("dir exists").next().is_none()); let mut archive = tar::Archive::new(reader); let mut files_unpacked = 0; diff --git a/src/archive/zip.rs b/src/archive/zip.rs index 9995e07..f525d3b 100644 --- a/src/archive/zip.rs +++ b/src/archive/zip.rs @@ -37,7 +37,7 @@ pub fn unpack_archive( where R: Read + Seek, { - assert!(output_folder.read_dir().expect("dir exists").count() == 0); + assert!(output_folder.read_dir().expect("dir exists").next().is_none()); let mut unpacked_files = 0; diff --git a/src/commands/decompress.rs b/src/commands/decompress.rs index 706b6f0..981376f 100644 --- a/src/commands/decompress.rs +++ b/src/commands/decompress.rs @@ -356,7 +356,7 @@ fn smart_unpack( let files = unpack_fn(temp_dir_path)?; - let root_contains_only_one_element = fs::read_dir(temp_dir_path)?.count() == 1; + let root_contains_only_one_element = fs::read_dir(temp_dir_path)?.take(2).count() == 1; let (previous_path, mut new_path) = if root_contains_only_one_element { // Only one file in the root directory, so we can just move it to the output directory