Skip to content

Commit 957d12d

Browse files
ericktnikomatsakis
authored andcommitted
---
yaml --- r: 33006 b: refs/heads/dist-snap c: 2569adc h: refs/heads/master v: v3
1 parent 72690b7 commit 957d12d

File tree

4 files changed

+138
-78
lines changed

4 files changed

+138
-78
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: d0c6ce338884ee21843f4b40bf6bf18d222ce5df
99
refs/heads/incoming: d9317a174e434d4c99fc1a37fd7dc0d2f5328d37
10-
refs/heads/dist-snap: 372c7de20104bd3f968cda6429dfad2c1d559a35
10+
refs/heads/dist-snap: 2569adc5ea0b950c6e41a1c72d9eb7efdba49f05
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1212
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/dist-snap/src/libsyntax/ext/auto_serialize2.rs

Lines changed: 124 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
/*
22
3-
The compiler code necessary to implement the #[auto_serialize2]
4-
extension. The idea here is that type-defining items may be tagged
5-
with #[auto_serialize2], which will cause us to generate a little
6-
companion module with the same name as the item.
3+
The compiler code necessary to implement the #[auto_serialize2] and
4+
#[auto_deserialize2] extension. The idea here is that type-defining items may
5+
be tagged with #[auto_serialize2] and #[auto_deserialize2], which will cause
6+
us to generate a little companion module with the same name as the item.
77
88
For example, a type like:
99
1010
#[auto_serialize2]
11+
#[auto_deserialize2]
1112
struct Node {id: uint}
1213
1314
would generate two implementations like:
@@ -34,6 +35,7 @@ Other interesting scenarios are whe the item has type parameters or
3435
references other non-built-in types. A type definition like:
3536
3637
#[auto_serialize2]
38+
#[auto_deserialize2]
3739
type spanned<T> = {node: T, span: span};
3840
3941
would yield functions like:
@@ -75,92 +77,139 @@ use codemap::span;
7577
use std::map;
7678
use std::map::HashMap;
7779

78-
export expand;
80+
export expand_auto_serialize;
81+
export expand_auto_deserialize;
7982

8083
// Transitional reexports so qquote can find the paths it is looking for
8184
mod syntax {
8285
pub use ext;
8386
pub use parse;
8487
}
8588

86-
fn expand(cx: ext_ctxt,
87-
span: span,
88-
_mitem: ast::meta_item,
89-
in_items: ~[@ast::item]) -> ~[@ast::item] {
90-
fn not_auto_serialize2(a: &ast::attribute) -> bool {
91-
attr::get_attr_name(*a) != ~"auto_serialize2"
89+
fn expand_auto_serialize(
90+
cx: ext_ctxt,
91+
span: span,
92+
_mitem: ast::meta_item,
93+
in_items: ~[@ast::item]
94+
) -> ~[@ast::item] {
95+
fn is_auto_serialize2(a: &ast::attribute) -> bool {
96+
attr::get_attr_name(*a) == ~"auto_serialize2"
9297
}
9398

9499
fn filter_attrs(item: @ast::item) -> @ast::item {
95-
@{attrs: vec::filter(item.attrs, not_auto_serialize2),
100+
@{attrs: vec::filter(item.attrs, |a| !is_auto_serialize2(a)),
96101
.. *item}
97102
}
98103

99104
do vec::flat_map(in_items) |item| {
100-
match item.node {
101-
ast::item_ty(@{node: ast::ty_rec(fields), _}, tps) => {
102-
let ser_impl = mk_rec_ser_impl(
103-
cx,
104-
item.span,
105-
item.ident,
106-
fields,
107-
tps
108-
);
109-
110-
let deser_impl = mk_rec_deser_impl(
111-
cx,
112-
item.span,
113-
item.ident,
114-
fields,
115-
tps
116-
);
117-
118-
~[filter_attrs(*item), ser_impl, deser_impl]
119-
},
120-
ast::item_class(@{ fields, _}, tps) => {
121-
let ser_impl = mk_struct_ser_impl(
122-
cx,
123-
item.span,
124-
item.ident,
125-
fields,
126-
tps
127-
);
128-
129-
let deser_impl = mk_struct_deser_impl(
130-
cx,
131-
item.span,
132-
item.ident,
133-
fields,
134-
tps
135-
);
136-
137-
~[filter_attrs(*item), ser_impl, deser_impl]
138-
},
139-
ast::item_enum(enum_def, tps) => {
140-
let ser_impl = mk_enum_ser_impl(
141-
cx,
142-
item.span,
143-
item.ident,
144-
enum_def,
145-
tps
146-
);
147-
148-
let deser_impl = mk_enum_deser_impl(
149-
cx,
150-
item.span,
151-
item.ident,
152-
enum_def,
153-
tps
154-
);
155-
156-
~[filter_attrs(*item), ser_impl, deser_impl]
157-
},
158-
_ => {
159-
cx.span_err(span, ~"#[auto_serialize2] can only be applied \
160-
to structs, record types, and enum \
161-
definitions");
162-
~[*item]
105+
if item.attrs.any(is_auto_serialize2) {
106+
match item.node {
107+
ast::item_ty(@{node: ast::ty_rec(fields), _}, tps) => {
108+
let ser_impl = mk_rec_ser_impl(
109+
cx,
110+
item.span,
111+
item.ident,
112+
fields,
113+
tps
114+
);
115+
116+
~[filter_attrs(*item), ser_impl]
117+
},
118+
ast::item_class(@{ fields, _}, tps) => {
119+
let ser_impl = mk_struct_ser_impl(
120+
cx,
121+
item.span,
122+
item.ident,
123+
fields,
124+
tps
125+
);
126+
127+
~[filter_attrs(*item), ser_impl]
128+
},
129+
ast::item_enum(enum_def, tps) => {
130+
let ser_impl = mk_enum_ser_impl(
131+
cx,
132+
item.span,
133+
item.ident,
134+
enum_def,
135+
tps
136+
);
137+
138+
~[filter_attrs(*item), ser_impl]
139+
},
140+
_ => {
141+
cx.span_err(span, ~"#[auto_serialize2] can only be \
142+
applied to structs, record types, \
143+
and enum definitions");
144+
~[*item]
145+
}
146+
}
147+
} else {
148+
~[*item]
149+
}
150+
}
151+
}
152+
153+
fn expand_auto_deserialize(
154+
cx: ext_ctxt,
155+
span: span,
156+
_mitem: ast::meta_item,
157+
in_items: ~[@ast::item]
158+
) -> ~[@ast::item] {
159+
fn is_auto_deserialize2(a: &ast::attribute) -> bool {
160+
attr::get_attr_name(*a) == ~"auto_deserialize2"
161+
}
162+
163+
fn filter_attrs(item: @ast::item) -> @ast::item {
164+
@{attrs: vec::filter(item.attrs, |a| !is_auto_deserialize2(a)),
165+
.. *item}
166+
}
167+
168+
do vec::flat_map(in_items) |item| {
169+
if item.attrs.any(is_auto_deserialize2) {
170+
match item.node {
171+
ast::item_ty(@{node: ast::ty_rec(fields), _}, tps) => {
172+
let deser_impl = mk_rec_deser_impl(
173+
cx,
174+
item.span,
175+
item.ident,
176+
fields,
177+
tps
178+
);
179+
180+
~[filter_attrs(*item), deser_impl]
181+
},
182+
ast::item_class(@{ fields, _}, tps) => {
183+
let deser_impl = mk_struct_deser_impl(
184+
cx,
185+
item.span,
186+
item.ident,
187+
fields,
188+
tps
189+
);
190+
191+
~[filter_attrs(*item), deser_impl]
192+
},
193+
ast::item_enum(enum_def, tps) => {
194+
let deser_impl = mk_enum_deser_impl(
195+
cx,
196+
item.span,
197+
item.ident,
198+
enum_def,
199+
tps
200+
);
201+
202+
~[filter_attrs(*item), deser_impl]
203+
},
204+
_ => {
205+
cx.span_err(span, ~"#[auto_deserialize2] can only be \
206+
applied to structs, record types, \
207+
and enum definitions");
208+
~[*item]
209+
}
163210
}
211+
} else {
212+
~[*item]
164213
}
165214
}
166215
}

branches/dist-snap/src/libsyntax/ext/base.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,12 @@ fn syntax_expander_table() -> HashMap<~str, syntax_extension> {
8282
syntax_expanders.insert(~"fmt", builtin(ext::fmt::expand_syntax_ext));
8383
syntax_expanders.insert(~"auto_serialize",
8484
item_decorator(ext::auto_serialize::expand));
85-
syntax_expanders.insert(~"auto_serialize2",
86-
item_decorator(ext::auto_serialize2::expand));
85+
syntax_expanders.insert(
86+
~"auto_serialize2",
87+
item_decorator(ext::auto_serialize2::expand_auto_serialize));
88+
syntax_expanders.insert(
89+
~"auto_deserialize2",
90+
item_decorator(ext::auto_serialize2::expand_auto_deserialize));
8791
syntax_expanders.insert(~"env", builtin(ext::env::expand_syntax_ext));
8892
syntax_expanders.insert(~"concat_idents",
8993
builtin(ext::concat_idents::expand_syntax_ext));

branches/dist-snap/src/test/run-pass/auto_serialize2.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ fn test_ser_and_deser<A:Eq Serializable Deserializable>(
3131
}
3232

3333
#[auto_serialize2]
34+
#[auto_deserialize2]
3435
enum Expr {
3536
Val(uint),
3637
Plus(@Expr, @Expr),
@@ -105,6 +106,7 @@ impl CLike : cmp::Eq {
105106
}
106107

107108
#[auto_serialize2]
109+
#[auto_deserialize2]
108110
type Spanned<T> = {lo: uint, hi: uint, node: T};
109111

110112
impl<T:cmp::Eq> Spanned<T> : cmp::Eq {
@@ -115,21 +117,26 @@ impl<T:cmp::Eq> Spanned<T> : cmp::Eq {
115117
}
116118

117119
#[auto_serialize2]
120+
#[auto_deserialize2]
118121
type SomeRec = {v: ~[uint]};
119122

120123
#[auto_serialize2]
124+
#[auto_deserialize2]
121125
enum AnEnum = SomeRec;
122126

123127
#[auto_serialize2]
128+
#[auto_deserialize2]
124129
struct Point {x: uint, y: uint}
125130

126131
#[auto_serialize2]
132+
#[auto_deserialize2]
127133
enum Quark<T> {
128134
Top(T),
129135
Bottom(T)
130136
}
131137

132138
#[auto_serialize2]
139+
#[auto_deserialize2]
133140
enum CLike { A, B, C }
134141

135142
fn main() {

0 commit comments

Comments
 (0)