Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 40bd335

Browse files
Rollup merge of rust-lang#84320 - jsha:details-implementors, r=Manishearth,Nemo157,GuillaumeGomez
Use details tag for trait implementors. Part of rust-lang#83332 and following on from rust-lang#83337 and rust-lang#83355. This removes one category of JS-generated toggles (implementors), and replaces them with a `<details>` tag. This simplifies the JS, and fixes some bugs where things that were supposed to be hidden by the toggle were not hidden. Compare https://hoffman-andrews.com/rust/details-implementors/std/io/trait.Read.html#impl-Read vs https://doc.rust-lang.org/nightly/std/io/trait.Read.html#implementors. This introduces a `left: -23px` to put the toggle in the correct place, matching the current style for `.collapse-toggle`. It's worth noting this introduces a slight behavior change: since the entire line is now a `<summary>`, any part of the line is clickable. So for instance, in `impl Read for File`, clicking `impl` or `for` will collapse / expand the docs. Clicking `Read` or `File` still links to the appropriate documentation as before.
2 parents 13bcf03 + 569096c commit 40bd335

25 files changed

+68
-66
lines changed

src/librustdoc/html/render/mod.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,7 @@ fn render_impl(
12841284
let cache = cx.cache();
12851285
let traits = &cache.traits;
12861286
let trait_ = i.trait_did_full(cache).map(|did| &traits[&did]);
1287+
let mut close_tags = String::new();
12871288

12881289
if render_mode == RenderMode::Normal {
12891290
let id = cx.derive_id(match i.inner_impl().trait_ {
@@ -1302,7 +1303,12 @@ fn render_impl(
13021303
format!(" aliases=\"{}\"", aliases.join(","))
13031304
};
13041305
if let Some(use_absolute) = use_absolute {
1305-
write!(w, "<h3 id=\"{}\" class=\"impl\"{}><code class=\"in-band\">", id, aliases);
1306+
write!(
1307+
w,
1308+
"<details class=\"rustdoc-toggle implementors-toggle\"><summary><h3 id=\"{}\" class=\"impl\"{}><code class=\"in-band\">",
1309+
id, aliases
1310+
);
1311+
close_tags.insert_str(0, "</details>");
13061312
write!(w, "{}", i.inner_impl().print(use_absolute, cx));
13071313
if show_def_docs {
13081314
for it in &i.inner_impl().items {
@@ -1325,11 +1331,12 @@ fn render_impl(
13251331
} else {
13261332
write!(
13271333
w,
1328-
"<h3 id=\"{}\" class=\"impl\"{}><code class=\"in-band\">{}</code>",
1334+
"<details class=\"rustdoc-toggle implementors-toggle\"><summary><h3 id=\"{}\" class=\"impl\"{}><code class=\"in-band\">{}</code>",
13291335
id,
13301336
aliases,
13311337
i.inner_impl().print(false, cx)
13321338
);
1339+
close_tags.insert_str(0, "</details>");
13331340
}
13341341
write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id);
13351342
render_stability_since_raw(
@@ -1341,6 +1348,7 @@ fn render_impl(
13411348
);
13421349
write_srclink(cx, &i.impl_item, w);
13431350
w.write_str("</h3>");
1351+
w.write_str("</summary>");
13441352

13451353
if trait_.is_some() {
13461354
if let Some(portability) = portability(&i.impl_item, Some(parent)) {
@@ -1542,6 +1550,7 @@ fn render_impl(
15421550
}
15431551

15441552
w.write_str("<div class=\"impl-items\">");
1553+
close_tags.insert_str(0, "</div>");
15451554
for trait_item in &i.inner_impl().items {
15461555
doc_impl_item(
15471556
w,
@@ -1612,7 +1621,7 @@ fn render_impl(
16121621
);
16131622
}
16141623
}
1615-
w.write_str("</div>");
1624+
w.write_str(&close_tags);
16161625
}
16171626

16181627
fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Buffer) {

src/librustdoc/html/static/main.js

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,31 +1196,18 @@ function hideThemeButtonState() {
11961196
if (!next) {
11971197
return;
11981198
}
1199-
if (hasClass(e, "impl") &&
1200-
(next.getElementsByClassName("method").length > 0 ||
1201-
next.getElementsByClassName("associatedconstant").length > 0)) {
1202-
var newToggle = toggle.cloneNode(true);
1203-
insertAfter(newToggle, e.childNodes[e.childNodes.length - 1]);
1204-
// In case the option "auto-collapse implementors" is not set to false, we collapse
1205-
// all implementors.
1206-
if (hideImplementors === true && e.parentNode.id === "implementors-list") {
1207-
collapseDocs(newToggle, "hide");
1208-
}
1209-
}
12101199
};
12111200

12121201
onEachLazy(document.getElementsByClassName("method"), func);
12131202
onEachLazy(document.getElementsByClassName("associatedconstant"), func);
1214-
onEachLazy(document.getElementsByClassName("impl"), funcImpl);
12151203
var impl_call = function() {};
1216-
// Large items are hidden by default in the HTML. If the setting overrides that, show 'em.
1217-
if (!hideLargeItemContents) {
1218-
onEachLazy(document.getElementsByTagName("details"), function (e) {
1219-
if (hasClass(e, "type-contents-toggle")) {
1220-
e.open = true;
1221-
}
1222-
});
1223-
}
1204+
onEachLazy(document.getElementsByTagName("details"), function (e) {
1205+
var showLargeItem = !hideLargeItemContents && hasClass(e, "type-contents-toggle");
1206+
var showImplementor = !hideImplementors && hasClass(e, "implementors-toggle");
1207+
if (showLargeItem || showImplementor) {
1208+
e.open = true;
1209+
}
1210+
});
12241211
if (hideMethodDocs === true) {
12251212
impl_call = function(e, newToggle) {
12261213
if (e.id.match(/^impl(?:-\d+)?$/) === null) {

src/librustdoc/html/static/rustdoc.css

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1572,6 +1572,10 @@ h4 > .notable-traits {
15721572
left: -10px;
15731573
}
15741574

1575+
.item-list > details.rustdoc-toggle > summary:not(.hideme)::before {
1576+
left: -10px;
1577+
}
1578+
15751579
#all-types {
15761580
margin: 10px;
15771581
}
@@ -1786,14 +1790,16 @@ details.rustdoc-toggle > summary::before {
17861790
font-weight: 300;
17871791
font-size: 0.8em;
17881792
letter-spacing: 1px;
1793+
cursor: pointer;
17891794
}
17901795

17911796
details.rustdoc-toggle > summary.hideme::before {
17921797
position: relative;
17931798
}
17941799

17951800
details.rustdoc-toggle > summary:not(.hideme)::before {
1796-
float: left;
1801+
position: absolute;
1802+
left: -23px;
17971803
}
17981804

17991805
/* When a "hideme" summary is open and the "Expand description" or "Show

src/test/rustdoc/const-generics/add-impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub struct Simd<T, const WIDTH: usize> {
88
inner: T,
99
}
1010

11-
// @has foo/struct.Simd.html '//div[@id="trait-implementations-list"]/h3/code' 'impl Add<Simd<u8, 16_usize>> for Simd<u8, 16>'
11+
// @has foo/struct.Simd.html '//div[@id="trait-implementations-list"]//h3/code' 'impl Add<Simd<u8, 16_usize>> for Simd<u8, 16>'
1212
impl Add for Simd<u8, 16> {
1313
type Output = Self;
1414

src/test/rustdoc/duplicate_impls/issue-33054.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// @has issue_33054/impls/struct.Foo.html
22
// @has - '//code' 'impl Foo'
33
// @has - '//code' 'impl Bar for Foo'
4-
// @count - '//*[@id="trait-implementations-list"]/*[@class="impl"]' 1
5-
// @count - '//*[@id="main"]/*[@class="impl"]' 1
4+
// @count - '//*[@id="trait-implementations-list"]//*[@class="impl"]' 1
5+
// @count - '//*[@id="main"]/details/summary/*[@class="impl"]' 1
66
// @has issue_33054/impls/bar/trait.Bar.html
77
// @has - '//code' 'impl Bar for Foo'
88
// @count - '//*[@class="struct"]' 1

src/test/rustdoc/issue-21474.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ mod inner {
77
pub trait Blah { }
88

99
// @count issue_21474/struct.What.html \
10-
// '//*[@id="trait-implementations-list"]/*[@class="impl"]' 1
10+
// '//*[@id="trait-implementations-list"]//*[@class="impl"]' 1
1111
pub struct What;

src/test/rustdoc/issue-29503.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub trait MyTrait {
55
fn my_string(&self) -> String;
66
}
77

8-
// @has - "//div[@id='implementors-list']/h3[@id='impl-MyTrait']//code" "impl<T> MyTrait for T where T: Debug"
8+
// @has - "//div[@id='implementors-list']//h3[@id='impl-MyTrait']//code" "impl<T> MyTrait for T where T: Debug"
99
impl<T> MyTrait for T where T: fmt::Debug {
1010
fn my_string(&self) -> String {
1111
format!("{:?}", self)

src/test/rustdoc/issue-45584.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ pub trait Bar<T, U> {}
44

55
// @has 'foo/struct.Foo1.html'
66
pub struct Foo1;
7-
// @count - '//*[@id="trait-implementations-list"]/*[@class="impl"]' 1
7+
// @count - '//*[@id="trait-implementations-list"]//*[@class="impl"]' 1
88
// @has - '//*[@class="impl"]' "impl Bar<Foo1, &'static Foo1> for Foo1"
99
impl Bar<Foo1, &'static Foo1> for Foo1 {}
1010

1111
// @has 'foo/struct.Foo2.html'
1212
pub struct Foo2;
13-
// @count - '//*[@id="trait-implementations-list"]/*[@class="impl"]' 1
13+
// @count - '//*[@id="trait-implementations-list"]//*[@class="impl"]' 1
1414
// @has - '//*[@class="impl"]' "impl Bar<&'static Foo2, Foo2> for u8"
1515
impl Bar<&'static Foo2, Foo2> for u8 {}

src/test/rustdoc/issue-50159.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ impl<B, C> Signal2 for B where B: Signal<Item = C> {
1313
// @has issue_50159/struct.Switch.html
1414
// @has - '//code' 'impl<B> Send for Switch<B> where <B as Signal>::Item: Send'
1515
// @has - '//code' 'impl<B> Sync for Switch<B> where <B as Signal>::Item: Sync'
16-
// @count - '//*[@id="implementations-list"]/*[@class="impl"]' 0
17-
// @count - '//*[@id="synthetic-implementations-list"]/*[@class="impl"]' 5
16+
// @count - '//*[@id="implementations-list"]//*[@class="impl"]' 0
17+
// @count - '//*[@id="synthetic-implementations-list"]//*[@class="impl"]' 5
1818
pub struct Switch<B: Signal> {
1919
pub inner: <B as Signal2>::Item2,
2020
}

src/test/rustdoc/issue-51236.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub mod traits {
77
}
88

99
// @has issue_51236/struct.Owned.html
10-
// @has - '//*[@id="synthetic-implementations-list"]/*[@class="impl"]//code' "impl<T> Send for \
10+
// @has - '//*[@id="synthetic-implementations-list"]//*[@class="impl"]//code' "impl<T> Send for \
1111
// Owned<T> where <T as Owned<'static>>::Reader: Send"
1212
pub struct Owned<T> where T: for<'a> ::traits::Owned<'a> {
1313
marker: PhantomData<<T as ::traits::Owned<'static>>::Reader>,

0 commit comments

Comments
 (0)