Skip to content

Commit bc06185

Browse files
committed
librustdoc: make item_path formatting lazy
1 parent cf097d5 commit bc06185

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

src/librustdoc/html/render/context.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use std::cell::RefCell;
22
use std::collections::BTreeMap;
3+
use std::fmt::{self, Write as _};
4+
use std::io;
35
use std::path::{Path, PathBuf};
46
use std::sync::mpsc::{Receiver, channel};
5-
use std::{fmt, io};
67

78
use rinja::Template;
89
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
@@ -270,16 +271,20 @@ impl<'tcx> Context<'tcx> {
270271
path.push_str(name.as_str());
271272
path.push('/');
272273
}
273-
path.push_str(&item_path(ty, names.last().unwrap().as_str()));
274+
let _ = write!(path, "{}", item_path(ty, names.last().unwrap().as_str()));
274275
match self.shared.redirections {
275276
Some(ref redirections) => {
276277
let mut current_path = String::new();
277278
for name in &self.current {
278279
current_path.push_str(name.as_str());
279280
current_path.push('/');
280281
}
281-
current_path.push_str(&item_path(ty, names.last().unwrap().as_str()));
282-
redirections.borrow_mut().insert(current_path, path);
282+
let _ = write!(
283+
current_path,
284+
"{}",
285+
item_path(ty, names.last().unwrap().as_str())
286+
);
287+
redirections.borrow_mut().insert(current_path, path.to_string());
283288
}
284289
None => {
285290
return layout::redirect(&format!(
@@ -854,9 +859,9 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
854859
if !buf.is_empty() {
855860
let name = item.name.as_ref().unwrap();
856861
let item_type = item.type_();
857-
let file_name = &item_path(item_type, name.as_str());
862+
let file_name = item_path(item_type, name.as_str()).to_string();
858863
self.shared.ensure_dir(&self.dst)?;
859-
let joint_dst = self.dst.join(file_name);
864+
let joint_dst = self.dst.join(&file_name);
860865
self.shared.fs.write(joint_dst, buf)?;
861866

862867
if !self.info.render_redirect_pages {
@@ -873,7 +878,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
873878
format!("{crate_name}/{file_name}"),
874879
);
875880
} else {
876-
let v = layout::redirect(file_name);
881+
let v = layout::redirect(&file_name);
877882
let redir_dst = self.dst.join(redir_name);
878883
self.shared.fs.write(redir_dst, v)?;
879884
}

src/librustdoc/html/render/print_item.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -2115,11 +2115,11 @@ pub(super) fn full_path(cx: &Context<'_>, item: &clean::Item) -> String {
21152115
s
21162116
}
21172117

2118-
pub(super) fn item_path(ty: ItemType, name: &str) -> String {
2119-
match ty {
2120-
ItemType::Module => format!("{}index.html", ensure_trailing_slash(name)),
2121-
_ => format!("{ty}.{name}.html"),
2122-
}
2118+
pub(super) fn item_path(ty: ItemType, name: &str) -> impl Display + '_ {
2119+
fmt::from_fn(move |f| match ty {
2120+
ItemType::Module => write!(f, "{}index.html", ensure_trailing_slash(name)),
2121+
_ => write!(f, "{ty}.{name}.html"),
2122+
})
21232123
}
21242124

21252125
fn bounds<'a, 'tcx>(

0 commit comments

Comments
 (0)