Skip to content

Commit a82692d

Browse files
committed
Use Iterator::collect instead of calling Vec::push in a loop
1 parent b0fd642 commit a82692d

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

src/librustdoc/html/render/cache.rs

+26-23
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ crate enum ExternalLocation {
2626
/// Builds the search index from the collected metadata
2727
crate fn build_index<'tcx>(krate: &clean::Crate, cache: &mut Cache, tcx: TyCtxt<'tcx>) -> String {
2828
let mut defid_to_pathid = FxHashMap::default();
29-
let mut crate_items = Vec::with_capacity(cache.search_index.len());
3029
let mut crate_paths = vec![];
3130

3231
// Attach all orphan items to the type's definition if the type
@@ -77,34 +76,38 @@ crate fn build_index<'tcx>(krate: &clean::Crate, cache: &mut Cache, tcx: TyCtxt<
7776

7877
// Reduce `DefId` in paths into smaller sequential numbers,
7978
// and prune the paths that do not appear in the index.
80-
let mut lastpath = String::new();
79+
let mut lastpath = "";
8180
let mut lastpathid = 0usize;
8281

83-
for item in search_index {
84-
item.parent_idx = item.parent.and_then(|defid| match defid_to_pathid.entry(defid) {
85-
Entry::Occupied(entry) => Some(*entry.get()),
86-
Entry::Vacant(entry) => {
87-
let pathid = lastpathid;
88-
entry.insert(pathid);
89-
lastpathid += 1;
82+
let crate_items: Vec<&IndexItem> = search_index
83+
.iter_mut()
84+
.map(|item| {
85+
item.parent_idx = item.parent.and_then(|defid| match defid_to_pathid.entry(defid) {
86+
Entry::Occupied(entry) => Some(*entry.get()),
87+
Entry::Vacant(entry) => {
88+
let pathid = lastpathid;
89+
entry.insert(pathid);
90+
lastpathid += 1;
9091

91-
if let Some(&(ref fqp, short)) = paths.get(&defid) {
92-
crate_paths.push((short, fqp.last().unwrap().clone()));
93-
Some(pathid)
94-
} else {
95-
None
92+
if let Some(&(ref fqp, short)) = paths.get(&defid) {
93+
crate_paths.push((short, fqp.last().unwrap().clone()));
94+
Some(pathid)
95+
} else {
96+
None
97+
}
9698
}
99+
});
100+
101+
// Omit the parent path if it is same to that of the prior item.
102+
if lastpath == &item.path {
103+
item.path.clear();
104+
} else {
105+
lastpath = &item.path;
97106
}
98-
});
99107

100-
// Omit the parent path if it is same to that of the prior item.
101-
if lastpath == item.path {
102-
item.path.clear();
103-
} else {
104-
lastpath = item.path.clone();
105-
}
106-
crate_items.push(&*item);
107-
}
108+
&*item
109+
})
110+
.collect();
108111

109112
struct CrateData<'a> {
110113
doc: String,

0 commit comments

Comments
 (0)