@@ -1264,6 +1264,7 @@ impl std::fmt::Write for WriteCounter {
12641264}
12651265
12661266// Implements Display by emitting the given number of spaces.
1267+ #[ derive( Clone , Copy ) ]
12671268struct Indent ( usize ) ;
12681269
12691270impl Display for Indent {
@@ -1275,6 +1276,37 @@ impl Display for Indent {
12751276 }
12761277}
12771278
1279+ impl clean:: Parameter {
1280+ fn print ( & self , cx : & Context < ' _ > ) -> impl fmt:: Display {
1281+ fmt:: from_fn ( move |f| {
1282+ if let Some ( self_ty) = self . to_receiver ( ) {
1283+ match self_ty {
1284+ clean:: SelfTy => f. write_str ( "self" ) ,
1285+ clean:: BorrowedRef { lifetime, mutability, type_ : box clean:: SelfTy } => {
1286+ f. write_str ( if f. alternate ( ) { "&" } else { "&" } ) ?;
1287+ if let Some ( lt) = lifetime {
1288+ write ! ( f, "{lt} " , lt = lt. print( ) ) ?;
1289+ }
1290+ write ! ( f, "{mutability}self" , mutability = mutability. print_with_space( ) )
1291+ }
1292+ _ => {
1293+ f. write_str ( "self: " ) ?;
1294+ self_ty. print ( cx) . fmt ( f)
1295+ }
1296+ }
1297+ } else {
1298+ if self . is_const {
1299+ write ! ( f, "const " ) ?;
1300+ }
1301+ if let Some ( name) = self . name {
1302+ write ! ( f, "{name}: " ) ?;
1303+ }
1304+ self . type_ . print ( cx) . fmt ( f)
1305+ }
1306+ } )
1307+ }
1308+ }
1309+
12781310impl clean:: FnDecl {
12791311 pub ( crate ) fn print ( & self , cx : & Context < ' _ > ) -> impl Display {
12801312 fmt:: from_fn ( move |f| {
@@ -1333,63 +1365,42 @@ impl clean::FnDecl {
13331365 f : & mut fmt:: Formatter < ' _ > ,
13341366 cx : & Context < ' _ > ,
13351367 ) -> fmt:: Result {
1336- let amp = if f . alternate ( ) { "&" } else { "&" } ;
1368+ f . write_char ( '(' ) ? ;
13371369
1338- write ! ( f, "(" ) ?;
1339- if let Some ( n) = line_wrapping_indent
1340- && !self . inputs . is_empty ( )
1341- {
1342- write ! ( f, "\n {}" , Indent ( n + 4 ) ) ?;
1343- }
1370+ if !self . inputs . is_empty ( ) {
1371+ let line_wrapping_indent = line_wrapping_indent. map ( |n| Indent ( n + 4 ) ) ;
13441372
1345- let last_input_index = self . inputs . len ( ) . checked_sub ( 1 ) ;
1346- for ( i, param) in self . inputs . iter ( ) . enumerate ( ) {
1347- if let Some ( selfty) = param. to_receiver ( ) {
1348- match selfty {
1349- clean:: SelfTy => {
1350- write ! ( f, "self" ) ?;
1351- }
1352- clean:: BorrowedRef { lifetime, mutability, type_ : box clean:: SelfTy } => {
1353- write ! ( f, "{amp}" ) ?;
1354- if let Some ( lt) = lifetime {
1355- write ! ( f, "{lt} " , lt = lt. print( ) ) ?;
1356- }
1357- write ! ( f, "{mutability}self" , mutability = mutability. print_with_space( ) ) ?;
1358- }
1359- _ => {
1360- write ! ( f, "self: " ) ?;
1361- selfty. print ( cx) . fmt ( f) ?;
1362- }
1363- }
1364- } else {
1365- if param. is_const {
1366- write ! ( f, "const " ) ?;
1367- }
1368- if let Some ( name) = param. name {
1369- write ! ( f, "{name}: " ) ?;
1373+ if let Some ( indent) = line_wrapping_indent {
1374+ write ! ( f, "\n {indent}" ) ?;
1375+ }
1376+
1377+ let sep = fmt:: from_fn ( |f| {
1378+ if let Some ( indent) = line_wrapping_indent {
1379+ write ! ( f, ",\n {indent}" )
1380+ } else {
1381+ f. write_str ( ", " )
13701382 }
1371- param. type_ . print ( cx) . fmt ( f) ?;
1383+ } ) ;
1384+
1385+ self . inputs . iter ( ) . map ( |param| param. print ( cx) ) . joined ( sep, f) ?;
1386+
1387+ if line_wrapping_indent. is_some ( ) {
1388+ writeln ! ( f, "," ) ?
13721389 }
1373- match ( line_wrapping_indent , last_input_index ) {
1374- ( _ , None ) => ( ) ,
1375- ( None , Some ( last_i ) ) if i != last_i => write ! ( f , ", " ) ? ,
1376- ( None , Some ( _ ) ) => ( ) ,
1377- ( Some ( n ) , Some ( last_i ) ) if i != last_i => write ! ( f, ", \n {}" , Indent ( n + 4 ) ) ?,
1378- ( Some ( _ ) , Some ( _ ) ) => writeln ! ( f , "," ) ? ,
1390+
1391+ if self . c_variadic {
1392+ match line_wrapping_indent {
1393+ None => write ! ( f , ", ..." ) ? ,
1394+ Some ( indent ) => writeln ! ( f, "{indent}..." ) ?,
1395+ } ;
13791396 }
13801397 }
13811398
1382- if self . c_variadic {
1383- match line_wrapping_indent {
1384- None => write ! ( f, ", ..." ) ?,
1385- Some ( n) => writeln ! ( f, "{}..." , Indent ( n + 4 ) ) ?,
1386- } ;
1399+ if let Some ( n) = line_wrapping_indent {
1400+ write ! ( f, "{}" , Indent ( n) ) ?
13871401 }
13881402
1389- match line_wrapping_indent {
1390- None => write ! ( f, ")" ) ?,
1391- Some ( n) => write ! ( f, "{})" , Indent ( n) ) ?,
1392- } ;
1403+ f. write_char ( ')' ) ?;
13931404
13941405 self . print_output ( cx) . fmt ( f)
13951406 }
0 commit comments