@@ -9,6 +9,8 @@ use rustc_middle::middle::stability;
99use rustc_middle:: ty:: { self , TyCtxt } ;
1010use rustc_span:: hygiene:: MacroKind ;
1111use rustc_span:: symbol:: { kw, sym, Symbol } ;
12+ use std:: borrow:: Borrow ;
13+ use std:: cell:: { RefCell , RefMut } ;
1214use std:: cmp:: Ordering ;
1315use std:: fmt;
1416use std:: rc:: Rc ;
@@ -216,6 +218,53 @@ fn toggle_close(mut w: impl fmt::Write) {
216218 w. write_str ( "</details>" ) . unwrap ( ) ;
217219}
218220
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+
219268fn item_module ( w : & mut Buffer , cx : & mut Context < ' _ > , item : & clean:: Item , items : & [ clean:: Item ] ) {
220269 write ! ( w, "{}" , document( cx, item, None , HeadingOffset :: H2 ) ) ;
221270
@@ -356,18 +405,18 @@ fn item_module(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Item, items:
356405
357406 clean:: ImportItem ( ref import) => {
358407 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) ;
360409 let import_attrs = Box :: new ( clean:: Attributes :: from_ast ( ast_attrs) ) ;
361410
362411 // Just need an item with the correct def_id and attrs
363412 let import_item = clean:: Item {
364413 item_id : import_def_id. into ( ) ,
365414 attrs : import_attrs,
366- cfg : ast_attrs. cfg ( cx . tcx ( ) , & cx. cache ( ) . hidden_cfg ) ,
415+ cfg : ast_attrs. cfg ( tcx, & cx. cache ( ) . hidden_cfg ) ,
367416 ..myitem. clone ( )
368417 } ;
369418
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 ( ) ) ;
371420 stab_tags
372421 } else {
373422 None
@@ -405,8 +454,7 @@ fn item_module(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Item, items:
405454
406455 let unsafety_flag = match * myitem. kind {
407456 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 =>
410458 {
411459 "<sup title=\" unsafe function\" >⚠</sup>"
412460 }
@@ -439,7 +487,7 @@ fn item_module(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Item, items:
439487 {docs_before}{docs}{docs_after}",
440488 name = myitem. name. unwrap( ) ,
441489 visibility_emoji = visibility_emoji,
442- stab_tags = extra_info_tags( myitem, item, cx . tcx( ) ) ,
490+ stab_tags = extra_info_tags( myitem, item, tcx) ,
443491 class = myitem. type_( ) ,
444492 unsafety_flag = unsafety_flag,
445493 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:
886934 write_small_section_header ( w, "foreign-impls" , "Implementations on Foreign Types" , "" ) ;
887935
888936 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) ;
890938 let assoc_link =
891939 AssocItemLink :: GotoSource ( implementor. impl_item . item_id , & provided_methods) ;
892940 render_impl (
@@ -919,7 +967,7 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
919967 }
920968 w. write_str ( "</div>" ) ;
921969
922- if t. is_auto ( cx . tcx ( ) ) {
970+ if t. is_auto ( tcx) {
923971 write_small_section_header (
924972 w,
925973 "synthetic-implementors" ,
@@ -948,7 +996,7 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
948996 "<div id=\" implementors-list\" ></div>" ,
949997 ) ;
950998
951- if t. is_auto ( cx . tcx ( ) ) {
999+ if t. is_auto ( tcx) {
9521000 write_small_section_header (
9531001 w,
9541002 "synthetic-implementors" ,
@@ -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 item_and_mut_cx ( & 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 ,
@@ -1219,7 +1237,7 @@ fn item_union(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean:
12191237 }
12201238 }
12211239
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 ( ) ;
12231241}
12241242
12251243fn print_tuple_struct_fields < ' a , ' cx : ' a > (
0 commit comments