Skip to content

Commit c083f86

Browse files
emilazyByron
authored andcommitted
feat: add mailmap::Snapshot::iter()
Allow iterating through entries without allocation.
1 parent ae2b9ce commit c083f86

File tree

1 file changed

+26
-18
lines changed
  • gix-mailmap/src/snapshot

1 file changed

+26
-18
lines changed

gix-mailmap/src/snapshot/mod.rs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,32 +51,40 @@ impl Snapshot {
5151
self
5252
}
5353

54-
/// Transform our acceleration structure into a list of entries.
54+
/// Transform our acceleration structure into an iterator of entries.
5555
///
5656
/// Note that the order is different from how they were obtained initially, and are explicitly ordered by
5757
/// (`old_email`, `old_name`).
58-
pub fn entries(&self) -> Vec<crate::Entry<'_>> {
59-
let mut out = Vec::with_capacity(self.entries_by_old_email.len());
60-
for entry in &self.entries_by_old_email {
61-
if entry.new_email.is_some() || entry.new_name.is_some() {
62-
out.push(crate::Entry {
58+
pub fn iter(&self) -> impl Iterator<Item = crate::Entry<'_>> {
59+
self.entries_by_old_email.iter().flat_map(|entry| {
60+
let initial = if entry.new_email.is_some() || entry.new_name.is_some() {
61+
Some(crate::Entry {
6362
new_name: entry.new_name.as_ref().map(|b| b.as_bstr()),
6463
new_email: entry.new_email.as_ref().map(|b| b.as_bstr()),
6564
old_name: None,
6665
old_email: entry.old_email.as_bstr(),
67-
});
68-
}
66+
})
67+
} else {
68+
None
69+
};
6970

70-
for name_entry in &entry.entries_by_old_name {
71-
out.push(crate::Entry {
72-
new_name: name_entry.new_name.as_ref().map(|b| b.as_bstr()),
73-
new_email: name_entry.new_email.as_ref().map(|b| b.as_bstr()),
74-
old_name: name_entry.old_name.as_bstr().into(),
75-
old_email: entry.old_email.as_bstr(),
76-
});
77-
}
78-
}
79-
out
71+
let rest = entry.entries_by_old_name.iter().map(|name_entry| crate::Entry {
72+
new_name: name_entry.new_name.as_ref().map(|b| b.as_bstr()),
73+
new_email: name_entry.new_email.as_ref().map(|b| b.as_bstr()),
74+
old_name: name_entry.old_name.as_bstr().into(),
75+
old_email: entry.old_email.as_bstr(),
76+
});
77+
78+
initial.into_iter().chain(rest)
79+
})
80+
}
81+
82+
/// Transform our acceleration structure into a list of entries.
83+
///
84+
/// Note that the order is different from how they were obtained initially, and are explicitly ordered by
85+
/// (`old_email`, `old_name`).
86+
pub fn entries(&self) -> Vec<crate::Entry<'_>> {
87+
self.iter().collect()
8088
}
8189

8290
/// Try to resolve `signature` by its contained email and name and provide resolved/mapped names as reference.

0 commit comments

Comments
 (0)