Skip to content

Commit

Permalink
Use itertools (collect_vec, try_collect)
Browse files Browse the repository at this point in the history
  • Loading branch information
marxin committed Sep 12, 2024
1 parent 1a8cae3 commit 40ec1c7
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 26 deletions.
6 changes: 2 additions & 4 deletions linker-diff/src/header_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -223,10 +224,7 @@ pub(crate) fn diff_fields(
table_name: &str,
diff_mode: DiffMode,
) -> Vec<Diff> {
let field_values = objects
.iter()
.map(get_fields_fn)
.collect::<Vec<Result<FieldValues>>>();
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![];
}
Expand Down
20 changes: 9 additions & 11 deletions linker-diff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<u8>> {
let mut bytes = std::fs::read(filename)
Expand All @@ -339,27 +339,27 @@ impl Report {
})?;
Ok(bytes)
})
.collect::<Result<Vec<Vec<u8>>>>()?;
.try_collect()?;

let elf_files = file_bytes
let elf_files: Vec<_> = file_bytes
.iter()
.map(|bytes| -> Result<ElfFile64> { Ok(ElfFile64::parse(bytes.as_slice())?) })
.collect::<Result<Vec<_>>>()?;
.try_collect()?;

let layouts = config
let layouts: Vec<_> = config
.filenames()
.map(|p| LayoutAndFiles::from_base_path(p))
.collect::<Result<Vec<_>>>()?;
.try_collect()?;

let objects = elf_files
let objects: Vec<_> = elf_files
.iter()
.zip(display_names)
.zip(config.filenames())
.zip(&layouts)
.map(|(((elf_file, name), path), layout)| -> Result<Object> {
Object::new(elf_file, name, path.clone(), layout.as_ref())
})
.collect::<Result<Vec<_>>>()?;
.try_collect()?;

let mut report = Report {
names: objects.iter().map(|o| o.name.clone()).collect(),
Expand Down Expand Up @@ -593,9 +593,7 @@ fn short_file_display_names(config: &Config) -> Result<Vec<String>> {
}
names = names
.iter()
.map(|name| {
String::from_utf8_lossy(&name.bytes().skip(n).collect::<Vec<u8>>()).into_owned()
})
.map(|name| String::from_utf8_lossy(&name.bytes().skip(n).collect_vec()).into_owned())
.collect_vec();
Ok(names)
}
Expand Down
12 changes: 6 additions & 6 deletions wild/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Result<Vec<_>>>()?;
.try_collect()?;

let link_output = linker.link(self.name(), &inputs, config)?;
let shared_objects = inputs
Expand Down Expand Up @@ -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!(
Expand Down Expand Up @@ -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| {
Expand All @@ -1381,7 +1381,7 @@ fn integration_test(
}
result
})
.collect::<Result<Vec<_>>>()?;
.try_collect()?;

let start = Instant::now();
diff_shared_objects(&config, &programs)?;
Expand Down
3 changes: 2 additions & 1 deletion wild_lib/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -745,7 +746,7 @@ mod tests {
InputSpec::File(_) => None,
InputSpec::Lib(lib_name) => Some(lib_name.as_ref()),
})
.collect::<Vec<&str>>(),
.collect_vec(),
&["gcc_s", "util", "rt", "pthread", "m", "dl", "c"]
);
assert_contains(&args.lib_search_path, "/lib");
Expand Down
4 changes: 2 additions & 2 deletions wild_lib/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1831,11 +1831,11 @@ impl<'data> GroupState<'data> {
resources: &FinaliseLayoutResources<'_, 'data>,
) -> Result<GroupLayout<'data>> {
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::<Result<Vec<_>>>()?;
.try_collect()?;
let strtab_start_offset = self
.common
.finalise_layout(memory_offsets, resources.section_layouts);
Expand Down
2 changes: 1 addition & 1 deletion wild_lib/src/output_section_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion wild_lib/src/symbol_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<usize>>();
.collect_vec();

let num_symbols = num_symbols_per_group.iter().sum();

Expand Down

0 comments on commit 40ec1c7

Please sign in to comment.