Skip to content

Commit ea829ee

Browse files
committed
make 'render_assoc_item' return 'impl fmt::Display'
1 parent bae1021 commit ea829ee

File tree

2 files changed

+127
-148
lines changed

2 files changed

+127
-148
lines changed

src/librustdoc/html/render/mod.rs

Lines changed: 72 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ mod type_layout;
3838
mod write_shared;
3939

4040
use std::collections::VecDeque;
41-
use std::fmt::{self, Write};
41+
use std::fmt::{self, Display as _, Write};
4242
use std::iter::Peekable;
4343
use std::path::PathBuf;
4444
use std::{fs, str};
@@ -1111,112 +1111,70 @@ fn render_stability_since_raw(
11111111
render_stability_since_raw_with_extra(w, ver, const_stability, "")
11121112
}
11131113

1114-
fn render_assoc_item(
1115-
w: &mut String,
1116-
item: &clean::Item,
1117-
link: AssocItemLink<'_>,
1114+
fn render_assoc_item<'a, 'tcx>(
1115+
item: &'a clean::Item,
1116+
link: AssocItemLink<'a>,
11181117
parent: ItemType,
1119-
cx: &Context<'_>,
1118+
cx: &'a Context<'tcx>,
11201119
render_mode: RenderMode,
1121-
) {
1122-
match &item.kind {
1123-
clean::StrippedItem(..) => {}
1120+
) -> impl fmt::Display + 'a + Captures<'tcx> {
1121+
fmt::from_fn(move |f| match &item.kind {
1122+
clean::StrippedItem(..) => Ok(()),
11241123
clean::RequiredMethodItem(m) | clean::MethodItem(m, _) => {
1125-
write_str(
1126-
w,
1127-
format_args!(
1128-
"{}",
1129-
assoc_method(item, &m.generics, &m.decl, link, parent, cx, render_mode)
1130-
),
1131-
);
1132-
}
1133-
clean::RequiredAssocConstItem(generics, ty) => {
1134-
write_str(
1135-
w,
1136-
format_args!(
1137-
"{}",
1138-
assoc_const(
1139-
item,
1140-
generics,
1141-
ty,
1142-
AssocConstValue::None,
1143-
link,
1144-
if parent == ItemType::Trait { 4 } else { 0 },
1145-
cx,
1146-
)
1147-
),
1148-
);
1149-
}
1150-
clean::ProvidedAssocConstItem(ci) => {
1151-
write_str(
1152-
w,
1153-
format_args!(
1154-
"{}",
1155-
assoc_const(
1156-
item,
1157-
&ci.generics,
1158-
&ci.type_,
1159-
AssocConstValue::TraitDefault(&ci.kind),
1160-
link,
1161-
if parent == ItemType::Trait { 4 } else { 0 },
1162-
cx,
1163-
)
1164-
),
1165-
);
1166-
}
1167-
clean::ImplAssocConstItem(ci) => {
1168-
write_str(
1169-
w,
1170-
format_args!(
1171-
"{}",
1172-
assoc_const(
1173-
item,
1174-
&ci.generics,
1175-
&ci.type_,
1176-
AssocConstValue::Impl(&ci.kind),
1177-
link,
1178-
if parent == ItemType::Trait { 4 } else { 0 },
1179-
cx,
1180-
)
1181-
),
1182-
);
1183-
}
1184-
clean::RequiredAssocTypeItem(ref generics, ref bounds) => {
1185-
write_str(
1186-
w,
1187-
format_args!(
1188-
"{}",
1189-
assoc_type(
1190-
item,
1191-
generics,
1192-
bounds,
1193-
None,
1194-
link,
1195-
if parent == ItemType::Trait { 4 } else { 0 },
1196-
cx,
1197-
)
1198-
),
1199-
);
1200-
}
1201-
clean::AssocTypeItem(ref ty, ref bounds) => {
1202-
write_str(
1203-
w,
1204-
format_args!(
1205-
"{}",
1206-
assoc_type(
1207-
item,
1208-
&ty.generics,
1209-
bounds,
1210-
Some(ty.item_type.as_ref().unwrap_or(&ty.type_)),
1211-
link,
1212-
if parent == ItemType::Trait { 4 } else { 0 },
1213-
cx,
1214-
)
1215-
),
1216-
);
1217-
}
1124+
assoc_method(item, &m.generics, &m.decl, link, parent, cx, render_mode).fmt(f)
1125+
}
1126+
clean::RequiredAssocConstItem(generics, ty) => assoc_const(
1127+
item,
1128+
generics,
1129+
ty,
1130+
AssocConstValue::None,
1131+
link,
1132+
if parent == ItemType::Trait { 4 } else { 0 },
1133+
cx,
1134+
)
1135+
.fmt(f),
1136+
clean::ProvidedAssocConstItem(ci) => assoc_const(
1137+
item,
1138+
&ci.generics,
1139+
&ci.type_,
1140+
AssocConstValue::TraitDefault(&ci.kind),
1141+
link,
1142+
if parent == ItemType::Trait { 4 } else { 0 },
1143+
cx,
1144+
)
1145+
.fmt(f),
1146+
clean::ImplAssocConstItem(ci) => assoc_const(
1147+
item,
1148+
&ci.generics,
1149+
&ci.type_,
1150+
AssocConstValue::Impl(&ci.kind),
1151+
link,
1152+
if parent == ItemType::Trait { 4 } else { 0 },
1153+
cx,
1154+
)
1155+
.fmt(f),
1156+
clean::RequiredAssocTypeItem(ref generics, ref bounds) => assoc_type(
1157+
item,
1158+
generics,
1159+
bounds,
1160+
None,
1161+
link,
1162+
if parent == ItemType::Trait { 4 } else { 0 },
1163+
cx,
1164+
)
1165+
.fmt(f),
1166+
clean::AssocTypeItem(ref ty, ref bounds) => assoc_type(
1167+
item,
1168+
&ty.generics,
1169+
bounds,
1170+
Some(ty.item_type.as_ref().unwrap_or(&ty.type_)),
1171+
link,
1172+
if parent == ItemType::Trait { 4 } else { 0 },
1173+
cx,
1174+
)
1175+
.fmt(f),
12181176
_ => panic!("render_assoc_item called on non-associated-item"),
1219-
}
1177+
})
12201178
}
12211179

12221180
// When an attribute is rendered inside a `<pre>` tag, it is formatted using
@@ -1791,16 +1749,19 @@ fn render_impl(
17911749
// Anchors are only used on trait impls.
17921750
write_str(w, format_args!("<a href=\"#{id}\" class=\"anchor\">§</a>"));
17931751
}
1794-
w.push_str("<h4 class=\"code-header\">");
1795-
render_assoc_item(
1752+
write_str(
17961753
w,
1797-
item,
1798-
link.anchor(source_id.as_ref().unwrap_or(&id)),
1799-
ItemType::Impl,
1800-
cx,
1801-
render_mode,
1754+
format_args!(
1755+
"<h4 class=\"code-header\">{}</h4></section>",
1756+
render_assoc_item(
1757+
item,
1758+
link.anchor(source_id.as_ref().unwrap_or(&id)),
1759+
ItemType::Impl,
1760+
cx,
1761+
render_mode,
1762+
),
1763+
),
18021764
);
1803-
w.push_str("</h4></section>");
18041765
}
18051766
}
18061767
clean::RequiredAssocConstItem(ref generics, ref ty) => {

src/librustdoc/html/render/print_item.rs

Lines changed: 55 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -696,15 +696,19 @@ fn item_trait(w: &mut String, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
696696
}
697697
for types in [&required_types, &provided_types] {
698698
for t in types {
699-
render_assoc_item(
699+
write_str(
700700
w,
701-
t,
702-
AssocItemLink::Anchor(None),
703-
ItemType::Trait,
704-
cx,
705-
RenderMode::Normal,
701+
format_args_nl!(
702+
"{};",
703+
render_assoc_item(
704+
t,
705+
AssocItemLink::Anchor(None),
706+
ItemType::Trait,
707+
cx,
708+
RenderMode::Normal,
709+
)
710+
),
706711
);
707-
w.push_str(";\n");
708712
}
709713
}
710714
// If there are too many associated constants, hide everything after them
@@ -728,15 +732,19 @@ fn item_trait(w: &mut String, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
728732
}
729733
for consts in [&required_consts, &provided_consts] {
730734
for c in consts {
731-
render_assoc_item(
735+
write_str(
732736
w,
733-
c,
734-
AssocItemLink::Anchor(None),
735-
ItemType::Trait,
736-
cx,
737-
RenderMode::Normal,
737+
format_args_nl!(
738+
"{};",
739+
render_assoc_item(
740+
c,
741+
AssocItemLink::Anchor(None),
742+
ItemType::Trait,
743+
cx,
744+
RenderMode::Normal,
745+
)
746+
),
738747
);
739-
w.push_str(";\n");
740748
}
741749
}
742750
if !toggle && should_hide_fields(count_methods) {
@@ -754,15 +762,19 @@ fn item_trait(w: &mut String, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
754762
);
755763
}
756764
for (pos, m) in required_methods.iter().enumerate() {
757-
render_assoc_item(
765+
write_str(
758766
w,
759-
m,
760-
AssocItemLink::Anchor(None),
761-
ItemType::Trait,
762-
cx,
763-
RenderMode::Normal,
767+
format_args_nl!(
768+
"{};",
769+
render_assoc_item(
770+
m,
771+
AssocItemLink::Anchor(None),
772+
ItemType::Trait,
773+
cx,
774+
RenderMode::Normal,
775+
)
776+
),
764777
);
765-
w.push_str(";\n");
766778

767779
if pos < required_methods.len() - 1 {
768780
w.push_str("<span class=\"item-spacer\"></span>");
@@ -779,17 +791,20 @@ fn item_trait(w: &mut String, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
779791
);
780792
}
781793
for (pos, m) in provided_methods.iter().enumerate() {
782-
render_assoc_item(
794+
write_str(
783795
w,
784-
m,
785-
AssocItemLink::Anchor(None),
786-
ItemType::Trait,
787-
cx,
788-
RenderMode::Normal,
796+
format_args_nl!(
797+
"{} {{ ... }}",
798+
render_assoc_item(
799+
m,
800+
AssocItemLink::Anchor(None),
801+
ItemType::Trait,
802+
cx,
803+
RenderMode::Normal,
804+
)
805+
),
789806
);
790807

791-
w.push_str(" { ... }\n");
792-
793808
if pos < provided_methods.len() - 1 {
794809
w.push_str("<span class=\"item-spacer\"></span>");
795810
}
@@ -823,16 +838,19 @@ fn item_trait(w: &mut String, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
823838
}
824839
write_str(w, format_args!("<section id=\"{id}\" class=\"method\">"));
825840
render_rightside(w, cx, m, RenderMode::Normal);
826-
write_str(w, format_args!("<h4 class=\"code-header\">"));
827-
render_assoc_item(
841+
write_str(
828842
w,
829-
m,
830-
AssocItemLink::Anchor(Some(&id)),
831-
ItemType::Impl,
832-
cx,
833-
RenderMode::Normal,
843+
format_args!(
844+
"<h4 class=\"code-header\">{}</h4></section>",
845+
render_assoc_item(
846+
m,
847+
AssocItemLink::Anchor(Some(&id)),
848+
ItemType::Impl,
849+
cx,
850+
RenderMode::Normal,
851+
)
852+
),
834853
);
835-
w.push_str("</h4></section>");
836854
document_item_info(cx, m, Some(t)).render_into(w).unwrap();
837855
if toggled {
838856
write_str(w, format_args!("</summary>"));

0 commit comments

Comments
 (0)