@@ -1556,44 +1556,44 @@ fn format_tuple_struct(
1556
1556
Some ( result)
1557
1557
}
1558
1558
1559
- fn rewrite_type_prefix (
1559
+ fn rewrite_type < R : Rewrite > (
1560
1560
context : & RewriteContext < ' _ > ,
1561
1561
indent : Indent ,
1562
- prefix : & str ,
1563
1562
ident : ast:: Ident ,
1563
+ vis : & ast:: Visibility ,
1564
1564
generics : & ast:: Generics ,
1565
1565
generic_bounds_opt : Option < & ast:: GenericBounds > ,
1566
+ rhs : Option < & R > ,
1566
1567
) -> Option < String > {
1567
1568
let mut result = String :: with_capacity ( 128 ) ;
1568
- result. push_str ( prefix ) ;
1569
+ result. push_str ( & format ! ( "{}type " , format_visibility ( context , vis ) ) ) ;
1569
1570
let ident_str = rewrite_ident ( context, ident) ;
1570
1571
1571
- // 2 = `= `
1572
1572
if generics. params . is_empty ( ) {
1573
1573
result. push_str ( ident_str)
1574
1574
} else {
1575
+ // 2 = `= `
1575
1576
let g_shape = Shape :: indented ( indent, context. config )
1576
1577
. offset_left ( result. len ( ) ) ?
1577
1578
. sub_width ( 2 ) ?;
1578
1579
let generics_str = rewrite_generics ( context, ident_str, generics, g_shape) ?;
1579
1580
result. push_str ( & generics_str) ;
1580
1581
}
1581
1582
1582
- let type_bounds_str = if let Some ( bounds) = generic_bounds_opt {
1583
- if bounds. is_empty ( ) {
1584
- String :: new ( )
1585
- } else {
1583
+ if let Some ( bounds) = generic_bounds_opt {
1584
+ if !bounds. is_empty ( ) {
1586
1585
// 2 = `: `
1587
1586
let shape = Shape :: indented ( indent, context. config ) . offset_left ( result. len ( ) + 2 ) ?;
1588
- bounds. rewrite ( context, shape) . map ( |s| format ! ( ": {}" , s) ) ?
1587
+ let type_bounds = bounds. rewrite ( context, shape) . map ( |s| format ! ( ": {}" , s) ) ?;
1588
+ result. push_str ( & type_bounds) ;
1589
1589
}
1590
- } else {
1591
- String :: new ( )
1592
- } ;
1593
- result. push_str ( & type_bounds_str) ;
1590
+ }
1594
1591
1595
1592
let where_budget = context. budget ( last_line_width ( & result) ) ;
1596
- let option = WhereClauseOption :: snuggled ( & result) ;
1593
+ let mut option = WhereClauseOption :: snuggled ( & result) ;
1594
+ if rhs. is_none ( ) {
1595
+ option. suppress_comma ( ) ;
1596
+ }
1597
1597
let where_clause_str = rewrite_where_clause (
1598
1598
context,
1599
1599
& generics. where_clause ,
@@ -1607,40 +1607,22 @@ fn rewrite_type_prefix(
1607
1607
) ?;
1608
1608
result. push_str ( & where_clause_str) ;
1609
1609
1610
- Some ( result)
1611
- }
1612
-
1613
- fn rewrite_type_item < R : Rewrite > (
1614
- context : & RewriteContext < ' _ > ,
1615
- indent : Indent ,
1616
- prefix : & str ,
1617
- suffix : & str ,
1618
- ident : ast:: Ident ,
1619
- rhs : & R ,
1620
- generics : & ast:: Generics ,
1621
- generic_bounds_opt : Option < & ast:: GenericBounds > ,
1622
- vis : & ast:: Visibility ,
1623
- ) -> Option < String > {
1624
- let mut result = String :: with_capacity ( 128 ) ;
1625
- result. push_str ( & rewrite_type_prefix (
1626
- context,
1627
- indent,
1628
- & format ! ( "{}{} " , format_visibility( context, vis) , prefix) ,
1629
- ident,
1630
- generics,
1631
- generic_bounds_opt,
1632
- ) ?) ;
1610
+ if let Some ( ty) = rhs {
1611
+ // If there's a where clause, add a newline before the assignment. Otherwise just add a
1612
+ // space.
1613
+ if !generics. where_clause . predicates . is_empty ( ) {
1614
+ result. push_str ( & indent. to_string_with_newline ( context. config ) ) ;
1615
+ } else {
1616
+ result. push ( ' ' ) ;
1617
+ }
1618
+ let lhs = format ! ( "{}=" , result) ;
1633
1619
1634
- if generics. where_clause . predicates . is_empty ( ) {
1635
- result. push_str ( suffix) ;
1620
+ // 1 = `;`
1621
+ let shape = Shape :: indented ( indent, context. config ) . sub_width ( 1 ) ?;
1622
+ rewrite_assign_rhs ( context, lhs, & * ty, shape) . map ( |s| s + ";" )
1636
1623
} else {
1637
- result. push_str ( & indent. to_string_with_newline ( context. config ) ) ;
1638
- result. push_str ( suffix. trim_start ( ) ) ;
1624
+ Some ( format ! ( "{};" , result) )
1639
1625
}
1640
-
1641
- // 1 = ";"
1642
- let rhs_shape = Shape :: indented ( indent, context. config ) . sub_width ( 1 ) ?;
1643
- rewrite_assign_rhs ( context, result, rhs, rhs_shape) . map ( |s| s + ";" )
1644
1626
}
1645
1627
1646
1628
pub ( crate ) fn rewrite_opaque_type (
@@ -1652,16 +1634,14 @@ pub(crate) fn rewrite_opaque_type(
1652
1634
vis : & ast:: Visibility ,
1653
1635
) -> Option < String > {
1654
1636
let opaque_type_bounds = OpaqueTypeBounds { generic_bounds } ;
1655
- rewrite_type_item (
1637
+ rewrite_type (
1656
1638
context,
1657
1639
indent,
1658
- "type" ,
1659
- " =" ,
1660
1640
ident,
1661
- & opaque_type_bounds ,
1641
+ vis ,
1662
1642
generics,
1663
1643
Some ( generic_bounds) ,
1664
- vis ,
1644
+ Some ( & opaque_type_bounds ) ,
1665
1645
)
1666
1646
}
1667
1647
@@ -1912,34 +1892,15 @@ pub(crate) fn rewrite_type_alias(
1912
1892
indent : Indent ,
1913
1893
vis : & ast:: Visibility ,
1914
1894
) -> Option < String > {
1915
- let mut prefix = rewrite_type_prefix (
1895
+ rewrite_type (
1916
1896
context,
1917
1897
indent,
1918
- & format ! ( "{}type " , format_visibility( context, vis) ) ,
1919
1898
ident,
1899
+ vis,
1920
1900
generics,
1921
1901
generic_bounds_opt,
1922
- ) ?;
1923
-
1924
- if let Some ( ty) = ty_opt {
1925
- // 1 = `;`
1926
- let shape = Shape :: indented ( indent, context. config ) . sub_width ( 1 ) ?;
1927
-
1928
- // If there's a where clause, add a newline before the assignment. Otherwise just add a
1929
- // space.
1930
- if !generics. where_clause . predicates . is_empty ( ) {
1931
- prefix. push_str ( & indent. to_string_with_newline ( context. config ) ) ;
1932
- } else {
1933
- prefix. push ( ' ' ) ;
1934
- }
1935
- let lhs = format ! ( "{}=" , prefix) ;
1936
- rewrite_assign_rhs ( context, lhs, & * * ty, shape) . map ( |s| s + ";" )
1937
- } else {
1938
- if !generics. where_clause . predicates . is_empty ( ) {
1939
- prefix. push_str ( & indent. to_string_with_newline ( context. config ) ) ;
1940
- }
1941
- Some ( format ! ( "{};" , prefix) )
1942
- }
1902
+ ty_opt,
1903
+ )
1943
1904
}
1944
1905
1945
1906
struct OpaqueType < ' a > {
0 commit comments