@@ -9,6 +9,8 @@ use rustc_middle::middle::stability;
9
9
use rustc_middle:: ty:: { self , TyCtxt } ;
10
10
use rustc_span:: hygiene:: MacroKind ;
11
11
use rustc_span:: symbol:: { kw, sym, Symbol } ;
12
+ use std:: borrow:: Borrow ;
13
+ use std:: cell:: { RefCell , RefMut } ;
12
14
use std:: cmp:: Ordering ;
13
15
use std:: fmt;
14
16
use std:: rc:: Rc ;
@@ -216,6 +218,53 @@ fn toggle_close(mut w: impl fmt::Write) {
216
218
w. write_str ( "</details>" ) . unwrap ( ) ;
217
219
}
218
220
221
+ trait ItemTemplate < ' a , ' cx : ' a > : askama:: Template + fmt:: Display {
222
+ fn item_and_mut_cx ( & self ) -> ( & ' a clean:: Item , RefMut < ' _ , & ' a mut Context < ' cx > > ) ;
223
+ }
224
+
225
+ fn item_template_document < ' a : ' b , ' b , ' cx : ' a > (
226
+ templ : & ' b impl ItemTemplate < ' a , ' cx > ,
227
+ ) -> impl fmt:: Display + Captures < ' a > + ' b + Captures < ' cx > {
228
+ display_fn ( move |f| {
229
+ let ( item, mut cx) = templ. item_and_mut_cx ( ) ;
230
+ let v = document ( * cx, item, None , HeadingOffset :: H2 ) ;
231
+ write ! ( f, "{v}" )
232
+ } )
233
+ }
234
+
235
+ fn item_template_document_type_layout < ' a : ' b , ' b , ' cx : ' a > (
236
+ templ : & ' b impl ItemTemplate < ' a , ' cx > ,
237
+ ) -> impl fmt:: Display + Captures < ' a > + ' b + Captures < ' cx > {
238
+ display_fn ( move |f| {
239
+ let ( item, cx) = templ. item_and_mut_cx ( ) ;
240
+ let def_id = item. item_id . expect_def_id ( ) ;
241
+ let v = document_type_layout ( * cx, def_id) ;
242
+ write ! ( f, "{v}" )
243
+ } )
244
+ }
245
+
246
+ fn item_template_render_attributes_in_pre < ' a : ' b , ' b , ' cx : ' a > (
247
+ templ : & ' b impl ItemTemplate < ' a , ' cx > ,
248
+ ) -> impl fmt:: Display + Captures < ' a > + ' b + Captures < ' cx > {
249
+ display_fn ( move |f| {
250
+ let ( item, cx) = templ. item_and_mut_cx ( ) ;
251
+ let tcx = cx. tcx ( ) ;
252
+ let v = render_attributes_in_pre ( item, "" , tcx) ;
253
+ write ! ( f, "{v}" )
254
+ } )
255
+ }
256
+
257
+ fn item_template_render_assoc_items < ' a : ' b , ' b , ' cx : ' a > (
258
+ templ : & ' b impl ItemTemplate < ' a , ' cx > ,
259
+ ) -> impl fmt:: Display + Captures < ' a > + ' b + Captures < ' cx > {
260
+ display_fn ( move |f| {
261
+ let ( item, mut cx) = templ. item_and_mut_cx ( ) ;
262
+ let def_id = item. item_id . expect_def_id ( ) ;
263
+ let v = render_assoc_items ( * cx, item, def_id, AssocItemRender :: All ) ;
264
+ write ! ( f, "{v}" )
265
+ } )
266
+ }
267
+
219
268
fn item_module ( w : & mut Buffer , cx : & mut Context < ' _ > , item : & clean:: Item , items : & [ clean:: Item ] ) {
220
269
write ! ( w, "{}" , document( cx, item, None , HeadingOffset :: H2 ) ) ;
221
270
@@ -1131,55 +1180,25 @@ fn item_union(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean:
1131
1180
#[ derive( Template ) ]
1132
1181
#[ template( path = "item_union.html" ) ]
1133
1182
struct ItemUnion < ' a , ' cx > {
1134
- cx : std :: cell :: RefCell < & ' a mut Context < ' cx > > ,
1183
+ cx : RefCell < & ' a mut Context < ' cx > > ,
1135
1184
it : & ' a clean:: Item ,
1136
1185
s : & ' a clean:: Union ,
1137
1186
}
1138
1187
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
- } )
1188
+ impl < ' a , ' cx : ' a > ItemTemplate < ' a , ' cx > for ItemUnion < ' a , ' cx > {
1189
+ fn item_and_mut_cx ( & self ) -> ( & ' a clean:: Item , RefMut < ' _ , & ' a mut Context < ' cx > > ) {
1190
+ ( self . it , self . cx . borrow_mut ( ) )
1159
1191
}
1192
+ }
1193
+
1194
+ impl < ' a , ' cx : ' a > ItemUnion < ' a , ' cx > {
1160
1195
fn render_union < ' b > ( & ' b self ) -> impl fmt:: Display + Captures < ' a > + ' b + Captures < ' cx > {
1161
1196
display_fn ( move |f| {
1162
1197
let cx = self . cx . borrow_mut ( ) ;
1163
1198
let v = render_union ( self . it , Some ( & self . s . generics ) , & self . s . fields , * cx) ;
1164
1199
write ! ( f, "{v}" )
1165
1200
} )
1166
1201
}
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
- }
1183
1202
fn document_field < ' b > (
1184
1203
& ' b self ,
1185
1204
field : & ' a clean:: Item ,
@@ -1219,7 +1238,7 @@ fn item_union(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean:
1219
1238
}
1220
1239
}
1221
1240
1222
- ItemUnion { cx : std :: cell :: RefCell :: new ( cx) , it, s } . render_into ( w) . unwrap ( ) ;
1241
+ ItemUnion { cx : RefCell :: new ( cx) , it, s } . render_into ( w) . unwrap ( ) ;
1223
1242
}
1224
1243
1225
1244
fn print_tuple_struct_fields < ' a , ' cx : ' a > (
0 commit comments