1
1
use clean::AttributesExt;
2
2
3
3
use std::cmp::Ordering;
4
+ use std::fmt;
4
5
5
6
use rustc_data_structures::fx::FxHashMap;
6
7
use rustc_hir as hir;
@@ -155,7 +156,7 @@ fn should_hide_fields(n_fields: usize) -> bool {
155
156
n_fields > 12
156
157
}
157
158
158
- fn toggle_open ( w : & mut Buffer , text : & str ) {
159
+ fn toggle_open(w: &mut Buffer, text: impl fmt::Display ) {
159
160
write!(
160
161
w,
161
162
"<details class=\"rustdoc-toggle type-contents-toggle\">\
@@ -481,6 +482,9 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
481
482
let consts = t.items.iter().filter(|m| m.is_associated_const()).collect::<Vec<_>>();
482
483
let required = t.items.iter().filter(|m| m.is_ty_method()).collect::<Vec<_>>();
483
484
let provided = t.items.iter().filter(|m| m.is_method()).collect::<Vec<_>>();
485
+ let count_types = types.len();
486
+ let count_consts = consts.len();
487
+ let count_methods = required.len() + provided.len();
484
488
485
489
// Output the trait definition
486
490
wrap_into_docblock(w, |w| {
@@ -511,9 +515,12 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
511
515
let mut toggle = false;
512
516
513
517
// If there are too many associated types, hide _everything_
514
- if should_hide_fields ( types . len ( ) ) {
518
+ if should_hide_fields(count_types ) {
515
519
toggle = true;
516
- toggle_open ( w, "associated items" ) ;
520
+ toggle_open(
521
+ w,
522
+ format_args!("{} associated items", count_types + count_consts + count_methods),
523
+ );
517
524
}
518
525
for t in &types {
519
526
render_assoc_item(w, t, AssocItemLink::Anchor(None), ItemType::Trait, cx);
@@ -523,9 +530,18 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
523
530
// We also do this if the types + consts is large because otherwise we could
524
531
// render a bunch of types and _then_ a bunch of consts just because both were
525
532
// _just_ under the limit
526
- if !toggle && should_hide_fields ( types . len ( ) + consts . len ( ) ) {
533
+ if !toggle && should_hide_fields(count_types + count_consts ) {
527
534
toggle = true;
528
- toggle_open ( w, "associated constants and methods" ) ;
535
+ toggle_open(
536
+ w,
537
+ format_args!(
538
+ "{} associated constant{} and {} method{}",
539
+ count_consts,
540
+ pluralize(count_consts),
541
+ count_methods,
542
+ pluralize(count_methods),
543
+ ),
544
+ );
529
545
}
530
546
if !types.is_empty() && !consts.is_empty() {
531
547
w.write_str("\n");
@@ -534,9 +550,9 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
534
550
render_assoc_item(w, t, AssocItemLink::Anchor(None), ItemType::Trait, cx);
535
551
w.write_str(";\n");
536
552
}
537
- if !toggle && should_hide_fields ( required . len ( ) + provided . len ( ) ) {
553
+ if !toggle && should_hide_fields(count_methods ) {
538
554
toggle = true;
539
- toggle_open ( w, " methods") ;
555
+ toggle_open(w, format_args!("{} methods", count_methods) );
540
556
}
541
557
if !consts.is_empty() && !required.is_empty() {
542
558
w.write_str("\n");
@@ -924,9 +940,10 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum
924
940
w.write_str(" {}");
925
941
} else {
926
942
w.write_str(" {\n");
927
- let toggle = should_hide_fields ( e. variants . len ( ) ) ;
943
+ let count_variants = e.variants.len();
944
+ let toggle = should_hide_fields(count_variants);
928
945
if toggle {
929
- toggle_open ( w, " variants") ;
946
+ toggle_open(w, format_args!("{} variants", count_variants) );
930
947
}
931
948
for v in &e.variants {
932
949
w.write_str(" ");
@@ -1003,7 +1020,8 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum
1003
1020
1004
1021
use crate::clean::Variant;
1005
1022
if let clean::VariantItem(Variant::Struct(ref s)) = *variant.kind {
1006
- toggle_open ( w, "fields" ) ;
1023
+ let count_fields = s.fields.len();
1024
+ toggle_open(w, format_args!("{} field{}", count_fields, pluralize(count_fields)));
1007
1025
let variant_id = cx.derive_id(format!(
1008
1026
"{}.{}.fields",
1009
1027
ItemType::Variant,
@@ -1376,7 +1394,7 @@ fn render_union(
1376
1394
fields.iter().filter(|f| matches!(*f.kind, clean::StructFieldItem(..))).count();
1377
1395
let toggle = should_hide_fields(count_fields);
1378
1396
if toggle {
1379
- toggle_open ( w, " fields") ;
1397
+ toggle_open(w, format_args!("{} fields", count_fields) );
1380
1398
}
1381
1399
1382
1400
for field in fields {
@@ -1432,7 +1450,7 @@ fn render_struct(
1432
1450
let has_visible_fields = count_fields > 0;
1433
1451
let toggle = should_hide_fields(count_fields);
1434
1452
if toggle {
1435
- toggle_open ( w, " fields") ;
1453
+ toggle_open(w, format_args!("{} fields", count_fields) );
1436
1454
}
1437
1455
for field in fields {
1438
1456
if let clean::StructFieldItem(ref ty) = *field.kind {
@@ -1609,3 +1627,7 @@ fn document_type_layout(w: &mut Buffer, cx: &Context<'_>, ty_def_id: DefId) {
1609
1627
1610
1628
writeln!(w, "</div>");
1611
1629
}
1630
+
1631
+ fn pluralize(count: usize) -> &'static str {
1632
+ if count > 1 { "s" } else { "" }
1633
+ }
0 commit comments