@@ -746,73 +746,74 @@ impl<'a> FmtVisitor<'a> {
746
746
result. push_str ( & indent. to_string ( self . config ) ) ;
747
747
}
748
748
749
- let variant_body = match field. node . kind {
750
- ast:: VariantKind :: TupleVariantKind ( ref types) => {
749
+ let variant_body = match * field. node . data {
750
+ ast:: VariantData :: Tuple ( ref types, _ ) => {
751
751
let mut result = field. node . name . to_string ( ) ;
752
+ let items = itemize_list ( self . codemap ,
753
+ types. iter ( ) ,
754
+ ")" ,
755
+ |arg| arg. node . ty . span . lo ,
756
+ |arg| arg. node . ty . span . hi ,
757
+ |arg| {
758
+ // FIXME silly width, indent
759
+ arg. node
760
+ . ty
761
+ . rewrite ( & self . get_context ( ) , 1000 , Indent :: empty ( ) )
762
+ } ,
763
+ span_after ( field. span , "(" , self . codemap ) ,
764
+ field. span . hi ) ;
765
+ let item_vec = items. collect :: < Vec < _ > > ( ) ;
752
766
753
- if !types. is_empty ( ) {
754
- let items = itemize_list ( self . codemap ,
755
- types. iter ( ) ,
756
- ")" ,
757
- |arg| arg. ty . span . lo ,
758
- |arg| arg. ty . span . hi ,
759
- |arg| {
760
- // FIXME silly width, indent
761
- arg. ty . rewrite ( & self . get_context ( ) ,
762
- 1000 ,
763
- Indent :: empty ( ) )
764
- } ,
765
- span_after ( field. span , "(" , self . codemap ) ,
766
- field. span . hi ) ;
767
- let item_vec = items. collect :: < Vec < _ > > ( ) ;
768
-
769
- result. push ( '(' ) ;
770
-
771
- let indent = indent + field. node . name . to_string ( ) . len ( ) + "(" . len ( ) ;
772
-
773
- let comma_cost = if self . config . enum_trailing_comma {
774
- 1
775
- } else {
776
- 0
777
- } ;
778
- let budget = self . config . max_width - indent. width ( ) - comma_cost - 1 ; // 1 = )
779
- let tactic = definitive_tactic ( & item_vec,
780
- ListTactic :: HorizontalVertical ,
781
- budget) ;
782
-
783
- let fmt = ListFormatting {
784
- tactic : tactic,
785
- separator : "," ,
786
- trailing_separator : SeparatorTactic :: Never ,
787
- indent : indent,
788
- width : budget,
789
- ends_with_newline : true ,
790
- config : self . config ,
791
- } ;
792
- let list_str = try_opt ! ( write_list( & item_vec, & fmt) ) ;
793
-
794
- result. push_str ( & list_str) ;
795
- result. push ( ')' ) ;
796
- }
767
+ result. push ( '(' ) ;
797
768
798
- if let Some ( ref expr) = field. node . disr_expr {
799
- result. push_str ( " = " ) ;
800
- let expr_snippet = self . snippet ( expr. span ) ;
801
- result. push_str ( & expr_snippet) ;
802
- }
769
+ let indent = indent + field. node . name . to_string ( ) . len ( ) + "(" . len ( ) ;
770
+
771
+ let comma_cost = if self . config . enum_trailing_comma {
772
+ 1
773
+ } else {
774
+ 0
775
+ } ;
776
+ let budget = self . config . max_width - indent. width ( ) - comma_cost - 1 ; // 1 = )
777
+ let tactic = definitive_tactic ( & item_vec, ListTactic :: HorizontalVertical , budget) ;
778
+
779
+ let fmt = ListFormatting {
780
+ tactic : tactic,
781
+ separator : "," ,
782
+ trailing_separator : SeparatorTactic :: Never ,
783
+ indent : indent,
784
+ width : budget,
785
+ ends_with_newline : true ,
786
+ config : self . config ,
787
+ } ;
788
+ let list_str = try_opt ! ( write_list( & item_vec, & fmt) ) ;
789
+
790
+ result. push_str ( & list_str) ;
791
+ result. push ( ')' ) ;
803
792
804
793
Some ( result)
805
794
}
806
- ast:: VariantKind :: StructVariantKind ( ref struct_def ) => {
795
+ ast:: VariantData :: Struct ( .. ) => {
807
796
// FIXME: Should limit the width, as we have a trailing comma
808
797
self . format_struct ( "" ,
809
798
field. node . name ,
810
799
ast:: Visibility :: Inherited ,
811
- struct_def ,
800
+ & * field . node . data ,
812
801
None ,
813
802
field. span ,
814
803
indent)
815
804
}
805
+ ast:: VariantData :: Unit ( ..) => {
806
+ let tag = if let Some ( ref expr) = field. node . disr_expr {
807
+ format ! ( "{} = {}" , field. node. name, self . snippet( expr. span) )
808
+ } else {
809
+ field. node . name . to_string ( )
810
+ } ;
811
+
812
+ wrap_str ( tag,
813
+ self . config . max_width ,
814
+ self . config . max_width - indent. width ( ) ,
815
+ indent)
816
+ }
816
817
} ;
817
818
818
819
if let Some ( variant_str) = variant_body {
@@ -827,7 +828,7 @@ impl<'a> FmtVisitor<'a> {
827
828
item_name : & str ,
828
829
ident : ast:: Ident ,
829
830
vis : ast:: Visibility ,
830
- struct_def : & ast:: StructDef ,
831
+ struct_def : & ast:: VariantData ,
831
832
generics : Option < & ast:: Generics > ,
832
833
span : Span ,
833
834
offset : Indent )
@@ -837,14 +838,13 @@ impl<'a> FmtVisitor<'a> {
837
838
let header_str = self . format_header ( item_name, ident, vis) ;
838
839
result. push_str ( & header_str) ;
839
840
840
- if struct_def. fields . is_empty ( ) {
841
- result. push ( ';' ) ;
842
- return Some ( result) ;
843
- }
844
-
845
- let is_tuple = match struct_def. fields [ 0 ] . node . kind {
846
- ast:: StructFieldKind :: NamedField ( ..) => false ,
847
- ast:: StructFieldKind :: UnnamedField ( ..) => true ,
841
+ let ( is_tuple, fields) = match * struct_def {
842
+ ast:: VariantData :: Unit ( ..) => {
843
+ result. push ( ';' ) ;
844
+ return Some ( result) ;
845
+ }
846
+ ast:: VariantData :: Tuple ( ref vec, _) => ( true , vec) ,
847
+ ast:: VariantData :: Struct ( ref vec, _) => ( false , vec) ,
848
848
} ;
849
849
850
850
let ( opener, terminator) = if is_tuple {
@@ -859,15 +859,14 @@ impl<'a> FmtVisitor<'a> {
859
859
opener,
860
860
offset,
861
861
offset + header_str. len( ) ,
862
- codemap:: mk_sp( span. lo,
863
- struct_def. fields[ 0 ] . span. lo) ) )
862
+ codemap:: mk_sp( span. lo, fields[ 0 ] . span. lo) ) )
864
863
}
865
864
None => opener. to_owned ( ) ,
866
865
} ;
867
866
result. push_str ( & generics_str) ;
868
867
869
868
let items = itemize_list ( self . codemap ,
870
- struct_def . fields . iter ( ) ,
869
+ fields. iter ( ) ,
871
870
terminator,
872
871
|field| {
873
872
// Include attributes and doc comments, if present
@@ -886,7 +885,7 @@ impl<'a> FmtVisitor<'a> {
886
885
let used_budget = offset. width ( ) + header_str. len ( ) + generics_str. len ( ) + 3 ;
887
886
888
887
// Conservative approximation
889
- let single_line_cost = ( span. hi - struct_def . fields [ 0 ] . span . lo ) . 0 ;
888
+ let single_line_cost = ( span. hi - fields[ 0 ] . span . lo ) . 0 ;
890
889
let break_line = !is_tuple || generics_str. contains ( '\n' ) ||
891
890
single_line_cost as usize + used_budget > self . config . max_width ;
892
891
@@ -932,7 +931,7 @@ impl<'a> FmtVisitor<'a> {
932
931
pub fn visit_struct ( & mut self ,
933
932
ident : ast:: Ident ,
934
933
vis : ast:: Visibility ,
935
- struct_def : & ast:: StructDef ,
934
+ struct_def : & ast:: VariantData ,
936
935
generics : & ast:: Generics ,
937
936
span : Span ) {
938
937
let indent = self . block_indent ;
0 commit comments