Skip to content

Commit ea15f6d

Browse files
committed
librustdoc: lazily format some paths
1 parent 5c5763a commit ea15f6d

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
lines changed

src/librustdoc/html/format.rs

+26-19
Original file line numberDiff line numberDiff line change
@@ -709,19 +709,22 @@ fn resolved_path(
709709
if w.alternate() {
710710
write!(w, "{}{:#}", last.name, last.args.print(cx))?;
711711
} else {
712-
let path = if use_absolute {
713-
if let Ok((_, _, fqp)) = href(did, cx) {
714-
format!(
715-
"{path}::{anchor}",
716-
path = join_with_double_colon(&fqp[..fqp.len() - 1]),
717-
anchor = anchor(did, *fqp.last().unwrap(), cx)
718-
)
712+
let path = fmt::from_fn(|f| {
713+
if use_absolute {
714+
if let Ok((_, _, fqp)) = href(did, cx) {
715+
write!(
716+
f,
717+
"{path}::{anchor}",
718+
path = join_with_double_colon(&fqp[..fqp.len() - 1]),
719+
anchor = anchor(did, *fqp.last().unwrap(), cx)
720+
)
721+
} else {
722+
write!(f, "{}", last.name)
723+
}
719724
} else {
720-
last.name.to_string()
725+
write!(f, "{}", anchor(did, last.name, cx))
721726
}
722-
} else {
723-
anchor(did, last.name, cx).to_string()
724-
};
727+
});
725728
write!(w, "{path}{args}", args = last.args.print(cx))?;
726729
}
727730
Ok(())
@@ -749,16 +752,20 @@ fn primitive_link_fragment(
749752
match m.primitive_locations.get(&prim) {
750753
Some(&def_id) if def_id.is_local() => {
751754
let len = cx.current.len();
752-
let path = if len == 0 {
753-
let cname_sym = ExternalCrate { crate_num: def_id.krate }.name(cx.tcx());
754-
format!("{cname_sym}/")
755-
} else {
756-
"../".repeat(len - 1)
757-
};
755+
let path = fmt::from_fn(|f| {
756+
if len == 0 {
757+
let cname_sym = ExternalCrate { crate_num: def_id.krate }.name(cx.tcx());
758+
write!(f, "{cname_sym}/")?;
759+
} else {
760+
for _ in 0..(len - 1) {
761+
f.write_str("../")?;
762+
}
763+
}
764+
Ok(())
765+
});
758766
write!(
759767
f,
760-
"<a class=\"primitive\" href=\"{}primitive.{}.html{fragment}\">",
761-
path,
768+
"<a class=\"primitive\" href=\"{path}primitive.{}.html{fragment}\">",
762769
prim.as_sym()
763770
)?;
764771
needs_termination = true;

src/librustdoc/html/render/context.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -266,12 +266,12 @@ impl<'tcx> Context<'tcx> {
266266
// preventing an infinite redirection loop in the generated
267267
// documentation.
268268

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

0 commit comments

Comments
 (0)