1
Fork 0

librustdoc: make item_path formatting lazy

This commit is contained in:
Yotam Ofek 2025-02-10 17:32:39 +00:00
parent cf097d5d7f
commit bc061858d2
2 changed files with 17 additions and 12 deletions

View file

@ -1,8 +1,9 @@
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::fmt::{self, Write as _};
use std::io;
use std::path::{Path, PathBuf};
use std::sync::mpsc::{Receiver, channel};
use std::{fmt, io};
use rinja::Template;
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
@ -270,7 +271,7 @@ impl<'tcx> Context<'tcx> {
path.push_str(name.as_str());
path.push('/');
}
path.push_str(&item_path(ty, names.last().unwrap().as_str()));
let _ = write!(path, "{}", item_path(ty, names.last().unwrap().as_str()));
match self.shared.redirections {
Some(ref redirections) => {
let mut current_path = String::new();
@ -278,8 +279,12 @@ impl<'tcx> Context<'tcx> {
current_path.push_str(name.as_str());
current_path.push('/');
}
current_path.push_str(&item_path(ty, names.last().unwrap().as_str()));
redirections.borrow_mut().insert(current_path, path);
let _ = write!(
current_path,
"{}",
item_path(ty, names.last().unwrap().as_str())
);
redirections.borrow_mut().insert(current_path, path.to_string());
}
None => {
return layout::redirect(&format!(
@ -854,9 +859,9 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
if !buf.is_empty() {
let name = item.name.as_ref().unwrap();
let item_type = item.type_();
let file_name = &item_path(item_type, name.as_str());
let file_name = item_path(item_type, name.as_str()).to_string();
self.shared.ensure_dir(&self.dst)?;
let joint_dst = self.dst.join(file_name);
let joint_dst = self.dst.join(&file_name);
self.shared.fs.write(joint_dst, buf)?;
if !self.info.render_redirect_pages {
@ -873,7 +878,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
format!("{crate_name}/{file_name}"),
);
} else {
let v = layout::redirect(file_name);
let v = layout::redirect(&file_name);
let redir_dst = self.dst.join(redir_name);
self.shared.fs.write(redir_dst, v)?;
}

View file

@ -2115,11 +2115,11 @@ pub(super) fn full_path(cx: &Context<'_>, item: &clean::Item) -> String {
s
}
pub(super) fn item_path(ty: ItemType, name: &str) -> String {
match ty {
ItemType::Module => format!("{}index.html", ensure_trailing_slash(name)),
_ => format!("{ty}.{name}.html"),
}
pub(super) fn item_path(ty: ItemType, name: &str) -> impl Display + '_ {
fmt::from_fn(move |f| match ty {
ItemType::Module => write!(f, "{}index.html", ensure_trailing_slash(name)),
_ => write!(f, "{ty}.{name}.html"),
})
}
fn bounds<'a, 'tcx>(