diff options
| author | Yotam Ofek <yotam.ofek@gmail.com> | 2025-02-10 17:32:39 +0000 |
|---|---|---|
| committer | Yotam Ofek <yotam.ofek@gmail.com> | 2025-02-14 14:50:04 +0000 |
| commit | bc061858d23cd970395023626ea9e5c30675bd7a (patch) | |
| tree | 5236f7b684206f5f9fbb75931700e4a40fb317c2 /src/librustdoc | |
| parent | cf097d5d7f9354080aa17ce45dd4da3d4a8c34c9 (diff) | |
| download | rust-bc061858d23cd970395023626ea9e5c30675bd7a.tar.gz rust-bc061858d23cd970395023626ea9e5c30675bd7a.zip | |
librustdoc: make `item_path` formatting lazy
Diffstat (limited to 'src/librustdoc')
| -rw-r--r-- | src/librustdoc/html/render/context.rs | 19 | ||||
| -rw-r--r-- | src/librustdoc/html/render/print_item.rs | 10 |
2 files changed, 17 insertions, 12 deletions
diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs index b774e60c62d..dab9ba79761 100644 --- a/src/librustdoc/html/render/context.rs +++ b/src/librustdoc/html/render/context.rs @@ -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)?; } diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index 924d8bde96c..d2d7415261b 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -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>( |
