8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
- use ast:: { meta_item, item, expr} ;
11
+ use ast:: { meta_item, item, expr, and } ;
12
12
use codemap:: span;
13
13
use ext:: base:: ext_ctxt;
14
14
use ext:: build;
@@ -31,7 +31,7 @@ pub fn expand_deriving_iter_bytes(cx: @ext_ctxt,
31
31
Literal ( Path :: new ( ~[ ~"bool "] ) ) ,
32
32
Literal ( Path :: new ( ~[ ~"core", ~"to_bytes", ~"Cb "] ) )
33
33
] ,
34
- ret_ty : nil_ty ( ) ,
34
+ ret_ty : Literal ( Path :: new ( ~ [ ~" bool " ] ) ) ,
35
35
const_nonmatching : false ,
36
36
combine_substructure: iter_bytes_substructure
37
37
}
@@ -58,13 +58,11 @@ fn iter_bytes_substructure(cx: @ext_ctxt, span: span, substr: &Substructure) ->
58
58
} ;
59
59
let iter_bytes_ident = substr. method_ident;
60
60
let call_iterbytes = |thing_expr| {
61
- build:: mk_stmt(
62
- cx, span,
63
- build:: mk_method_call( cx, span,
64
- thing_expr, iter_bytes_ident,
65
- copy lsb0_f) )
61
+ build:: mk_method_call( cx, span,
62
+ thing_expr, iter_bytes_ident,
63
+ copy lsb0_f)
66
64
} ;
67
- let mut stmts = ~[ ] ;
65
+ let mut exprs = ~[ ] ;
68
66
let fields;
69
67
match * substr. fields {
70
68
Struct ( ref fs) => {
@@ -78,16 +76,22 @@ fn iter_bytes_substructure(cx: @ext_ctxt, span: span, substr: &Substructure) ->
78
76
None => build:: mk_uint ( cx, span, index)
79
77
} ;
80
78
81
- stmts . push ( call_iterbytes ( discriminant) ) ;
79
+ exprs . push ( call_iterbytes ( discriminant) ) ;
82
80
83
81
fields = fs;
84
82
}
85
83
_ => cx. span_bug ( span, "Impossible substructure in `deriving(IterBytes)`" )
86
84
}
87
85
88
86
for fields. each |& ( _, field, _) | {
89
- stmts . push ( call_iterbytes ( field) ) ;
87
+ exprs . push ( call_iterbytes ( field) ) ;
90
88
}
91
89
92
- build:: mk_block ( cx, span, ~[ ] , stmts, None )
90
+ if exprs. len ( ) == 0 {
91
+ cx. span_bug ( span, "#[deriving(IterBytes)] needs at least one field" ) ;
92
+ }
93
+
94
+ do vec:: foldl ( exprs[ 0 ] , exprs. slice ( 1 , exprs. len ( ) ) ) |prev, me| {
95
+ build:: mk_binary ( cx, span, and, prev, * me)
96
+ }
93
97
}
0 commit comments