@@ -809,7 +809,7 @@ fn assoc_method(
809
809
) {
810
810
let tcx = cx. tcx ( ) ;
811
811
let header = meth. fn_header ( tcx) . expect ( "Trying to get header from a non-function item" ) ;
812
- let name = meth. name . as_ref ( ) . unwrap ( ) ;
812
+ let name = meth. name . as_ref ( ) . unwrap ( ) . as_str ( ) ;
813
813
let vis = visibility_print_with_space ( meth. visibility ( tcx) , meth. item_id , cx) . to_string ( ) ;
814
814
// FIXME: Once https://github.com/rust-lang/rust/issues/67792 is implemented, we can remove
815
815
// this condition.
@@ -825,48 +825,53 @@ fn assoc_method(
825
825
let abi = print_abi_with_space ( header. abi ) . to_string ( ) ;
826
826
let href = assoc_href_attr ( meth, link, cx) ;
827
827
828
- // NOTE: `{:#}` does not print HTML formatting, `{}` does. So `g.print` can't be reused between the length calculation and `write!`.
829
- let generics_len = format ! ( "{:#}" , g. print( cx) ) . len ( ) ;
830
- let mut header_len = "fn " . len ( )
831
- + vis. len ( )
832
- + constness. len ( )
833
- + asyncness. len ( )
834
- + unsafety. len ( )
835
- + defaultness. len ( )
836
- + abi. len ( )
837
- + name. as_str ( ) . len ( )
838
- + generics_len;
839
-
840
- let notable_traits = d. output . as_return ( ) . and_then ( |output| notable_traits_button ( output, cx) ) ;
828
+ let notable_traits = d
829
+ . output
830
+ . as_return ( )
831
+ . and_then ( |output| notable_traits_button ( output, cx) )
832
+ . unwrap_or_default ( ) ;
841
833
842
834
let ( indent, indent_str, end_newline) = if parent == ItemType :: Trait {
843
- header_len += 4 ;
844
835
let indent_str = " " ;
845
836
render_attributes_in_pre ( w, meth, indent_str) ;
846
837
( 4 , indent_str, Ending :: NoNewline )
847
838
} else {
848
839
render_attributes_in_code ( w, meth) ;
849
840
( 0 , "" , Ending :: Newline )
850
841
} ;
851
- w. reserve ( header_len + "<a href=\" \" class=\" fn\" >{" . len ( ) + "</a>" . len ( ) ) ;
852
- write ! (
853
- w,
854
- "{indent}{vis}{constness}{asyncness}{unsafety}{defaultness}{abi}fn <a{href} class=\" fn\" >{name}</a>\
855
- {generics}{decl}{notable_traits}{where_clause}",
856
- indent = indent_str,
857
- vis = vis,
858
- constness = constness,
859
- asyncness = asyncness,
860
- unsafety = unsafety,
861
- defaultness = defaultness,
862
- abi = abi,
863
- href = href,
864
- name = name,
865
- generics = g. print( cx) ,
866
- decl = d. full_print( header_len, indent, cx) ,
867
- notable_traits = notable_traits. unwrap_or_default( ) ,
868
- where_clause = print_where_clause( g, cx, indent, end_newline) ,
869
- ) ;
842
+
843
+ let fn_header = FunctionHeader {
844
+ indent_str,
845
+ vis,
846
+ constness,
847
+ asyncness,
848
+ unsafety,
849
+ defaultness,
850
+ abi,
851
+ href,
852
+ name,
853
+ generics : g. print ( cx) . to_string ( ) ,
854
+ } ;
855
+
856
+ let decl = d. full_print ( & fn_header, indent, cx) ;
857
+ let where_clause = print_where_clause ( g, cx, indent, end_newline) ;
858
+
859
+ write ! ( w, "{fn_header}{decl}{notable_traits}{where_clause}" ) ;
860
+ }
861
+
862
+ #[ derive( Template ) ]
863
+ #[ template( path = "function_header.html" ) ]
864
+ struct FunctionHeader < ' a > {
865
+ indent_str : & ' static str ,
866
+ vis : String ,
867
+ constness : & ' a str ,
868
+ asyncness : & ' a str ,
869
+ unsafety : & ' a str ,
870
+ defaultness : & ' a str ,
871
+ abi : String ,
872
+ href : String ,
873
+ name : & ' a str ,
874
+ generics : String ,
870
875
}
871
876
872
877
/// Writes a span containing the versions at which an item became stable and/or const-stable. For
0 commit comments