Skip to content

Commit 900cda2

Browse files
Print only blanket implementations on reference primitive type
1 parent 6c585fc commit 900cda2

File tree

2 files changed

+133
-67
lines changed

2 files changed

+133
-67
lines changed

src/librustdoc/html/render/mod.rs

+118-62
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ fn short_item_info(
616616
pub(crate) fn render_impls(
617617
cx: &mut Context<'_>,
618618
w: &mut Buffer,
619-
impls: &[&&Impl],
619+
impls: &[&Impl],
620620
containing_item: &clean::Item,
621621
toggle_open_by_default: bool,
622622
) {
@@ -1039,9 +1039,9 @@ pub(crate) fn render_all_impls(
10391039
w: &mut Buffer,
10401040
cx: &mut Context<'_>,
10411041
containing_item: &clean::Item,
1042-
concrete: &[&&Impl],
1043-
synthetic: &[&&Impl],
1044-
blanket_impl: &[&&Impl],
1042+
concrete: &[&Impl],
1043+
synthetic: &[&Impl],
1044+
blanket_impl: &[&Impl],
10451045
) {
10461046
let mut impls = Buffer::empty_from(w);
10471047
render_impls(cx, &mut impls, concrete, containing_item, true);
@@ -1158,9 +1158,9 @@ fn render_assoc_items_inner(
11581158
return;
11591159
}
11601160

1161-
let (synthetic, concrete): (Vec<&&Impl>, Vec<&&Impl>) =
1162-
traits.iter().partition(|t| t.inner_impl().kind.is_auto());
1163-
let (blanket_impl, concrete): (Vec<&&Impl>, _) =
1161+
let (synthetic, concrete): (Vec<&Impl>, Vec<&Impl>) =
1162+
traits.into_iter().partition(|t| t.inner_impl().kind.is_auto());
1163+
let (blanket_impl, concrete): (Vec<&Impl>, _) =
11641164
concrete.into_iter().partition(|t| t.inner_impl().kind.is_blanket());
11651165

11661166
render_all_impls(w, cx, containing_item, &concrete, &synthetic, &blanket_impl);
@@ -1968,6 +1968,70 @@ fn small_url_encode(s: String) -> String {
19681968
}
19691969
}
19701970

1971+
pub(crate) fn sidebar_render_assoc_items(
1972+
cx: &Context<'_>,
1973+
out: &mut Buffer,
1974+
id_map: &mut IdMap,
1975+
concrete: Vec<&Impl>,
1976+
synthetic: Vec<&Impl>,
1977+
blanket_impl: Vec<&Impl>,
1978+
) {
1979+
let format_impls = |impls: Vec<&Impl>, id_map: &mut IdMap| {
1980+
let mut links = FxHashSet::default();
1981+
1982+
let mut ret = impls
1983+
.iter()
1984+
.filter_map(|it| {
1985+
let trait_ = it.inner_impl().trait_.as_ref()?;
1986+
let encoded =
1987+
id_map.derive(get_id_for_impl(&it.inner_impl().for_, Some(trait_), cx));
1988+
1989+
let i_display = format!("{:#}", trait_.print(cx));
1990+
let out = Escape(&i_display);
1991+
let prefix = match it.inner_impl().polarity {
1992+
ty::ImplPolarity::Positive | ty::ImplPolarity::Reservation => "",
1993+
ty::ImplPolarity::Negative => "!",
1994+
};
1995+
let generated = format!("<a href=\"#{}\">{}{}</a>", encoded, prefix, out);
1996+
if links.insert(generated.clone()) { Some(generated) } else { None }
1997+
})
1998+
.collect::<Vec<String>>();
1999+
ret.sort();
2000+
ret
2001+
};
2002+
2003+
let concrete_format = format_impls(concrete, id_map);
2004+
let synthetic_format = format_impls(synthetic, id_map);
2005+
let blanket_format = format_impls(blanket_impl, id_map);
2006+
2007+
if !concrete_format.is_empty() {
2008+
print_sidebar_block(
2009+
out,
2010+
"trait-implementations",
2011+
"Trait Implementations",
2012+
concrete_format.iter(),
2013+
);
2014+
}
2015+
2016+
if !synthetic_format.is_empty() {
2017+
print_sidebar_block(
2018+
out,
2019+
"synthetic-implementations",
2020+
"Auto Trait Implementations",
2021+
synthetic_format.iter(),
2022+
);
2023+
}
2024+
2025+
if !blanket_format.is_empty() {
2026+
print_sidebar_block(
2027+
out,
2028+
"blanket-implementations",
2029+
"Blanket Implementations",
2030+
blanket_format.iter(),
2031+
);
2032+
}
2033+
}
2034+
19712035
fn sidebar_assoc_items(cx: &Context<'_>, out: &mut Buffer, it: &clean::Item) {
19722036
let did = it.item_id.expect_def_id();
19732037
let cache = cx.cache();
@@ -2016,65 +2080,12 @@ fn sidebar_assoc_items(cx: &Context<'_>, out: &mut Buffer, it: &clean::Item) {
20162080
sidebar_deref_methods(cx, out, impl_, v, &mut derefs, &mut used_links);
20172081
}
20182082

2019-
let format_impls = |impls: Vec<&Impl>, id_map: &mut IdMap| {
2020-
let mut links = FxHashSet::default();
2021-
2022-
let mut ret = impls
2023-
.iter()
2024-
.filter_map(|it| {
2025-
let trait_ = it.inner_impl().trait_.as_ref()?;
2026-
let encoded =
2027-
id_map.derive(get_id_for_impl(&it.inner_impl().for_, Some(trait_), cx));
2028-
2029-
let i_display = format!("{:#}", trait_.print(cx));
2030-
let out = Escape(&i_display);
2031-
let prefix = match it.inner_impl().polarity {
2032-
ty::ImplPolarity::Positive | ty::ImplPolarity::Reservation => "",
2033-
ty::ImplPolarity::Negative => "!",
2034-
};
2035-
let generated = format!("<a href=\"#{}\">{}{}</a>", encoded, prefix, out);
2036-
if links.insert(generated.clone()) { Some(generated) } else { None }
2037-
})
2038-
.collect::<Vec<String>>();
2039-
ret.sort();
2040-
ret
2041-
};
2042-
20432083
let (synthetic, concrete): (Vec<&Impl>, Vec<&Impl>) =
20442084
v.iter().partition::<Vec<_>, _>(|i| i.inner_impl().kind.is_auto());
20452085
let (blanket_impl, concrete): (Vec<&Impl>, Vec<&Impl>) =
20462086
concrete.into_iter().partition::<Vec<_>, _>(|i| i.inner_impl().kind.is_blanket());
20472087

2048-
let concrete_format = format_impls(concrete, &mut id_map);
2049-
let synthetic_format = format_impls(synthetic, &mut id_map);
2050-
let blanket_format = format_impls(blanket_impl, &mut id_map);
2051-
2052-
if !concrete_format.is_empty() {
2053-
print_sidebar_block(
2054-
out,
2055-
"trait-implementations",
2056-
"Trait Implementations",
2057-
concrete_format.iter(),
2058-
);
2059-
}
2060-
2061-
if !synthetic_format.is_empty() {
2062-
print_sidebar_block(
2063-
out,
2064-
"synthetic-implementations",
2065-
"Auto Trait Implementations",
2066-
synthetic_format.iter(),
2067-
);
2068-
}
2069-
2070-
if !blanket_format.is_empty() {
2071-
print_sidebar_block(
2072-
out,
2073-
"blanket-implementations",
2074-
"Blanket Implementations",
2075-
blanket_format.iter(),
2076-
);
2077-
}
2088+
sidebar_render_assoc_items(cx, out, &mut id_map, concrete, synthetic, blanket_impl);
20782089
}
20792090
}
20802091
}
@@ -2344,9 +2355,54 @@ fn sidebar_trait(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, t: &clean
23442355
buf.push_str("</section>")
23452356
}
23462357

2358+
/// Returns the list of implementations for the primitive reference type, filtering out any
2359+
/// implementations that are on concrete or partially generic types, only keeping implementations
2360+
/// of the form `impl<T> Trait for &T`.
2361+
pub(crate) fn get_filtered_impls_for_reference<'a>(
2362+
shared: &'a Rc<SharedContext<'_>>,
2363+
it: &clean::Item,
2364+
) -> (Vec<&'a Impl>, Vec<&'a Impl>, Vec<&'a Impl>) {
2365+
let def_id = it.item_id.expect_def_id();
2366+
// If the reference primitive is somehow not defined, exit early.
2367+
let Some(v) = shared.cache.impls.get(&def_id) else { return (Vec::new(), Vec::new(), Vec::new()) };
2368+
// Since there is no "direct implementation" on the reference primitive type, we filter out
2369+
// every implementation which isn't a trait implementation.
2370+
let traits: Vec<_> = v.iter().filter(|i| i.inner_impl().trait_.is_some()).collect();
2371+
let (synthetic, concrete): (Vec<&Impl>, Vec<&Impl>) =
2372+
traits.into_iter().partition(|t| t.inner_impl().kind.is_auto());
2373+
2374+
let (blanket_impl, concrete): (Vec<&Impl>, _) =
2375+
concrete.into_iter().partition(|t| t.inner_impl().kind.is_blanket());
2376+
// Now we keep only references over full generic types.
2377+
let concrete: Vec<_> = concrete
2378+
.into_iter()
2379+
.filter(|t| match t.inner_impl().for_ {
2380+
clean::Type::BorrowedRef { ref type_, .. } => type_.is_full_generic(),
2381+
_ => false,
2382+
})
2383+
.collect();
2384+
2385+
(concrete, synthetic, blanket_impl)
2386+
}
2387+
23472388
fn sidebar_primitive(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item) {
23482389
let mut sidebar = Buffer::new();
2349-
sidebar_assoc_items(cx, &mut sidebar, it);
2390+
2391+
if it.name.map(|n| n.as_str() != "reference").unwrap_or(false) {
2392+
sidebar_assoc_items(cx, &mut sidebar, it);
2393+
} else {
2394+
let shared = Rc::clone(&cx.shared);
2395+
let (concrete, synthetic, blanket_impl) = get_filtered_impls_for_reference(&shared, it);
2396+
2397+
sidebar_render_assoc_items(
2398+
cx,
2399+
&mut sidebar,
2400+
&mut IdMap::new(),
2401+
concrete,
2402+
synthetic,
2403+
blanket_impl,
2404+
);
2405+
}
23502406

23512407
if !sidebar.is_empty() {
23522408
write!(buf, "<section>{}</section>", sidebar.into_inner());

src/librustdoc/html/render/print_item.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ use std::fmt;
1616
use std::rc::Rc;
1717

1818
use super::{
19-
collect_paths_for_type, document, ensure_trailing_slash, item_ty_to_section,
20-
notable_traits_decl, render_assoc_item, render_assoc_items, render_attributes_in_code,
21-
render_attributes_in_pre, render_impl, render_rightside, render_stability_since_raw,
22-
AssocItemLink, Context, ImplRenderingParameters,
19+
collect_paths_for_type, document, ensure_trailing_slash, get_filtered_impls_for_reference,
20+
item_ty_to_section, notable_traits_decl, render_all_impls, render_assoc_item,
21+
render_assoc_items, render_attributes_in_code, render_attributes_in_pre, render_impl,
22+
render_rightside, render_stability_since_raw, AssocItemLink, Context, ImplRenderingParameters,
2323
};
2424
use crate::clean;
2525
use crate::config::ModuleSorting;
@@ -1371,8 +1371,18 @@ fn item_proc_macro(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, m: &c
13711371
}
13721372

13731373
fn item_primitive(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item) {
1374+
let def_id = it.item_id.expect_def_id();
13741375
document(w, cx, it, None, HeadingOffset::H2);
1375-
render_assoc_items(w, cx, it, it.item_id.expect_def_id(), AssocItemRender::All)
1376+
if it.name.map(|n| n.as_str() != "reference").unwrap_or(false) {
1377+
render_assoc_items(w, cx, it, def_id, AssocItemRender::All);
1378+
} else {
1379+
// We handle the "reference" primitive type on its own because we only want to list
1380+
// implementations on generic types.
1381+
let shared = Rc::clone(&cx.shared);
1382+
let (concrete, synthetic, blanket_impl) = get_filtered_impls_for_reference(&shared, it);
1383+
1384+
render_all_impls(w, cx, it, &concrete, &synthetic, &blanket_impl);
1385+
}
13761386
}
13771387

13781388
fn item_constant(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, c: &clean::Constant) {

0 commit comments

Comments
 (0)