Skip to content

Split traits by crate-local and crate-non-local #130418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1197,11 +1197,33 @@ pub(crate) fn render_all_impls(
blanket_impl: &[&Impl],
) {
let mut impls = Buffer::html();
render_impls(cx, &mut impls, concrete, containing_item, true);

let (concrete_local, concrete_nonlocal): (Vec<&Impl>, Vec<&Impl>) = concrete
.into_iter()
.filter(|elem| elem.inner_impl().trait_.is_some())
.partition(|elem| elem.inner_impl().trait_.as_ref().unwrap().def_id().is_local());

render_impls(cx, &mut impls, &concrete_local, containing_item, true);

let impls = impls.into_inner();
if !impls.is_empty() {
write_impl_section_heading(&mut w, "Trait Implementations", "trait-implementations");
write!(w, "<div id=\"trait-implementations-list\">{impls}</div>").unwrap();
write_impl_section_heading(
&mut w,
"Crate Trait Implementations",
"crate-trait-implementations",
);
write!(w, "<div id=\"crate-trait-implementations-list\">{impls}</div>").unwrap();
}

if !concrete_nonlocal.is_empty() {
write_impl_section_heading(
&mut w,
"External Trait Implementations",
"external-trait-implementations",
);
w.write_str("<div id=\"external-trait-implementations-list\">").unwrap();
render_impls(cx, &mut w, &concrete_nonlocal, containing_item, false);
w.write_str("</div>").unwrap();
}

if !synthetic.is_empty() {
Expand Down
44 changes: 41 additions & 3 deletions src/librustdoc/html/render/sidebar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,39 @@ fn sidebar_render_assoc_items(
blanket_impl: Vec<&Impl>,
items: &mut Vec<LinkBlock<'_>>,
) {
let format_concrete_impls = |impls: Vec<&Impl>, id_map: &mut IdMap| {
let mut links = FxHashSet::default();

let (locals, externals): (Vec<(Link<'static>, bool)>, Vec<(Link<'static>, bool)>) = impls
.iter()
.filter_map(|it| {
let trait_ = it.inner_impl().trait_.as_ref()?;

let encoded = id_map.derive(super::get_id_for_impl(cx.tcx(), it.impl_item.item_id));

let prefix = match it.inner_impl().polarity {
ty::ImplPolarity::Positive | ty::ImplPolarity::Reservation => "",
ty::ImplPolarity::Negative => "!",
};
let generated = Link::new(encoded, format!("{prefix}{:#}", trait_.print(cx)));
if links.insert(generated.clone()) {
Some((generated, trait_.res.def_id().is_local()))
} else {
None
}
})
.partition(|elem| elem.1);

let mut locals = locals.into_iter().map(|elem| elem.0).collect::<Vec<Link<'static>>>();
locals.sort();

let mut externals =
externals.into_iter().map(|elem| elem.0).collect::<Vec<Link<'static>>>();
externals.sort();

(locals, externals)
};

let format_impls = |impls: Vec<&Impl>, id_map: &mut IdMap| {
let mut links = FxHashSet::default();

Expand All @@ -641,14 +674,19 @@ fn sidebar_render_assoc_items(
ret
};

let concrete = format_impls(concrete, id_map);
let (concrete_locals, concrete_externals) = format_concrete_impls(concrete, id_map);
let synthetic = format_impls(synthetic, id_map);
let blanket = format_impls(blanket_impl, id_map);
items.extend([
LinkBlock::new(
Link::new("trait-implementations", "Trait Implementations"),
Link::new("crate-trait-implementations", "Crate Trait Implementations"),
"trait-implementation",
concrete_locals,
),
LinkBlock::new(
Link::new("external-trait-implementations", "External Trait Implementations"),
"trait-implementation",
concrete,
concrete_externals,
),
LinkBlock::new(
Link::new("synthetic-implementations", "Auto Trait Implementations"),
Expand Down
Loading