Return impl Display instead of String in fragment

This commit is contained in:
Guillaume Gomez 2025-11-20 20:49:31 +01:00
parent a5f972ec47
commit 8f76773041
2 changed files with 23 additions and 20 deletions

View file

@ -1,3 +1,4 @@
use std::fmt::Write;
use std::hash::Hash;
use std::path::PathBuf;
use std::sync::{Arc, OnceLock as OnceCell};
@ -524,7 +525,8 @@ impl Item {
debug!(?url);
match fragment {
Some(UrlFragment::Item(def_id)) => {
url.push_str(&crate::html::format::fragment(*def_id, cx.tcx()))
write!(url, "{}", crate::html::format::fragment(*def_id, cx.tcx()))
.unwrap();
}
Some(UrlFragment::UserWritten(raw)) => {
url.push('#');

View file

@ -833,26 +833,27 @@ fn print_higher_ranked_params_with_space(
})
}
pub(crate) fn fragment(did: DefId, tcx: TyCtxt<'_>) -> String {
let def_kind = tcx.def_kind(did);
match def_kind {
DefKind::AssocTy | DefKind::AssocFn | DefKind::AssocConst | DefKind::Variant => {
let item_type = ItemType::from_def_id(did, tcx);
format!("#{}.{}", item_type.as_str(), tcx.item_name(did))
pub(crate) fn fragment(did: DefId, tcx: TyCtxt<'_>) -> impl Display {
fmt::from_fn(move |f| {
let def_kind = tcx.def_kind(did);
match def_kind {
DefKind::AssocTy | DefKind::AssocFn | DefKind::AssocConst | DefKind::Variant => {
let item_type = ItemType::from_def_id(did, tcx);
write!(f, "#{}.{}", item_type.as_str(), tcx.item_name(did))
}
DefKind::Field => {
let parent_def_id = tcx.parent(did);
f.write_char('#')?;
if tcx.def_kind(parent_def_id) == DefKind::Variant {
write!(f, "variant.{}.field", tcx.item_name(parent_def_id).as_str())?;
} else {
f.write_str("structfield")?;
};
write!(f, ".{}", tcx.item_name(did))
}
_ => Ok(()),
}
DefKind::Field => {
let parent_def_id = tcx.parent(did);
let s;
let kind = if tcx.def_kind(parent_def_id) == DefKind::Variant {
s = format!("variant.{}.field", tcx.item_name(parent_def_id).as_str());
&s
} else {
"structfield"
};
format!("#{kind}.{}", tcx.item_name(did))
}
_ => String::new(),
}
})
}
pub(crate) fn print_anchor(did: DefId, text: Symbol, cx: &Context<'_>) -> impl Display {