diff options
| author | Yotam Ofek <yotam.ofek@gmail.com> | 2025-02-10 17:38:16 +0000 |
|---|---|---|
| committer | Yotam Ofek <yotam.ofek@gmail.com> | 2025-02-14 14:50:05 +0000 |
| commit | ea15f6d792069df85f4f371130a20c8d95f02074 (patch) | |
| tree | c24e079479f901467d6f48331ff365e15cbc957e /src | |
| parent | 5c5763a6cfdb01016c8a0853f859d40bf50ff49c (diff) | |
| download | rust-ea15f6d792069df85f4f371130a20c8d95f02074.tar.gz rust-ea15f6d792069df85f4f371130a20c8d95f02074.zip | |
librustdoc: lazily format some paths
Diffstat (limited to 'src')
| -rw-r--r-- | src/librustdoc/html/format.rs | 45 | ||||
| -rw-r--r-- | src/librustdoc/html/render/context.rs | 12 |
2 files changed, 32 insertions, 25 deletions
diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index d99b40c0a38..91b4b3ba1eb 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -709,19 +709,22 @@ fn resolved_path( if w.alternate() { write!(w, "{}{:#}", last.name, last.args.print(cx))?; } else { - let path = if use_absolute { - if let Ok((_, _, fqp)) = href(did, cx) { - format!( - "{path}::{anchor}", - path = join_with_double_colon(&fqp[..fqp.len() - 1]), - anchor = anchor(did, *fqp.last().unwrap(), cx) - ) + let path = fmt::from_fn(|f| { + if use_absolute { + if let Ok((_, _, fqp)) = href(did, cx) { + write!( + f, + "{path}::{anchor}", + path = join_with_double_colon(&fqp[..fqp.len() - 1]), + anchor = anchor(did, *fqp.last().unwrap(), cx) + ) + } else { + write!(f, "{}", last.name) + } } else { - last.name.to_string() + write!(f, "{}", anchor(did, last.name, cx)) } - } else { - anchor(did, last.name, cx).to_string() - }; + }); write!(w, "{path}{args}", args = last.args.print(cx))?; } Ok(()) @@ -749,16 +752,20 @@ fn primitive_link_fragment( match m.primitive_locations.get(&prim) { Some(&def_id) if def_id.is_local() => { let len = cx.current.len(); - let path = if len == 0 { - let cname_sym = ExternalCrate { crate_num: def_id.krate }.name(cx.tcx()); - format!("{cname_sym}/") - } else { - "../".repeat(len - 1) - }; + let path = fmt::from_fn(|f| { + if len == 0 { + let cname_sym = ExternalCrate { crate_num: def_id.krate }.name(cx.tcx()); + write!(f, "{cname_sym}/")?; + } else { + for _ in 0..(len - 1) { + f.write_str("../")?; + } + } + Ok(()) + }); write!( f, - "<a class=\"primitive\" href=\"{}primitive.{}.html{fragment}\">", - path, + "<a class=\"primitive\" href=\"{path}primitive.{}.html{fragment}\">", prim.as_sym() )?; needs_termination = true; diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs index dab9ba79761..146bdd34069 100644 --- a/src/librustdoc/html/render/context.rs +++ b/src/librustdoc/html/render/context.rs @@ -266,12 +266,12 @@ impl<'tcx> Context<'tcx> { // preventing an infinite redirection loop in the generated // documentation. - let mut path = String::new(); - for name in &names[..names.len() - 1] { - path.push_str(name.as_str()); - path.push('/'); - } - let _ = write!(path, "{}", item_path(ty, names.last().unwrap().as_str())); + let path = fmt::from_fn(|f| { + for name in &names[..names.len() - 1] { + write!(f, "{name}/")?; + } + write!(f, "{}", item_path(ty, names.last().unwrap().as_str())) + }); match self.shared.redirections { Some(ref redirections) => { let mut current_path = String::new(); |
