@@ -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
@@ -1131,55 +1180,25 @@ fn item_union(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean:
11311180 #[ derive( Template ) ]
11321181 #[ template( path = "item_union.html" ) ]
11331182 struct ItemUnion < ' a , ' cx > {
1134- cx : std :: cell :: RefCell < & ' a mut Context < ' cx > > ,
1183+ cx : RefCell < & ' a mut Context < ' cx > > ,
11351184 it : & ' a clean:: Item ,
11361185 s : & ' a clean:: Union ,
11371186 }
11381187
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 ( ) )
11591191 }
1192+ }
1193+
1194+ impl < ' a , ' cx : ' a > ItemUnion < ' a , ' cx > {
11601195 fn render_union < ' b > ( & ' b self ) -> impl fmt:: Display + Captures < ' a > + ' b + Captures < ' cx > {
11611196 display_fn ( move |f| {
11621197 let cx = self . cx . borrow_mut ( ) ;
11631198 let v = render_union ( self . it , Some ( & self . s . generics ) , & self . s . fields , * cx) ;
11641199 write ! ( f, "{v}" )
11651200 } )
11661201 }
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- }
11831202 fn document_field < ' b > (
11841203 & ' b self ,
11851204 field : & ' a clean:: Item ,
@@ -1219,7 +1238,7 @@ fn item_union(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean:
12191238 }
12201239 }
12211240
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 ( ) ;
12231242}
12241243
12251244fn print_tuple_struct_fields < ' a , ' cx : ' a > (
0 commit comments