@@ -1556,44 +1556,44 @@ fn format_tuple_struct(
15561556 Some ( result)
15571557}
15581558
1559- fn rewrite_type_prefix (
1559+ fn rewrite_type < R : Rewrite > (
15601560 context : & RewriteContext < ' _ > ,
15611561 indent : Indent ,
1562- prefix : & str ,
15631562 ident : ast:: Ident ,
1563+ vis : & ast:: Visibility ,
15641564 generics : & ast:: Generics ,
15651565 generic_bounds_opt : Option < & ast:: GenericBounds > ,
1566+ rhs : Option < & R > ,
15661567) -> Option < String > {
15671568 let mut result = String :: with_capacity ( 128 ) ;
1568- result. push_str ( prefix ) ;
1569+ result. push_str ( & format ! ( "{}type " , format_visibility ( context , vis ) ) ) ;
15691570 let ident_str = rewrite_ident ( context, ident) ;
15701571
1571- // 2 = `= `
15721572 if generics. params . is_empty ( ) {
15731573 result. push_str ( ident_str)
15741574 } else {
1575+ // 2 = `= `
15751576 let g_shape = Shape :: indented ( indent, context. config )
15761577 . offset_left ( result. len ( ) ) ?
15771578 . sub_width ( 2 ) ?;
15781579 let generics_str = rewrite_generics ( context, ident_str, generics, g_shape) ?;
15791580 result. push_str ( & generics_str) ;
15801581 }
15811582
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 ( ) {
15861585 // 2 = `: `
15871586 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) ;
15891589 }
1590- } else {
1591- String :: new ( )
1592- } ;
1593- result. push_str ( & type_bounds_str) ;
1590+ }
15941591
15951592 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+ }
15971597 let where_clause_str = rewrite_where_clause (
15981598 context,
15991599 & generics. where_clause ,
@@ -1607,40 +1607,22 @@ fn rewrite_type_prefix(
16071607 ) ?;
16081608 result. push_str ( & where_clause_str) ;
16091609
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) ;
16331619
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 + ";" )
16361623 } else {
1637- result. push_str ( & indent. to_string_with_newline ( context. config ) ) ;
1638- result. push_str ( suffix. trim_start ( ) ) ;
1624+ Some ( format ! ( "{};" , result) )
16391625 }
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 + ";" )
16441626}
16451627
16461628pub ( crate ) fn rewrite_opaque_type (
@@ -1652,16 +1634,14 @@ pub(crate) fn rewrite_opaque_type(
16521634 vis : & ast:: Visibility ,
16531635) -> Option < String > {
16541636 let opaque_type_bounds = OpaqueTypeBounds { generic_bounds } ;
1655- rewrite_type_item (
1637+ rewrite_type (
16561638 context,
16571639 indent,
1658- "type" ,
1659- " =" ,
16601640 ident,
1661- & opaque_type_bounds ,
1641+ vis ,
16621642 generics,
16631643 Some ( generic_bounds) ,
1664- vis ,
1644+ Some ( & opaque_type_bounds ) ,
16651645 )
16661646}
16671647
@@ -1912,34 +1892,15 @@ pub(crate) fn rewrite_type_alias(
19121892 indent : Indent ,
19131893 vis : & ast:: Visibility ,
19141894) -> Option < String > {
1915- let mut prefix = rewrite_type_prefix (
1895+ rewrite_type (
19161896 context,
19171897 indent,
1918- & format ! ( "{}type " , format_visibility( context, vis) ) ,
19191898 ident,
1899+ vis,
19201900 generics,
19211901 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+ )
19431904}
19441905
19451906struct OpaqueType < ' a > {
0 commit comments