Skip to content

Commit 2717752

Browse files
ref(debug-files): Remove unnecessary collect (#2705)
Noticed that this function currently returns a `Vec`, when an `impl Iterator` would likely be preferable
1 parent aafc2cf commit 2717752

2 files changed

Lines changed: 35 additions & 30 deletions

File tree

src/commands/debug_files/find.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ fn find_ids_for_dsym(
224224
if dirent.path().extension() != Some(OsStr::new("class"));
225225
if let Ok(dif) = DifFile::open_path(dirent.path(), Some(DifType::Dsym));
226226
then {
227-
return Some(extract_remaining_ids(&dif.ids(), remaining, DifType::Dsym))
227+
return Some(extract_remaining_ids(dif.ids(), remaining, DifType::Dsym))
228228
}
229229
}
230230
None
@@ -237,7 +237,7 @@ fn find_ids_for_elf(
237237
if_chain! {
238238
if let Ok(dif) = DifFile::open_path(dirent.path(), Some(DifType::Elf));
239239
then {
240-
return Some(extract_remaining_ids(&dif.ids(), remaining, DifType::Elf))
240+
return Some(extract_remaining_ids(dif.ids(), remaining, DifType::Elf))
241241
}
242242
}
243243
None
@@ -252,7 +252,7 @@ fn find_ids_for_pe(
252252
dirent.path().extension() == Some(OsStr::new("dll"));
253253
if let Ok(dif) = DifFile::open_path(dirent.path(), Some(DifType::Pe));
254254
then {
255-
return Some(extract_remaining_ids(&dif.ids(), remaining, DifType::Pe))
255+
return Some(extract_remaining_ids(dif.ids(), remaining, DifType::Pe))
256256
}
257257
}
258258
None
@@ -266,7 +266,7 @@ fn find_ids_for_pdb(
266266
if dirent.path().extension() == Some(OsStr::new("pdb"));
267267
if let Ok(dif) = DifFile::open_path(dirent.path(), Some(DifType::Pdb));
268268
then {
269-
return Some(extract_remaining_ids(&dif.ids(), remaining, DifType::Pdb))
269+
return Some(extract_remaining_ids(dif.ids(), remaining, DifType::Pdb))
270270
}
271271
}
272272
None
@@ -280,7 +280,7 @@ fn find_ids_for_portablepdb(
280280
if dirent.path().extension() == Some(OsStr::new("pdb"));
281281
if let Ok(dif) = DifFile::open_path(dirent.path(), Some(DifType::PortablePdb));
282282
then {
283-
return Some(extract_remaining_ids(&dif.ids(), remaining, DifType::PortablePdb))
283+
return Some(extract_remaining_ids(dif.ids(), remaining, DifType::PortablePdb))
284284
}
285285
}
286286
None
@@ -294,7 +294,7 @@ fn find_ids_for_sourcebundle(
294294
if dirent.path().extension() == Some(OsStr::new("zip"));
295295
if let Ok(dif) = DifFile::open_path(dirent.path(), Some(DifType::SourceBundle));
296296
then {
297-
return Some(extract_remaining_ids(&dif.ids(), remaining, DifType::SourceBundle))
297+
return Some(extract_remaining_ids(dif.ids(), remaining, DifType::SourceBundle))
298298
}
299299
}
300300
None
@@ -308,25 +308,27 @@ fn find_ids_for_breakpad(
308308
if dirent.path().extension() == Some(OsStr::new("sym"));
309309
if let Ok(dif) = DifFile::open_path(dirent.path(), Some(DifType::Breakpad));
310310
then {
311-
return Some(extract_remaining_ids(&dif.ids(), remaining, DifType::Breakpad))
311+
return Some(extract_remaining_ids(dif.ids(), remaining, DifType::Breakpad))
312312
}
313313
}
314314
None
315315
}
316316

317-
fn extract_remaining_ids(
318-
ids: &[DebugId],
317+
fn extract_remaining_ids<I>(
318+
ids: I,
319319
remaining: &HashSet<DebugId>,
320320
t: DifType,
321-
) -> Vec<(DebugId, DifType)> {
322-
ids.iter()
323-
.filter_map(|id| {
324-
if remaining.contains(id) {
325-
return Some((id.to_owned(), t));
326-
}
327-
None
328-
})
329-
.collect()
321+
) -> Vec<(DebugId, DifType)>
322+
where
323+
I: Iterator<Item = DebugId>,
324+
{
325+
ids.filter_map(|id| {
326+
if remaining.contains(&id) {
327+
return Some((id.to_owned(), t));
328+
}
329+
None
330+
})
331+
.collect()
330332
}
331333

332334
pub fn execute(matches: &ArgMatches) -> Result<()> {

src/utils/dif.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use std::fmt;
21
use std::path::Path;
32
use std::str;
3+
use std::{fmt, iter};
44

55
use anyhow::{bail, Context as _, Error, Result};
66
use proguard::ProguardMapping;
@@ -327,16 +327,19 @@ impl<'a> DifFile<'a> {
327327
}
328328
}
329329

330-
pub fn ids(&self) -> Vec<DebugId> {
331-
match self {
332-
DifFile::Archive(archive) => archive
333-
.get()
334-
.objects()
335-
.filter_map(Result::ok)
336-
.map(|object| object.debug_id())
337-
.collect(),
338-
DifFile::Proguard(pg) => vec![pg.get().uuid().into()],
339-
}
330+
pub fn ids(&self) -> impl Iterator<Item = DebugId> + '_ {
331+
let rv: Box<dyn Iterator<Item = _>> = match self {
332+
DifFile::Archive(archive) => Box::new(
333+
archive
334+
.get()
335+
.objects()
336+
.filter_map(Result::ok)
337+
.map(|object| object.debug_id()),
338+
),
339+
DifFile::Proguard(pg) => Box::new(iter::once(pg.get().uuid().into())),
340+
};
341+
342+
rv
340343
}
341344

342345
pub fn features(&self) -> ObjectDifFeatures {
@@ -407,7 +410,7 @@ impl<'a> DifFile<'a> {
407410
}
408411

409412
fn has_ids(&self) -> bool {
410-
self.ids().iter().any(|id| !id.is_nil())
413+
self.ids().any(|id| !id.is_nil())
411414
}
412415
}
413416

0 commit comments

Comments
 (0)