Skip to content

Commit 66ff9a4

Browse files
committed
rustdoc: factor commonly usable functions out of item_union
1 parent 2f6bc5d commit 66ff9a4

File tree

2 files changed

+59
-41
lines changed

2 files changed

+59
-41
lines changed

src/librustdoc/html/render/print_item.rs

+55-37
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ use rustc_middle::middle::stability;
99
use rustc_middle::ty::{self, TyCtxt};
1010
use rustc_span::hygiene::MacroKind;
1111
use rustc_span::symbol::{kw, sym, Symbol};
12+
use rustc_target::abi::{LayoutS, Primitive, TagEncoding, Variants};
13+
use std::borrow::Borrow;
14+
use std::cell::{RefCell, RefMut};
1215
use std::cmp::Ordering;
1316
use std::fmt;
1417
use std::rc::Rc;
@@ -1098,6 +1101,51 @@ fn item_opaque_ty(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &cl
10981101
write!(w, "{}", render_assoc_items(cx, it, it.item_id.expect_def_id(), AssocItemRender::All))
10991102
}
11001103

1104+
trait ItemTemplate<'a, 'cx: 'a> {
1105+
fn borrow_mut(&self) -> (&'a clean::Item, RefMut<'_, &'a mut Context<'cx>>);
1106+
}
1107+
fn item_template_render_assoc_items<'a: 'b, 'cx: 'a, 'b>(
1108+
this: &'b impl ItemTemplate<'a, 'cx>,
1109+
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1110+
display_fn(move |f| {
1111+
let (item, mut cx) = this.borrow_mut();
1112+
let def_id = item.item_id.expect_def_id();
1113+
let v = render_assoc_items(&mut *cx, item, def_id, AssocItemRender::All);
1114+
write!(f, "{v}")
1115+
})
1116+
}
1117+
1118+
fn item_template_document_type_layout<'a: 'b, 'cx: 'a, 'b>(
1119+
this: &'b impl ItemTemplate<'a, 'cx>,
1120+
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1121+
display_fn(move |f| {
1122+
let (item, mut cx) = this.borrow_mut();
1123+
let def_id = item.item_id.expect_def_id();
1124+
let v = document_type_layout(&mut *cx, def_id);
1125+
write!(f, "{v}")
1126+
})
1127+
}
1128+
1129+
fn item_template_document<'a: 'b, 'cx: 'a, 'b>(
1130+
this: &'b impl ItemTemplate<'a, 'cx>,
1131+
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1132+
display_fn(move |f| {
1133+
let (item, mut cx) = this.borrow_mut();
1134+
let v = document(&mut *cx, item, None, HeadingOffset::H2);
1135+
write!(f, "{v}")
1136+
})
1137+
}
1138+
1139+
fn item_template_render_attributes_in_pre<'a: 'b, 'cx: 'a, 'b>(
1140+
this: &'b impl ItemTemplate<'a, 'cx>,
1141+
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1142+
display_fn(move |f| {
1143+
let (it, cx) = this.borrow_mut();
1144+
let v = render_attributes_in_pre(it, "", cx.tcx());
1145+
write!(f, "{v}")
1146+
})
1147+
}
1148+
11011149
fn item_typedef(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean::Typedef) {
11021150
fn write_content(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Typedef) {
11031151
wrap_item(w, |w| {
@@ -1131,55 +1179,25 @@ fn item_union(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean:
11311179
#[derive(Template)]
11321180
#[template(path = "item_union.html")]
11331181
struct ItemUnion<'a, 'cx> {
1134-
cx: std::cell::RefCell<&'a mut Context<'cx>>,
1182+
cx: RefCell<&'a mut Context<'cx>>,
11351183
it: &'a clean::Item,
11361184
s: &'a clean::Union,
11371185
}
11381186

1139-
impl<'a, 'cx: 'a> ItemUnion<'a, 'cx> {
1140-
fn render_assoc_items<'b>(
1141-
&'b self,
1142-
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1143-
display_fn(move |f| {
1144-
let def_id = self.it.item_id.expect_def_id();
1145-
let mut cx = self.cx.borrow_mut();
1146-
let v = render_assoc_items(*cx, self.it, def_id, AssocItemRender::All);
1147-
write!(f, "{v}")
1148-
})
1149-
}
1150-
fn document_type_layout<'b>(
1151-
&'b self,
1152-
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1153-
display_fn(move |f| {
1154-
let def_id = self.it.item_id.expect_def_id();
1155-
let cx = self.cx.borrow_mut();
1156-
let v = document_type_layout(*cx, def_id);
1157-
write!(f, "{v}")
1158-
})
1187+
impl<'a, 'cx: 'a> ItemTemplate<'a, 'cx> for ItemUnion<'a, 'cx> {
1188+
fn borrow_mut(&self) -> (&'a clean::Item, RefMut<'_, &'a mut Context<'cx>>) {
1189+
(&self.it, self.cx.borrow_mut())
11591190
}
1191+
}
1192+
1193+
impl<'a, 'cx: 'a> ItemUnion<'a, 'cx> {
11601194
fn render_union<'b>(&'b self) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
11611195
display_fn(move |f| {
11621196
let cx = self.cx.borrow_mut();
11631197
let v = render_union(self.it, Some(&self.s.generics), &self.s.fields, *cx);
11641198
write!(f, "{v}")
11651199
})
11661200
}
1167-
fn render_attributes_in_pre<'b>(
1168-
&'b self,
1169-
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1170-
display_fn(move |f| {
1171-
let tcx = self.cx.borrow().tcx();
1172-
let v = render_attributes_in_pre(self.it, "", tcx);
1173-
write!(f, "{v}")
1174-
})
1175-
}
1176-
fn document<'b>(&'b self) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
1177-
display_fn(move |f| {
1178-
let mut cx = self.cx.borrow_mut();
1179-
let v = document(*cx, self.it, None, HeadingOffset::H2);
1180-
write!(f, "{v}")
1181-
})
1182-
}
11831201
fn document_field<'b>(
11841202
&'b self,
11851203
field: &'a clean::Item,

src/librustdoc/html/templates/item_union.html

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<pre class="rust item-decl"><code>
2-
{{ self.render_attributes_in_pre() | safe }}
2+
{{ self::item_template_render_attributes_in_pre(self.borrow()) | safe }}
33
{{ self.render_union() | safe }}
44
</code></pre>
5-
{{ self.document() | safe }}
5+
{{ self::item_template_document(self.borrow()) | safe }}
66
{% if self.fields_iter().peek().is_some() %}
77
<h2 id="fields" class="fields small-section-header">
88
Fields<a href="#fields" class="anchor">§</a>
@@ -19,5 +19,5 @@ <h2 id="fields" class="fields small-section-header">
1919
{{ self.document_field(field) | safe }}
2020
{% endfor %}
2121
{% endif %}
22-
{{ self.render_assoc_items() | safe }}
23-
{{ self.document_type_layout() | safe }}
22+
{{ self::item_template_render_assoc_items(self.borrow()) | safe }}
23+
{{ self::item_template_document_type_layout(self.borrow()) | safe }}

0 commit comments

Comments
 (0)