Skip to content

Commit 93d83c4

Browse files
committed
librustdoc: make item_path formatting lazy
1 parent 0b4fb0f commit 93d83c4

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

src/librustdoc/html/render/context.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::cell::RefCell;
22
use std::collections::BTreeMap;
3+
use std::fmt::Write as _;
34
use std::io;
45
use std::path::{Path, PathBuf};
56
use std::sync::mpsc::{Receiver, channel};
@@ -267,16 +268,20 @@ impl<'tcx> Context<'tcx> {
267268
path.push_str(name.as_str());
268269
path.push('/');
269270
}
270-
path.push_str(&item_path(ty, names.last().unwrap().as_str()));
271+
let _ = write!(path, "{}", item_path(ty, names.last().unwrap().as_str()));
271272
match self.shared.redirections {
272273
Some(ref redirections) => {
273274
let mut current_path = String::new();
274275
for name in &self.current {
275276
current_path.push_str(name.as_str());
276277
current_path.push('/');
277278
}
278-
current_path.push_str(&item_path(ty, names.last().unwrap().as_str()));
279-
redirections.borrow_mut().insert(current_path, path);
279+
let _ = write!(
280+
current_path,
281+
"{}",
282+
item_path(ty, names.last().unwrap().as_str())
283+
);
284+
redirections.borrow_mut().insert(current_path, path.to_string());
280285
}
281286
None => {
282287
return layout::redirect(&format!(
@@ -851,9 +856,9 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
851856
if !buf.is_empty() {
852857
let name = item.name.as_ref().unwrap();
853858
let item_type = item.type_();
854-
let file_name = &item_path(item_type, name.as_str());
859+
let file_name = item_path(item_type, name.as_str()).to_string();
855860
self.shared.ensure_dir(&self.dst)?;
856-
let joint_dst = self.dst.join(file_name);
861+
let joint_dst = self.dst.join(&file_name);
857862
self.shared.fs.write(joint_dst, buf)?;
858863

859864
if !self.info.render_redirect_pages {
@@ -870,7 +875,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
870875
format!("{crate_name}/{file_name}"),
871876
);
872877
} else {
873-
let v = layout::redirect(file_name);
878+
let v = layout::redirect(&file_name);
874879
let redir_dst = self.dst.join(redir_name);
875880
self.shared.fs.write(redir_dst, v)?;
876881
}

src/librustdoc/html/render/print_item.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2042,11 +2042,11 @@ pub(super) fn full_path(cx: &Context<'_>, item: &clean::Item) -> String {
20422042
s
20432043
}
20442044

2045-
pub(super) fn item_path(ty: ItemType, name: &str) -> String {
2046-
match ty {
2047-
ItemType::Module => format!("{}index.html", ensure_trailing_slash(name)),
2048-
_ => format!("{ty}.{name}.html"),
2049-
}
2045+
pub(super) fn item_path(ty: ItemType, name: &str) -> impl Display + '_ {
2046+
fmt::from_fn(move |f| match ty {
2047+
ItemType::Module => write!(f, "{}index.html", ensure_trailing_slash(name)),
2048+
_ => write!(f, "{ty}.{name}.html"),
2049+
})
20502050
}
20512051

20522052
fn bounds<'a, 'tcx>(

0 commit comments

Comments
 (0)