@@ -1400,36 +1400,27 @@ fn format_tuple_struct(
1400
1400
Some ( result)
1401
1401
}
1402
1402
1403
- pub fn rewrite_type_alias (
1403
+ fn rewrite_type_prefix (
1404
1404
context : & RewriteContext ,
1405
1405
indent : Indent ,
1406
+ prefix : & str ,
1406
1407
ident : ast:: Ident ,
1407
- ty : & ast:: Ty ,
1408
1408
generics : & ast:: Generics ,
1409
- vis : & ast:: Visibility ,
1410
- span : Span ,
1411
1409
) -> Option < String > {
1412
1410
let mut result = String :: with_capacity ( 128 ) ;
1413
-
1414
- result. push_str ( & format_visibility ( context, vis) ) ;
1415
- result. push_str ( "type " ) ;
1411
+ result. push_str ( prefix) ;
1412
+ let ident_str = rewrite_ident ( context, ident) ;
1416
1413
1417
1414
// 2 = `= `
1418
- let g_shape = Shape :: indented ( indent, context. config )
1419
- . offset_left ( result. len ( ) ) ?
1420
- . sub_width ( 2 ) ?;
1421
- let g_span = mk_sp (
1422
- context. snippet_provider . span_after ( span, "type" ) ,
1423
- ty. span . lo ( ) ,
1424
- ) ;
1425
- let generics_str = rewrite_generics (
1426
- context,
1427
- rewrite_ident ( context, ident) ,
1428
- generics,
1429
- g_shape,
1430
- g_span,
1431
- ) ?;
1432
- result. push_str ( & generics_str) ;
1415
+ if generics. params . is_empty ( ) {
1416
+ result. push_str ( ident_str)
1417
+ } else {
1418
+ let g_shape = Shape :: indented ( indent, context. config )
1419
+ . offset_left ( result. len ( ) ) ?
1420
+ . sub_width ( 2 ) ?;
1421
+ let generics_str = rewrite_generics ( context, ident_str, generics, g_shape, generics. span ) ?;
1422
+ result. push_str ( & generics_str) ;
1423
+ }
1433
1424
1434
1425
let where_budget = context. budget ( last_line_width ( & result) ) ;
1435
1426
let option = WhereClauseOption :: snuggled ( & result) ;
@@ -1440,24 +1431,76 @@ pub fn rewrite_type_alias(
1440
1431
Shape :: legacy ( where_budget, indent) ,
1441
1432
Density :: Vertical ,
1442
1433
"=" ,
1443
- Some ( span . hi ( ) ) ,
1434
+ None ,
1444
1435
generics. span . hi ( ) ,
1445
1436
option,
1446
1437
false ,
1447
1438
) ?;
1448
1439
result. push_str ( & where_clause_str) ;
1449
- if where_clause_str. is_empty ( ) {
1450
- result. push_str ( " =" ) ;
1440
+
1441
+ Some ( result)
1442
+ }
1443
+
1444
+ fn rewrite_type_item < R : Rewrite > (
1445
+ context : & RewriteContext ,
1446
+ indent : Indent ,
1447
+ prefix : & str ,
1448
+ suffix : & str ,
1449
+ ident : ast:: Ident ,
1450
+ rhs : & R ,
1451
+ generics : & ast:: Generics ,
1452
+ vis : & ast:: Visibility ,
1453
+ ) -> Option < String > {
1454
+ let mut result = String :: with_capacity ( 128 ) ;
1455
+ result. push_str ( & rewrite_type_prefix (
1456
+ context,
1457
+ indent,
1458
+ & format ! ( "{}{} " , format_visibility( context, vis) , prefix) ,
1459
+ ident,
1460
+ generics,
1461
+ ) ?) ;
1462
+
1463
+ if generics. where_clause . predicates . is_empty ( ) {
1464
+ result. push_str ( suffix) ;
1451
1465
} else {
1452
- result. push_str ( & format ! (
1453
- "{}=" ,
1454
- indent. to_string_with_newline( context. config)
1455
- ) ) ;
1466
+ result. push_str ( & indent. to_string_with_newline ( context. config ) ) ;
1467
+ result. push_str ( suffix. trim_left ( ) ) ;
1456
1468
}
1457
1469
1458
1470
// 1 = ";"
1459
- let ty_shape = Shape :: indented ( indent, context. config ) . sub_width ( 1 ) ?;
1460
- rewrite_assign_rhs ( context, result, ty, ty_shape) . map ( |s| s + ";" )
1471
+ let rhs_shape = Shape :: indented ( indent, context. config ) . sub_width ( 1 ) ?;
1472
+ rewrite_assign_rhs ( context, result, rhs, rhs_shape) . map ( |s| s + ";" )
1473
+ }
1474
+
1475
+ pub fn rewrite_type_alias (
1476
+ context : & RewriteContext ,
1477
+ indent : Indent ,
1478
+ ident : ast:: Ident ,
1479
+ ty : & ast:: Ty ,
1480
+ generics : & ast:: Generics ,
1481
+ vis : & ast:: Visibility ,
1482
+ ) -> Option < String > {
1483
+ rewrite_type_item ( context, indent, "type" , " =" , ident, ty, generics, vis)
1484
+ }
1485
+
1486
+ pub fn rewrite_existential_type (
1487
+ context : & RewriteContext ,
1488
+ indent : Indent ,
1489
+ ident : ast:: Ident ,
1490
+ generic_bounds : & ast:: GenericBounds ,
1491
+ generics : & ast:: Generics ,
1492
+ vis : & ast:: Visibility ,
1493
+ ) -> Option < String > {
1494
+ rewrite_type_item (
1495
+ context,
1496
+ indent,
1497
+ "existential type" ,
1498
+ ":" ,
1499
+ ident,
1500
+ generic_bounds,
1501
+ generics,
1502
+ vis,
1503
+ )
1461
1504
}
1462
1505
1463
1506
fn type_annotation_spacing ( config : & Config ) -> ( & str , & str ) {
@@ -1706,6 +1749,16 @@ pub fn rewrite_associated_type(
1706
1749
}
1707
1750
}
1708
1751
1752
+ pub fn rewrite_existential_impl_type (
1753
+ context : & RewriteContext ,
1754
+ ident : ast:: Ident ,
1755
+ generic_bounds : & ast:: GenericBounds ,
1756
+ indent : Indent ,
1757
+ ) -> Option < String > {
1758
+ rewrite_associated_type ( ident, None , Some ( generic_bounds) , context, indent)
1759
+ . map ( |s| format ! ( "existential {}" , s) )
1760
+ }
1761
+
1709
1762
pub fn rewrite_associated_impl_type (
1710
1763
ident : ast:: Ident ,
1711
1764
defaultness : ast:: Defaultness ,
0 commit comments