@@ -1504,44 +1504,44 @@ fn format_tuple_struct(
1504
1504
Some ( result)
1505
1505
}
1506
1506
1507
- fn rewrite_type_prefix (
1507
+ fn rewrite_type < R : Rewrite > (
1508
1508
context : & RewriteContext < ' _ > ,
1509
1509
indent : Indent ,
1510
- prefix : & str ,
1511
1510
ident : symbol:: Ident ,
1511
+ vis : & ast:: Visibility ,
1512
1512
generics : & ast:: Generics ,
1513
1513
generic_bounds_opt : Option < & ast:: GenericBounds > ,
1514
+ rhs : Option < & R > ,
1514
1515
) -> Option < String > {
1515
1516
let mut result = String :: with_capacity ( 128 ) ;
1516
- result. push_str ( prefix ) ;
1517
+ result. push_str ( & format ! ( "{}type " , format_visibility ( context , vis ) ) ) ;
1517
1518
let ident_str = rewrite_ident ( context, ident) ;
1518
1519
1519
- // 2 = `= `
1520
1520
if generics. params . is_empty ( ) {
1521
1521
result. push_str ( ident_str)
1522
1522
} else {
1523
+ // 2 = `= `
1523
1524
let g_shape = Shape :: indented ( indent, context. config )
1524
1525
. offset_left ( result. len ( ) ) ?
1525
1526
. sub_width ( 2 ) ?;
1526
1527
let generics_str = rewrite_generics ( context, ident_str, generics, g_shape) ?;
1527
1528
result. push_str ( & generics_str) ;
1528
1529
}
1529
1530
1530
- let type_bounds_str = if let Some ( bounds) = generic_bounds_opt {
1531
- if bounds. is_empty ( ) {
1532
- String :: new ( )
1533
- } else {
1531
+ if let Some ( bounds) = generic_bounds_opt {
1532
+ if !bounds. is_empty ( ) {
1534
1533
// 2 = `: `
1535
1534
let shape = Shape :: indented ( indent, context. config ) . offset_left ( result. len ( ) + 2 ) ?;
1536
- bounds. rewrite ( context, shape) . map ( |s| format ! ( ": {}" , s) ) ?
1535
+ let type_bounds = bounds. rewrite ( context, shape) . map ( |s| format ! ( ": {}" , s) ) ?;
1536
+ result. push_str ( & type_bounds) ;
1537
1537
}
1538
- } else {
1539
- String :: new ( )
1540
- } ;
1541
- result. push_str ( & type_bounds_str) ;
1538
+ }
1542
1539
1543
1540
let where_budget = context. budget ( last_line_width ( & result) ) ;
1544
- let option = WhereClauseOption :: snuggled ( & result) ;
1541
+ let mut option = WhereClauseOption :: snuggled ( & result) ;
1542
+ if rhs. is_none ( ) {
1543
+ option. suppress_comma ( ) ;
1544
+ }
1545
1545
let where_clause_str = rewrite_where_clause (
1546
1546
context,
1547
1547
& generics. where_clause ,
@@ -1555,40 +1555,22 @@ fn rewrite_type_prefix(
1555
1555
) ?;
1556
1556
result. push_str ( & where_clause_str) ;
1557
1557
1558
- Some ( result)
1559
- }
1560
-
1561
- fn rewrite_type_item < R : Rewrite > (
1562
- context : & RewriteContext < ' _ > ,
1563
- indent : Indent ,
1564
- prefix : & str ,
1565
- suffix : & str ,
1566
- ident : symbol:: Ident ,
1567
- rhs : & R ,
1568
- generics : & ast:: Generics ,
1569
- generic_bounds_opt : Option < & ast:: GenericBounds > ,
1570
- vis : & ast:: Visibility ,
1571
- ) -> Option < String > {
1572
- let mut result = String :: with_capacity ( 128 ) ;
1573
- result. push_str ( & rewrite_type_prefix (
1574
- context,
1575
- indent,
1576
- & format ! ( "{}{} " , format_visibility( context, vis) , prefix) ,
1577
- ident,
1578
- generics,
1579
- generic_bounds_opt,
1580
- ) ?) ;
1558
+ if let Some ( ty) = rhs {
1559
+ // If there's a where clause, add a newline before the assignment. Otherwise just add a
1560
+ // space.
1561
+ if !generics. where_clause . predicates . is_empty ( ) {
1562
+ result. push_str ( & indent. to_string_with_newline ( context. config ) ) ;
1563
+ } else {
1564
+ result. push ( ' ' ) ;
1565
+ }
1566
+ let lhs = format ! ( "{}=" , result) ;
1581
1567
1582
- if generics. where_clause . predicates . is_empty ( ) {
1583
- result. push_str ( suffix) ;
1568
+ // 1 = `;`
1569
+ let shape = Shape :: indented ( indent, context. config ) . sub_width ( 1 ) ?;
1570
+ rewrite_assign_rhs ( context, lhs, & * ty, shape) . map ( |s| s + ";" )
1584
1571
} else {
1585
- result. push_str ( & indent. to_string_with_newline ( context. config ) ) ;
1586
- result. push_str ( suffix. trim_start ( ) ) ;
1572
+ Some ( format ! ( "{};" , result) )
1587
1573
}
1588
-
1589
- // 1 = ";"
1590
- let rhs_shape = Shape :: indented ( indent, context. config ) . sub_width ( 1 ) ?;
1591
- rewrite_assign_rhs ( context, result, rhs, rhs_shape) . map ( |s| s + ";" )
1592
1574
}
1593
1575
1594
1576
pub ( crate ) fn rewrite_opaque_type (
@@ -1600,16 +1582,14 @@ pub(crate) fn rewrite_opaque_type(
1600
1582
vis : & ast:: Visibility ,
1601
1583
) -> Option < String > {
1602
1584
let opaque_type_bounds = OpaqueTypeBounds { generic_bounds } ;
1603
- rewrite_type_item (
1585
+ rewrite_type (
1604
1586
context,
1605
1587
indent,
1606
- "type" ,
1607
- " =" ,
1608
1588
ident,
1609
- & opaque_type_bounds ,
1589
+ vis ,
1610
1590
generics,
1611
1591
Some ( generic_bounds) ,
1612
- vis ,
1592
+ Some ( & opaque_type_bounds ) ,
1613
1593
)
1614
1594
}
1615
1595
@@ -1839,34 +1819,15 @@ pub(crate) fn rewrite_type_alias(
1839
1819
indent : Indent ,
1840
1820
vis : & ast:: Visibility ,
1841
1821
) -> Option < String > {
1842
- let mut prefix = rewrite_type_prefix (
1822
+ rewrite_type (
1843
1823
context,
1844
1824
indent,
1845
- & format ! ( "{}type " , format_visibility( context, vis) ) ,
1846
1825
ident,
1826
+ vis,
1847
1827
generics,
1848
1828
generic_bounds_opt,
1849
- ) ?;
1850
-
1851
- if let Some ( ty) = ty_opt {
1852
- // 1 = `;`
1853
- let shape = Shape :: indented ( indent, context. config ) . sub_width ( 1 ) ?;
1854
-
1855
- // If there's a where clause, add a newline before the assignment. Otherwise just add a
1856
- // space.
1857
- if !generics. where_clause . predicates . is_empty ( ) {
1858
- prefix. push_str ( & indent. to_string_with_newline ( context. config ) ) ;
1859
- } else {
1860
- prefix. push ( ' ' ) ;
1861
- }
1862
- let lhs = format ! ( "{}=" , prefix) ;
1863
- rewrite_assign_rhs ( context, lhs, & * * ty, shape) . map ( |s| s + ";" )
1864
- } else {
1865
- if !generics. where_clause . predicates . is_empty ( ) {
1866
- prefix. push_str ( & indent. to_string_with_newline ( context. config ) ) ;
1867
- }
1868
- Some ( format ! ( "{};" , prefix) )
1869
- }
1829
+ ty_opt,
1830
+ )
1870
1831
}
1871
1832
1872
1833
struct OpaqueType < ' a > {
0 commit comments