Skip to content

Commit 7e303f9

Browse files
committed
librustdoc: lazily format some paths
1 parent 9c9aec2 commit 7e303f9

File tree

2 files changed

+33
-26
lines changed

2 files changed

+33
-26
lines changed

src/librustdoc/html/format.rs

+26-19
Original file line numberDiff line numberDiff line change
@@ -814,19 +814,22 @@ fn resolved_path(
814814
if w.alternate() {
815815
write!(w, "{}{:#}", last.name, last.args.print(cx))?;
816816
} else {
817-
let path = if use_absolute {
818-
if let Ok((_, _, fqp)) = href(did, cx) {
819-
format!(
820-
"{path}::{anchor}",
821-
path = join_with_double_colon(&fqp[..fqp.len() - 1]),
822-
anchor = anchor(did, *fqp.last().unwrap(), cx)
823-
)
817+
let path = fmt::from_fn(|f| {
818+
if use_absolute {
819+
if let Ok((_, _, fqp)) = href(did, cx) {
820+
write!(
821+
f,
822+
"{path}::{anchor}",
823+
path = join_with_double_colon(&fqp[..fqp.len() - 1]),
824+
anchor = anchor(did, *fqp.last().unwrap(), cx)
825+
)
826+
} else {
827+
write!(f, "{}", last.name)
828+
}
824829
} else {
825-
last.name.to_string()
830+
write!(f, "{}", anchor(did, last.name, cx))
826831
}
827-
} else {
828-
anchor(did, last.name, cx).to_string()
829-
};
832+
});
830833
write!(w, "{path}{args}", args = last.args.print(cx))?;
831834
}
832835
Ok(())
@@ -854,16 +857,20 @@ fn primitive_link_fragment(
854857
match m.primitive_locations.get(&prim) {
855858
Some(&def_id) if def_id.is_local() => {
856859
let len = cx.current.len();
857-
let path = if len == 0 {
858-
let cname_sym = ExternalCrate { crate_num: def_id.krate }.name(cx.tcx());
859-
format!("{cname_sym}/")
860-
} else {
861-
"../".repeat(len - 1)
862-
};
860+
let path = fmt::from_fn(|f| {
861+
if len == 0 {
862+
let cname_sym = ExternalCrate { crate_num: def_id.krate }.name(cx.tcx());
863+
write!(f, "{cname_sym}/")?;
864+
} else {
865+
for _ in 0..(len - 1) {
866+
f.write_str("../")?;
867+
}
868+
}
869+
Ok(())
870+
});
863871
write!(
864872
f,
865-
"<a class=\"primitive\" href=\"{}primitive.{}.html{fragment}\">",
866-
path,
873+
"<a class=\"primitive\" href=\"{path}primitive.{}.html{fragment}\">",
867874
prim.as_sym()
868875
)?;
869876
needs_termination = true;

src/librustdoc/html/render/context.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::cell::RefCell;
22
use std::collections::BTreeMap;
3-
use std::fmt::Write as _;
3+
use std::fmt::{self, Write as _};
44
use std::io;
55
use std::path::{Path, PathBuf};
66
use std::sync::mpsc::{Receiver, channel};
@@ -263,12 +263,12 @@ impl<'tcx> Context<'tcx> {
263263
// preventing an infinite redirection loop in the generated
264264
// documentation.
265265

266-
let mut path = String::new();
267-
for name in &names[..names.len() - 1] {
268-
path.push_str(name.as_str());
269-
path.push('/');
270-
}
271-
let _ = write!(path, "{}", item_path(ty, names.last().unwrap().as_str()));
266+
let path = fmt::from_fn(|f| {
267+
for name in &names[..names.len() - 1] {
268+
write!(f, "{name}/")?;
269+
}
270+
write!(f, "{}", item_path(ty, names.last().unwrap().as_str()))
271+
});
272272
match self.shared.redirections {
273273
Some(ref redirections) => {
274274
let mut current_path = String::new();

0 commit comments

Comments
 (0)