@@ -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
@@ -356,18 +405,18 @@ fn item_module(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Item, items:
356
405
357
406
clean:: ImportItem ( ref import) => {
358
407
let stab_tags = if let Some ( import_def_id) = import. source . did {
359
- let ast_attrs = cx . tcx ( ) . get_attrs_unchecked ( import_def_id) ;
408
+ let ast_attrs = tcx. get_attrs_unchecked ( import_def_id) ;
360
409
let import_attrs = Box :: new ( clean:: Attributes :: from_ast ( ast_attrs) ) ;
361
410
362
411
// Just need an item with the correct def_id and attrs
363
412
let import_item = clean:: Item {
364
413
item_id : import_def_id. into ( ) ,
365
414
attrs : import_attrs,
366
- cfg : ast_attrs. cfg ( cx . tcx ( ) , & cx. cache ( ) . hidden_cfg ) ,
415
+ cfg : ast_attrs. cfg ( tcx, & cx. cache ( ) . hidden_cfg ) ,
367
416
..myitem. clone ( )
368
417
} ;
369
418
370
- let stab_tags = Some ( extra_info_tags ( & import_item, item, cx . tcx ( ) ) . to_string ( ) ) ;
419
+ let stab_tags = Some ( extra_info_tags ( & import_item, item, tcx) . to_string ( ) ) ;
371
420
stab_tags
372
421
} else {
373
422
None
@@ -405,8 +454,7 @@ fn item_module(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Item, items:
405
454
406
455
let unsafety_flag = match * myitem. kind {
407
456
clean:: FunctionItem ( _) | clean:: ForeignFunctionItem ( _)
408
- if myitem. fn_header ( cx. tcx ( ) ) . unwrap ( ) . unsafety
409
- == hir:: Unsafety :: Unsafe =>
457
+ if myitem. fn_header ( tcx) . unwrap ( ) . unsafety == hir:: Unsafety :: Unsafe =>
410
458
{
411
459
"<sup title=\" unsafe function\" >⚠</sup>"
412
460
}
@@ -439,7 +487,7 @@ fn item_module(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Item, items:
439
487
{docs_before}{docs}{docs_after}",
440
488
name = myitem. name. unwrap( ) ,
441
489
visibility_emoji = visibility_emoji,
442
- stab_tags = extra_info_tags( myitem, item, cx . tcx( ) ) ,
490
+ stab_tags = extra_info_tags( myitem, item, tcx) ,
443
491
class = myitem. type_( ) ,
444
492
unsafety_flag = unsafety_flag,
445
493
href = item_path( myitem. type_( ) , myitem. name. unwrap( ) . as_str( ) ) ,
@@ -886,7 +934,7 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
886
934
write_small_section_header ( w, "foreign-impls" , "Implementations on Foreign Types" , "" ) ;
887
935
888
936
for implementor in foreign {
889
- let provided_methods = implementor. inner_impl ( ) . provided_trait_methods ( cx . tcx ( ) ) ;
937
+ let provided_methods = implementor. inner_impl ( ) . provided_trait_methods ( tcx) ;
890
938
let assoc_link =
891
939
AssocItemLink :: GotoSource ( implementor. impl_item . item_id , & provided_methods) ;
892
940
render_impl (
@@ -919,7 +967,7 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
919
967
}
920
968
w. write_str ( "</div>" ) ;
921
969
922
- if t. is_auto ( cx . tcx ( ) ) {
970
+ if t. is_auto ( tcx) {
923
971
write_small_section_header (
924
972
w,
925
973
"synthetic-implementors" ,
@@ -948,7 +996,7 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
948
996
"<div id=\" implementors-list\" ></div>" ,
949
997
) ;
950
998
951
- if t. is_auto ( cx . tcx ( ) ) {
999
+ if t. is_auto ( tcx) {
952
1000
write_small_section_header (
953
1001
w,
954
1002
"synthetic-implementors" ,
@@ -1131,55 +1179,25 @@ fn item_union(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean:
1131
1179
#[ derive( Template ) ]
1132
1180
#[ template( path = "item_union.html" ) ]
1133
1181
struct ItemUnion < ' a , ' cx > {
1134
- cx : std :: cell :: RefCell < & ' a mut Context < ' cx > > ,
1182
+ cx : RefCell < & ' a mut Context < ' cx > > ,
1135
1183
it : & ' a clean:: Item ,
1136
1184
s : & ' a clean:: Union ,
1137
1185
}
1138
1186
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 item_and_mut_cx ( & self ) -> ( & ' a clean:: Item , RefMut < ' _ , & ' a mut Context < ' cx > > ) {
1189
+ ( self . it , self . cx . borrow_mut ( ) )
1159
1190
}
1191
+ }
1192
+
1193
+ impl < ' a , ' cx : ' a > ItemUnion < ' a , ' cx > {
1160
1194
fn render_union < ' b > ( & ' b self ) -> impl fmt:: Display + Captures < ' a > + ' b + Captures < ' cx > {
1161
1195
display_fn ( move |f| {
1162
1196
let cx = self . cx . borrow_mut ( ) ;
1163
1197
let v = render_union ( self . it , Some ( & self . s . generics ) , & self . s . fields , * cx) ;
1164
1198
write ! ( f, "{v}" )
1165
1199
} )
1166
1200
}
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
1201
fn document_field < ' b > (
1184
1202
& ' b self ,
1185
1203
field : & ' a clean:: Item ,
@@ -1219,7 +1237,7 @@ fn item_union(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean:
1219
1237
}
1220
1238
}
1221
1239
1222
- ItemUnion { cx : std :: cell :: RefCell :: new ( cx) , it, s } . render_into ( w) . unwrap ( ) ;
1240
+ ItemUnion { cx : RefCell :: new ( cx) , it, s } . render_into ( w) . unwrap ( ) ;
1223
1241
}
1224
1242
1225
1243
fn print_tuple_struct_fields < ' a , ' cx : ' a > (
0 commit comments