Skip to content

Commit 313cb8a

Browse files
committed
Change ItemModifier and ItemDecorator to traits
For convenience, the traits are implemented for the respective bare functions. Change code from this: ```rust ItemDecorator(some_function) // or ItemModifier(some_other_function) ``` to ```rust ItemDecorator(box some_function) // or ItemModifier(box some_other_function) ``` [breaking-change]
1 parent 6ceb9b4 commit 313cb8a

File tree

3 files changed

+46
-13
lines changed

3 files changed

+46
-13
lines changed

src/libsyntax/ext/base.rs

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,45 @@ pub struct MacroDef {
3939
pub ext: SyntaxExtension
4040
}
4141

42-
pub type ItemDecorator =
43-
fn(&mut ExtCtxt, Span, Gc<ast::MetaItem>, Gc<ast::Item>, |Gc<ast::Item>|);
42+
pub trait ItemDecorator {
43+
fn expand(&self,
44+
ecx: &mut ExtCtxt,
45+
sp: Span,
46+
meta_item: Gc<ast::MetaItem>,
47+
item: Gc<ast::Item>,
48+
push: |Gc<ast::Item>|);
49+
}
50+
51+
impl ItemDecorator for fn(&mut ExtCtxt, Span, Gc<ast::MetaItem>, Gc<ast::Item>, |Gc<ast::Item>|) {
52+
fn expand(&self,
53+
ecx: &mut ExtCtxt,
54+
sp: Span,
55+
meta_item: Gc<ast::MetaItem>,
56+
item: Gc<ast::Item>,
57+
push: |Gc<ast::Item>|) {
58+
(*self)(ecx, sp, meta_item, item, push)
59+
}
60+
}
4461

45-
pub type ItemModifier =
46-
fn(&mut ExtCtxt, Span, Gc<ast::MetaItem>, Gc<ast::Item>) -> Gc<ast::Item>;
62+
pub trait ItemModifier {
63+
fn expand(&self,
64+
ecx: &mut ExtCtxt,
65+
span: Span,
66+
meta_item: Gc<ast::MetaItem>,
67+
item: Gc<ast::Item>)
68+
-> Gc<ast::Item>;
69+
}
70+
71+
impl ItemModifier for fn(&mut ExtCtxt, Span, Gc<ast::MetaItem>, Gc<ast::Item>) -> Gc<ast::Item> {
72+
fn expand(&self,
73+
ecx: &mut ExtCtxt,
74+
span: Span,
75+
meta_item: Gc<ast::MetaItem>,
76+
item: Gc<ast::Item>)
77+
-> Gc<ast::Item> {
78+
(*self)(ecx, span, meta_item, item)
79+
}
80+
}
4781

4882
pub struct BasicMacroExpander {
4983
pub expander: MacroExpanderFn,
@@ -281,11 +315,11 @@ pub enum SyntaxExtension {
281315
/// based upon it.
282316
///
283317
/// `#[deriving(...)]` is an `ItemDecorator`.
284-
ItemDecorator(ItemDecorator),
318+
ItemDecorator(Box<ItemDecorator + 'static>),
285319

286320
/// A syntax extension that is attached to an item and modifies it
287321
/// in-place.
288-
ItemModifier(ItemModifier),
322+
ItemModifier(Box<ItemModifier + 'static>),
289323

290324
/// A normal, function-like syntax extension.
291325
///
@@ -371,7 +405,7 @@ fn initial_syntax_expander_table() -> SyntaxEnv {
371405
builtin_normal_expander(
372406
ext::log_syntax::expand_syntax_ext));
373407
syntax_expanders.insert(intern("deriving"),
374-
ItemDecorator(ext::deriving::expand_meta_deriving));
408+
ItemDecorator(box ext::deriving::expand_meta_deriving));
375409

376410
// Quasi-quoting expanders
377411
syntax_expanders.insert(intern("quote_tokens"),

src/libsyntax/ext/expand.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ fn expand_item(it: Gc<ast::Item>, fld: &mut MacroExpander)
251251

252252
match fld.cx.syntax_env.find(&intern(mname.get())) {
253253
Some(rc) => match *rc {
254-
ItemDecorator(dec_fn) => {
254+
ItemDecorator(ref dec) => {
255255
attr::mark_used(attr);
256256

257257
fld.cx.bt_push(ExpnInfo {
@@ -266,8 +266,7 @@ fn expand_item(it: Gc<ast::Item>, fld: &mut MacroExpander)
266266
// we'd ideally decorator_items.push_all(expand_item(item, fld)),
267267
// but that double-mut-borrows fld
268268
let mut items: SmallVector<Gc<ast::Item>> = SmallVector::zero();
269-
dec_fn(fld.cx, attr.span, attr.node.value, it,
270-
|item| items.push(item));
269+
dec.expand(fld.cx, attr.span, attr.node.value, it, |item| items.push(item));
271270
decorator_items.extend(items.move_iter()
272271
.flat_map(|item| expand_item(item, fld).move_iter()));
273272

@@ -328,7 +327,7 @@ fn expand_item_modifiers(mut it: Gc<ast::Item>, fld: &mut MacroExpander)
328327

329328
match fld.cx.syntax_env.find(&intern(mname.get())) {
330329
Some(rc) => match *rc {
331-
ItemModifier(dec_fn) => {
330+
ItemModifier(ref mac) => {
332331
attr::mark_used(attr);
333332
fld.cx.bt_push(ExpnInfo {
334333
call_site: attr.span,
@@ -338,7 +337,7 @@ fn expand_item_modifiers(mut it: Gc<ast::Item>, fld: &mut MacroExpander)
338337
span: None,
339338
}
340339
});
341-
it = dec_fn(fld.cx, attr.span, attr.node.value, it);
340+
it = mac.expand(fld.cx, attr.span, attr.node.value, it);
342341
fld.cx.bt_pop();
343342
}
344343
_ => unreachable!()

src/test/auxiliary/macro_crate_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
3636
reg.register_macro("identity", expand_identity);
3737
reg.register_syntax_extension(
3838
token::intern("into_foo"),
39-
ItemModifier(expand_into_foo));
39+
ItemModifier(box expand_into_foo));
4040
}
4141

4242
fn expand_make_a_1(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree])

0 commit comments

Comments
 (0)