about summary refs log tree commit diff
path: root/src/libsyntax/ext/base.rs
diff options
context:
space:
mode:
authorSteven Fackler <sfackler@gmail.com>2014-09-09 23:57:14 -0700
committerSteven Fackler <sfackler@gmail.com>2014-09-10 09:15:13 -0700
commit313cb8acaea05e703441bd5adb92aacce5bf6411 (patch)
tree66f774dcb2ed082983adeea86d26bd23175a1678 /src/libsyntax/ext/base.rs
parent6ceb9b4157a076977b0d782632960bb90a0d39d9 (diff)
downloadrust-313cb8acaea05e703441bd5adb92aacce5bf6411.tar.gz
rust-313cb8acaea05e703441bd5adb92aacce5bf6411.zip
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]
Diffstat (limited to 'src/libsyntax/ext/base.rs')
-rw-r--r--src/libsyntax/ext/base.rs48
1 files changed, 41 insertions, 7 deletions
diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs
index d59b20dfc4c..43be3c227ac 100644
--- a/src/libsyntax/ext/base.rs
+++ b/src/libsyntax/ext/base.rs
@@ -39,11 +39,45 @@ pub struct MacroDef {
     pub ext: SyntaxExtension
 }
 
-pub type ItemDecorator =
-    fn(&mut ExtCtxt, Span, Gc<ast::MetaItem>, Gc<ast::Item>, |Gc<ast::Item>|);
+pub trait ItemDecorator {
+    fn expand(&self,
+              ecx: &mut ExtCtxt,
+              sp: Span,
+              meta_item: Gc<ast::MetaItem>,
+              item: Gc<ast::Item>,
+              push: |Gc<ast::Item>|);
+}
+
+impl ItemDecorator for fn(&mut ExtCtxt, Span, Gc<ast::MetaItem>, Gc<ast::Item>, |Gc<ast::Item>|) {
+    fn expand(&self,
+              ecx: &mut ExtCtxt,
+              sp: Span,
+              meta_item: Gc<ast::MetaItem>,
+              item: Gc<ast::Item>,
+              push: |Gc<ast::Item>|) {
+        (*self)(ecx, sp, meta_item, item, push)
+    }
+}
 
-pub type ItemModifier =
-    fn(&mut ExtCtxt, Span, Gc<ast::MetaItem>, Gc<ast::Item>) -> Gc<ast::Item>;
+pub trait ItemModifier {
+    fn expand(&self,
+              ecx: &mut ExtCtxt,
+              span: Span,
+              meta_item: Gc<ast::MetaItem>,
+              item: Gc<ast::Item>)
+              -> Gc<ast::Item>;
+}
+
+impl ItemModifier for fn(&mut ExtCtxt, Span, Gc<ast::MetaItem>, Gc<ast::Item>) -> Gc<ast::Item> {
+    fn expand(&self,
+              ecx: &mut ExtCtxt,
+              span: Span,
+              meta_item: Gc<ast::MetaItem>,
+              item: Gc<ast::Item>)
+              -> Gc<ast::Item> {
+        (*self)(ecx, span, meta_item, item)
+    }
+}
 
 pub struct BasicMacroExpander {
     pub expander: MacroExpanderFn,
@@ -281,11 +315,11 @@ pub enum SyntaxExtension {
     /// based upon it.
     ///
     /// `#[deriving(...)]` is an `ItemDecorator`.
-    ItemDecorator(ItemDecorator),
+    ItemDecorator(Box<ItemDecorator + 'static>),
 
     /// A syntax extension that is attached to an item and modifies it
     /// in-place.
-    ItemModifier(ItemModifier),
+    ItemModifier(Box<ItemModifier + 'static>),
 
     /// A normal, function-like syntax extension.
     ///
@@ -371,7 +405,7 @@ fn initial_syntax_expander_table() -> SyntaxEnv {
                             builtin_normal_expander(
                                     ext::log_syntax::expand_syntax_ext));
     syntax_expanders.insert(intern("deriving"),
-                            ItemDecorator(ext::deriving::expand_meta_deriving));
+                            ItemDecorator(box ext::deriving::expand_meta_deriving));
 
     // Quasi-quoting expanders
     syntax_expanders.insert(intern("quote_tokens"),