@@ -1556,30 +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
+ generic_bounds_opt : Option < & ast:: GenericBounds > ,
1566
+ rhs : Option < & R > ,
1565
1567
) -> Option < String > {
1566
1568
let mut result = String :: with_capacity ( 128 ) ;
1567
- result. push_str ( prefix ) ;
1569
+ result. push_str ( & format ! ( "{}type " , format_visibility ( context , vis ) ) ) ;
1568
1570
let ident_str = rewrite_ident ( context, ident) ;
1569
1571
1570
- // 2 = `= `
1571
1572
if generics. params . is_empty ( ) {
1572
1573
result. push_str ( ident_str)
1573
1574
} else {
1575
+ // 2 = `= `
1574
1576
let g_shape = Shape :: indented ( indent, context. config )
1575
1577
. offset_left ( result. len ( ) ) ?
1576
1578
. sub_width ( 2 ) ?;
1577
1579
let generics_str = rewrite_generics ( context, ident_str, generics, g_shape) ?;
1578
1580
result. push_str ( & generics_str) ;
1579
1581
}
1580
1582
1583
+ if let Some ( bounds) = generic_bounds_opt {
1584
+ if !bounds. is_empty ( ) {
1585
+ // 2 = `: `
1586
+ let shape = Shape :: indented ( indent, context. config ) . offset_left ( result. len ( ) + 2 ) ?;
1587
+ let type_bounds = bounds. rewrite ( context, shape) . map ( |s| format ! ( ": {}" , s) ) ?;
1588
+ result. push_str ( & type_bounds) ;
1589
+ }
1590
+ }
1591
+
1581
1592
let where_budget = context. budget ( last_line_width ( & result) ) ;
1582
- let option = WhereClauseOption :: snuggled ( & result) ;
1593
+ let mut option = WhereClauseOption :: snuggled ( & result) ;
1594
+ if rhs. is_none ( ) {
1595
+ option. suppress_comma ( ) ;
1596
+ }
1583
1597
let where_clause_str = rewrite_where_clause (
1584
1598
context,
1585
1599
& generics. where_clause ,
@@ -1593,49 +1607,22 @@ fn rewrite_type_prefix(
1593
1607
) ?;
1594
1608
result. push_str ( & where_clause_str) ;
1595
1609
1596
- Some ( result)
1597
- }
1598
-
1599
- fn rewrite_type_item < R : Rewrite > (
1600
- context : & RewriteContext < ' _ > ,
1601
- indent : Indent ,
1602
- prefix : & str ,
1603
- suffix : & str ,
1604
- ident : ast:: Ident ,
1605
- rhs : & R ,
1606
- generics : & ast:: Generics ,
1607
- vis : & ast:: Visibility ,
1608
- ) -> Option < String > {
1609
- let mut result = String :: with_capacity ( 128 ) ;
1610
- result. push_str ( & rewrite_type_prefix (
1611
- context,
1612
- indent,
1613
- & format ! ( "{}{} " , format_visibility( context, vis) , prefix) ,
1614
- ident,
1615
- generics,
1616
- ) ?) ;
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) ;
1617
1619
1618
- if generics. where_clause . predicates . is_empty ( ) {
1619
- 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 + ";" )
1620
1623
} else {
1621
- result. push_str ( & indent. to_string_with_newline ( context. config ) ) ;
1622
- result. push_str ( suffix. trim_start ( ) ) ;
1624
+ Some ( format ! ( "{};" , result) )
1623
1625
}
1624
-
1625
- // 1 = ";"
1626
- let rhs_shape = Shape :: indented ( indent, context. config ) . sub_width ( 1 ) ?;
1627
- rewrite_assign_rhs ( context, result, rhs, rhs_shape) . map ( |s| s + ";" )
1628
- }
1629
-
1630
- pub ( crate ) fn rewrite_type_alias (
1631
- context : & RewriteContext < ' _ > ,
1632
- indent : Indent ,
1633
- ident : ast:: Ident ,
1634
- ty : & ast:: Ty ,
1635
- generics : & ast:: Generics ,
1636
- vis : & ast:: Visibility ,
1637
- ) -> Option < String > {
1638
- rewrite_type_item ( context, indent, "type" , " =" , ident, ty, generics, vis)
1639
1626
}
1640
1627
1641
1628
pub ( crate ) fn rewrite_opaque_type (
@@ -1647,15 +1634,14 @@ pub(crate) fn rewrite_opaque_type(
1647
1634
vis : & ast:: Visibility ,
1648
1635
) -> Option < String > {
1649
1636
let opaque_type_bounds = OpaqueTypeBounds { generic_bounds } ;
1650
- rewrite_type_item (
1637
+ rewrite_type (
1651
1638
context,
1652
1639
indent,
1653
- "type" ,
1654
- " =" ,
1655
1640
ident,
1656
- & opaque_type_bounds,
1657
- generics,
1658
1641
vis,
1642
+ generics,
1643
+ Some ( generic_bounds) ,
1644
+ Some ( & opaque_type_bounds) ,
1659
1645
)
1660
1646
}
1661
1647
@@ -1897,40 +1883,24 @@ fn rewrite_static(
1897
1883
}
1898
1884
}
1899
1885
1900
- pub ( crate ) fn rewrite_associated_type (
1886
+ pub ( crate ) fn rewrite_type_alias (
1901
1887
ident : ast:: Ident ,
1902
1888
ty_opt : Option < & ptr:: P < ast:: Ty > > ,
1903
1889
generics : & ast:: Generics ,
1904
1890
generic_bounds_opt : Option < & ast:: GenericBounds > ,
1905
1891
context : & RewriteContext < ' _ > ,
1906
1892
indent : Indent ,
1893
+ vis : & ast:: Visibility ,
1907
1894
) -> Option < String > {
1908
- let ident_str = rewrite_ident ( context, ident) ;
1909
- // 5 = "type "
1910
- let generics_shape = Shape :: indented ( indent, context. config ) . offset_left ( 5 ) ?;
1911
- let generics_str = rewrite_generics ( context, ident_str, generics, generics_shape) ?;
1912
- let prefix = format ! ( "type {}" , generics_str) ;
1913
-
1914
- let type_bounds_str = if let Some ( bounds) = generic_bounds_opt {
1915
- if bounds. is_empty ( ) {
1916
- String :: new ( )
1917
- } else {
1918
- // 2 = ": ".len()
1919
- let shape = Shape :: indented ( indent, context. config ) . offset_left ( prefix. len ( ) + 2 ) ?;
1920
- bounds. rewrite ( context, shape) . map ( |s| format ! ( ": {}" , s) ) ?
1921
- }
1922
- } else {
1923
- String :: new ( )
1924
- } ;
1925
-
1926
- if let Some ( ty) = ty_opt {
1927
- // 1 = `;`
1928
- let shape = Shape :: indented ( indent, context. config ) . sub_width ( 1 ) ?;
1929
- let lhs = format ! ( "{}{} =" , prefix, type_bounds_str) ;
1930
- rewrite_assign_rhs ( context, lhs, & * * ty, shape) . map ( |s| s + ";" )
1931
- } else {
1932
- Some ( format ! ( "{}{};" , prefix, type_bounds_str) )
1933
- }
1895
+ rewrite_type (
1896
+ context,
1897
+ indent,
1898
+ ident,
1899
+ vis,
1900
+ generics,
1901
+ generic_bounds_opt,
1902
+ ty_opt,
1903
+ )
1934
1904
}
1935
1905
1936
1906
struct OpaqueType < ' a > {
@@ -1973,13 +1943,14 @@ pub(crate) fn rewrite_opaque_impl_type(
1973
1943
1974
1944
pub ( crate ) fn rewrite_associated_impl_type (
1975
1945
ident : ast:: Ident ,
1946
+ vis : & ast:: Visibility ,
1976
1947
defaultness : ast:: Defaultness ,
1977
1948
ty_opt : Option < & ptr:: P < ast:: Ty > > ,
1978
1949
generics : & ast:: Generics ,
1979
1950
context : & RewriteContext < ' _ > ,
1980
1951
indent : Indent ,
1981
1952
) -> Option < String > {
1982
- let result = rewrite_associated_type ( ident, ty_opt, generics, None , context, indent) ?;
1953
+ let result = rewrite_type_alias ( ident, ty_opt, generics, None , context, indent, vis ) ?;
1983
1954
1984
1955
match defaultness {
1985
1956
ast:: Defaultness :: Default ( ..) => Some ( format ! ( "default {}" , result) ) ,
@@ -3156,14 +3127,20 @@ impl Rewrite for ast::ForeignItem {
3156
3127
// 1 = ;
3157
3128
rewrite_assign_rhs ( context, prefix, & * * ty, shape. sub_width ( 1 ) ?) . map ( |s| s + ";" )
3158
3129
}
3159
- ast:: ForeignItemKind :: TyAlias ( ..) => {
3160
- let vis = format_visibility ( context, & self . vis ) ;
3161
- Some ( format ! (
3162
- "{}type {};" ,
3163
- vis,
3164
- rewrite_ident( context, self . ident)
3165
- ) )
3166
- }
3130
+ ast:: ForeignItemKind :: TyAlias (
3131
+ _,
3132
+ ref generics,
3133
+ ref generic_bounds,
3134
+ ref type_default,
3135
+ ) => rewrite_type_alias (
3136
+ self . ident ,
3137
+ type_default. as_ref ( ) ,
3138
+ generics,
3139
+ Some ( generic_bounds) ,
3140
+ & context,
3141
+ shape. indent ,
3142
+ & self . vis ,
3143
+ ) ,
3167
3144
ast:: ForeignItemKind :: MacCall ( ref mac) => {
3168
3145
rewrite_macro ( mac, None , context, shape, MacroPosition :: Item )
3169
3146
}
0 commit comments