Skip to content

Commit 990f1da

Browse files
committed
make link_tooltip return impl fmt::Display
1 parent 2c7c97f commit 990f1da

File tree

2 files changed

+30
-27
lines changed

2 files changed

+30
-27
lines changed

src/librustdoc/clean/types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ impl Item {
512512
Some(RenderedLink {
513513
original_text: s.clone(),
514514
new_text: link_text.clone(),
515-
tooltip: link_tooltip(*id, fragment, cx),
515+
tooltip: link_tooltip(*id, fragment, cx).to_string(),
516516
href,
517517
})
518518
} else {

src/librustdoc/html/format.rs

+29-26
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use std::borrow::Cow;
1111
use std::cmp::Ordering;
1212
use std::fmt::{self, Display, Write};
1313
use std::iter::{self, once};
14+
use std::slice;
1415

1516
use itertools::Either;
1617
use rustc_abi::ExternAbi;
@@ -647,33 +648,35 @@ pub(crate) fn href_relative_parts<'fqp>(
647648
}
648649
}
649650

650-
pub(crate) fn link_tooltip(did: DefId, fragment: &Option<UrlFragment>, cx: &Context<'_>) -> String {
651-
let cache = cx.cache();
652-
let Some((fqp, shortty)) = cache.paths.get(&did).or_else(|| cache.external_paths.get(&did))
653-
else {
654-
return String::new();
655-
};
656-
let mut buf = String::new();
657-
let fqp = if *shortty == ItemType::Primitive {
658-
// primitives are documented in a crate, but not actually part of it
659-
&fqp[fqp.len() - 1..]
660-
} else {
661-
fqp
662-
};
663-
if let &Some(UrlFragment::Item(id)) = fragment {
664-
write_str(&mut buf, format_args!("{} ", cx.tcx().def_descr(id)));
665-
for component in fqp {
666-
write_str(&mut buf, format_args!("{component}::"));
667-
}
668-
write_str(&mut buf, format_args!("{}", cx.tcx().item_name(id)));
669-
} else if !fqp.is_empty() {
670-
let mut fqp_it = fqp.iter();
671-
write_str(&mut buf, format_args!("{shortty} {}", fqp_it.next().unwrap()));
672-
for component in fqp_it {
673-
write_str(&mut buf, format_args!("::{component}"));
651+
pub(crate) fn link_tooltip(
652+
did: DefId,
653+
fragment: &Option<UrlFragment>,
654+
cx: &Context<'_>,
655+
) -> impl fmt::Display {
656+
fmt::from_fn(move |f| {
657+
let cache = cx.cache();
658+
let Some((fqp, shortty)) = cache.paths.get(&did).or_else(|| cache.external_paths.get(&did))
659+
else {
660+
return Ok(());
661+
};
662+
let fqp = if *shortty == ItemType::Primitive {
663+
// primitives are documented in a crate, but not actually part of it
664+
slice::from_ref(fqp.last().unwrap())
665+
} else {
666+
fqp
667+
};
668+
if let &Some(UrlFragment::Item(id)) = fragment {
669+
write!(f, "{} ", cx.tcx().def_descr(id))?;
670+
for component in fqp {
671+
write!(f, "{component}::")?;
672+
}
673+
write!(f, "{}", cx.tcx().item_name(id))?;
674+
} else if !fqp.is_empty() {
675+
write!(f, "{shortty} ")?;
676+
fqp.iter().joined("::", f)?;
674677
}
675-
}
676-
buf
678+
Ok(())
679+
})
677680
}
678681

679682
/// Used to render a [`clean::Path`].

0 commit comments

Comments
 (0)