From 40ec1c7394e8ed7475df39ec59fab7bd3b1870cf Mon Sep 17 00:00:00 2001 From: Martin Liska Date: Thu, 12 Sep 2024 15:27:07 +0200 Subject: [PATCH] Use itertools (collect_vec, try_collect) --- linker-diff/src/header_diff.rs | 6 ++---- linker-diff/src/lib.rs | 20 +++++++++----------- wild/tests/integration_tests.rs | 12 ++++++------ wild_lib/src/args.rs | 3 ++- wild_lib/src/layout.rs | 4 ++-- wild_lib/src/output_section_id.rs | 2 +- wild_lib/src/symbol_db.rs | 2 +- 7 files changed, 23 insertions(+), 26 deletions(-) diff --git a/linker-diff/src/header_diff.rs b/linker-diff/src/header_diff.rs index d6fb8506..d49f277c 100644 --- a/linker-diff/src/header_diff.rs +++ b/linker-diff/src/header_diff.rs @@ -8,6 +8,7 @@ use crate::Result; use anyhow::anyhow; use anyhow::bail; use anyhow::Context as _; +use itertools::Itertools; use linker_utils::elf::shf; use linker_utils::elf::SectionFlags; use object::elf::*; @@ -223,10 +224,7 @@ pub(crate) fn diff_fields( table_name: &str, diff_mode: DiffMode, ) -> Vec { - let field_values = objects - .iter() - .map(get_fields_fn) - .collect::>>(); + let field_values = objects.iter().map(get_fields_fn).collect_vec(); if diff_mode == DiffMode::IgnoreIfAllErrors && field_values.iter().all(|d| d.is_err()) { return vec![]; } diff --git a/linker-diff/src/lib.rs b/linker-diff/src/lib.rs index 700e7599..50ca82cb 100644 --- a/linker-diff/src/lib.rs +++ b/linker-diff/src/lib.rs @@ -329,7 +329,7 @@ impl Report { } let display_names = short_file_display_names(&config)?; - let file_bytes = config + let file_bytes: Vec<_> = config .filenames() .map(|filename| -> Result> { let mut bytes = std::fs::read(filename) @@ -339,19 +339,19 @@ impl Report { })?; Ok(bytes) }) - .collect::>>>()?; + .try_collect()?; - let elf_files = file_bytes + let elf_files: Vec<_> = file_bytes .iter() .map(|bytes| -> Result { Ok(ElfFile64::parse(bytes.as_slice())?) }) - .collect::>>()?; + .try_collect()?; - let layouts = config + let layouts: Vec<_> = config .filenames() .map(|p| LayoutAndFiles::from_base_path(p)) - .collect::>>()?; + .try_collect()?; - let objects = elf_files + let objects: Vec<_> = elf_files .iter() .zip(display_names) .zip(config.filenames()) @@ -359,7 +359,7 @@ impl Report { .map(|(((elf_file, name), path), layout)| -> Result { Object::new(elf_file, name, path.clone(), layout.as_ref()) }) - .collect::>>()?; + .try_collect()?; let mut report = Report { names: objects.iter().map(|o| o.name.clone()).collect(), @@ -593,9 +593,7 @@ fn short_file_display_names(config: &Config) -> Result> { } names = names .iter() - .map(|name| { - String::from_utf8_lossy(&name.bytes().skip(n).collect::>()).into_owned() - }) + .map(|name| String::from_utf8_lossy(&name.bytes().skip(n).collect_vec()).into_owned()) .collect_vec(); Ok(names) } diff --git a/wild/tests/integration_tests.rs b/wild/tests/integration_tests.rs index 5215db14..cf17b4d0 100644 --- a/wild/tests/integration_tests.rs +++ b/wild/tests/integration_tests.rs @@ -425,14 +425,14 @@ impl ProgramInputs { config, linker, ); - let inputs = std::iter::once(primary) + let inputs: Vec<_> = std::iter::once(primary) .chain( config .deps .iter() .map(|dep| build_linker_input(dep, config, linker)), ) - .collect::>>()?; + .try_collect()?; let link_output = linker.link(self.name(), &inputs, config)?; let shared_objects = inputs @@ -1057,11 +1057,11 @@ impl Display for LinkCommand { for sub in &self.input_commands { writeln!(f, "{sub}")?; } - let mut args: Vec<_> = self + let mut args = self .command .get_args() .map(|a| a.to_string_lossy()) - .collect(); + .collect_vec(); match (self.invocation_mode, &self.linker) { (LinkerInvocationMode::Cc, Linker::Wild) => { write!( @@ -1358,7 +1358,7 @@ fn integration_test( let configs = parse_configs(&src_path(filename)) .with_context(|| format!("Failed to parse test parameters from `{filename}`"))?; for config in configs { - let programs = linkers + let programs: Vec<_> = linkers .iter() .filter(|linker| config.is_linker_enabled(linker)) .map(|linker| { @@ -1381,7 +1381,7 @@ fn integration_test( } result }) - .collect::>>()?; + .try_collect()?; let start = Instant::now(); diff_shared_objects(&config, &programs)?; diff --git a/wild_lib/src/args.rs b/wild_lib/src/args.rs index d063b163..6d3686d6 100644 --- a/wild_lib/src/args.rs +++ b/wild_lib/src/args.rs @@ -637,6 +637,7 @@ mod tests { use super::IGNORED_FLAGS; use crate::args::Action; use crate::args::InputSpec; + use itertools::Itertools; use std::num::NonZeroUsize; use std::path::Path; use std::path::PathBuf; @@ -745,7 +746,7 @@ mod tests { InputSpec::File(_) => None, InputSpec::Lib(lib_name) => Some(lib_name.as_ref()), }) - .collect::>(), + .collect_vec(), &["gcc_s", "util", "rt", "pthread", "m", "dl", "c"] ); assert_contains(&args.lib_search_path, "/lib"); diff --git a/wild_lib/src/layout.rs b/wild_lib/src/layout.rs index 963a8df1..4d93d27a 100644 --- a/wild_lib/src/layout.rs +++ b/wild_lib/src/layout.rs @@ -1831,11 +1831,11 @@ impl<'data> GroupState<'data> { resources: &FinaliseLayoutResources<'_, 'data>, ) -> Result> { let eh_frame_start_address = *memory_offsets.get(part_id::EH_FRAME); - let mut files = self + let mut files: Vec<_> = self .files .into_iter() .map(|file| file.finalise_layout(memory_offsets, resolutions_out, resources)) - .collect::>>()?; + .try_collect()?; let strtab_start_offset = self .common .finalise_layout(memory_offsets, resources.section_layouts); diff --git a/wild_lib/src/output_section_id.rs b/wild_lib/src/output_section_id.rs index 5de9f7b8..ee98f257 100644 --- a/wild_lib/src/output_section_id.rs +++ b/wild_lib/src/output_section_id.rs @@ -700,7 +700,7 @@ impl<'data> OutputSectionsBuilder<'data> { } pub(crate) fn with_base_address(base_address: u64) -> Self { - let section_infos: Vec<_> = SECTION_DEFINITIONS + let section_infos = SECTION_DEFINITIONS .iter() .map(|d| SectionOutputInfo { section_flags: d.section_flags, diff --git a/wild_lib/src/symbol_db.rs b/wild_lib/src/symbol_db.rs index fd4bfd19..4fb51bb0 100644 --- a/wild_lib/src/symbol_db.rs +++ b/wild_lib/src/symbol_db.rs @@ -214,7 +214,7 @@ impl<'data> SymbolDb<'data> { let num_symbols_per_group = groups .iter() .map(|g| g.files.iter().map(|f| f.num_symbols()).sum()) - .collect::>(); + .collect_vec(); let num_symbols = num_symbols_per_group.iter().sum();