@@ -1562,6 +1562,7 @@ fn rewrite_type_prefix(
1562
1562
prefix : & str ,
1563
1563
ident : ast:: Ident ,
1564
1564
generics : & ast:: Generics ,
1565
+ generic_bounds_opt : Option < & ast:: GenericBounds > ,
1565
1566
) -> Option < String > {
1566
1567
let mut result = String :: with_capacity ( 128 ) ;
1567
1568
result. push_str ( prefix) ;
@@ -1578,6 +1579,19 @@ fn rewrite_type_prefix(
1578
1579
result. push_str ( & generics_str) ;
1579
1580
}
1580
1581
1582
+ let type_bounds_str = if let Some ( bounds) = generic_bounds_opt {
1583
+ if bounds. is_empty ( ) {
1584
+ String :: new ( )
1585
+ } else {
1586
+ // 2 = `: `
1587
+ let shape = Shape :: indented ( indent, context. config ) . offset_left ( result. len ( ) + 2 ) ?;
1588
+ bounds. rewrite ( context, shape) . map ( |s| format ! ( ": {}" , s) ) ?
1589
+ }
1590
+ } else {
1591
+ String :: new ( )
1592
+ } ;
1593
+ result. push_str ( & type_bounds_str) ;
1594
+
1581
1595
let where_budget = context. budget ( last_line_width ( & result) ) ;
1582
1596
let option = WhereClauseOption :: snuggled ( & result) ;
1583
1597
let where_clause_str = rewrite_where_clause (
@@ -1604,6 +1618,7 @@ fn rewrite_type_item<R: Rewrite>(
1604
1618
ident : ast:: Ident ,
1605
1619
rhs : & R ,
1606
1620
generics : & ast:: Generics ,
1621
+ generic_bounds_opt : Option < & ast:: GenericBounds > ,
1607
1622
vis : & ast:: Visibility ,
1608
1623
) -> Option < String > {
1609
1624
let mut result = String :: with_capacity ( 128 ) ;
@@ -1613,6 +1628,7 @@ fn rewrite_type_item<R: Rewrite>(
1613
1628
& format ! ( "{}{} " , format_visibility( context, vis) , prefix) ,
1614
1629
ident,
1615
1630
generics,
1631
+ generic_bounds_opt,
1616
1632
) ?) ;
1617
1633
1618
1634
if generics. where_clause . predicates . is_empty ( ) {
@@ -1627,17 +1643,6 @@ fn rewrite_type_item<R: Rewrite>(
1627
1643
rewrite_assign_rhs ( context, result, rhs, rhs_shape) . map ( |s| s + ";" )
1628
1644
}
1629
1645
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
- }
1640
-
1641
1646
pub ( crate ) fn rewrite_opaque_type (
1642
1647
context : & RewriteContext < ' _ > ,
1643
1648
indent : Indent ,
@@ -1655,6 +1660,7 @@ pub(crate) fn rewrite_opaque_type(
1655
1660
ident,
1656
1661
& opaque_type_bounds,
1657
1662
generics,
1663
+ Some ( generic_bounds) ,
1658
1664
vis,
1659
1665
)
1660
1666
}
@@ -1897,39 +1903,39 @@ fn rewrite_static(
1897
1903
}
1898
1904
}
1899
1905
1900
- pub ( crate ) fn rewrite_associated_type (
1906
+ pub ( crate ) fn rewrite_type_alias (
1901
1907
ident : ast:: Ident ,
1902
1908
ty_opt : Option < & ptr:: P < ast:: Ty > > ,
1903
1909
generics : & ast:: Generics ,
1904
1910
generic_bounds_opt : Option < & ast:: GenericBounds > ,
1905
1911
context : & RewriteContext < ' _ > ,
1906
1912
indent : Indent ,
1913
+ vis : & ast:: Visibility ,
1907
1914
) -> 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
- } ;
1915
+ let mut prefix = rewrite_type_prefix (
1916
+ context,
1917
+ indent,
1918
+ & format ! ( "{}type " , format_visibility( context, vis) ) ,
1919
+ ident,
1920
+ generics,
1921
+ generic_bounds_opt,
1922
+ ) ?;
1925
1923
1926
1924
if let Some ( ty) = ty_opt {
1927
1925
// 1 = `;`
1928
1926
let shape = Shape :: indented ( indent, context. config ) . sub_width ( 1 ) ?;
1929
- let lhs = format ! ( "{}{} =" , prefix, type_bounds_str) ;
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) ;
1930
1936
rewrite_assign_rhs ( context, lhs, & * * ty, shape) . map ( |s| s + ";" )
1931
1937
} else {
1932
- Some ( format ! ( "{}{} ;" , prefix, type_bounds_str ) )
1938
+ Some ( format ! ( "{};" , prefix) )
1933
1939
}
1934
1940
}
1935
1941
@@ -1973,13 +1979,14 @@ pub(crate) fn rewrite_opaque_impl_type(
1973
1979
1974
1980
pub ( crate ) fn rewrite_associated_impl_type (
1975
1981
ident : ast:: Ident ,
1982
+ vis : & ast:: Visibility ,
1976
1983
defaultness : ast:: Defaultness ,
1977
1984
ty_opt : Option < & ptr:: P < ast:: Ty > > ,
1978
1985
generics : & ast:: Generics ,
1979
1986
context : & RewriteContext < ' _ > ,
1980
1987
indent : Indent ,
1981
1988
) -> Option < String > {
1982
- let result = rewrite_associated_type ( ident, ty_opt, generics, None , context, indent) ?;
1989
+ let result = rewrite_type_alias ( ident, ty_opt, generics, None , context, indent, vis ) ?;
1983
1990
1984
1991
match defaultness {
1985
1992
ast:: Defaultness :: Default ( ..) => Some ( format ! ( "default {}" , result) ) ,
@@ -3132,7 +3139,7 @@ impl Rewrite for ast::ForeignItem {
3132
3139
// FIXME: this may be a faulty span from libsyntax.
3133
3140
let span = mk_sp ( self . span . lo ( ) , self . span . hi ( ) - BytePos ( 1 ) ) ;
3134
3141
3135
- let item_str = match self . kind {
3142
+ let item_str: String = match self . kind {
3136
3143
ast:: ForeignItemKind :: Fn ( _, ref fn_sig, ref generics, _) => rewrite_fn_base (
3137
3144
context,
3138
3145
shape. indent ,
@@ -3156,14 +3163,20 @@ impl Rewrite for ast::ForeignItem {
3156
3163
// 1 = ;
3157
3164
rewrite_assign_rhs ( context, prefix, & * * ty, shape. sub_width ( 1 ) ?) . map ( |s| s + ";" )
3158
3165
}
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
- }
3166
+ ast:: ForeignItemKind :: TyAlias (
3167
+ _,
3168
+ ref generics,
3169
+ ref generic_bounds,
3170
+ ref type_default,
3171
+ ) => rewrite_type_alias (
3172
+ self . ident ,
3173
+ type_default. as_ref ( ) ,
3174
+ generics,
3175
+ Some ( generic_bounds) ,
3176
+ & context,
3177
+ shape. indent ,
3178
+ & self . vis ,
3179
+ ) ,
3167
3180
ast:: ForeignItemKind :: MacCall ( ref mac) => {
3168
3181
rewrite_macro ( mac, None , context, shape, MacroPosition :: Item )
3169
3182
}
0 commit comments