I've gotten stuck, when I tried to implement traits programmatically using syntax extensions.
I wrote a minimal example here. I hope someone can help (or point me in the right direction).
// Just some methods collection I want to implement programmatically.
trait TraitToHack {
fn implement_me(&self) -> int; // Say, I'd like to implement this method programmatically to return 42.
}
// I create dummy struct and wrap it with special attribute.
#[AttributeToHack(TraitToHack)]
struct StructToHack;
// I register syntax extension based on Decorator. Its signature is something like this:
// fn expand(cx: &mut ext::base::ExtCtxt, span: codemap::Span, mitem: &ast::MetaItem, item: &ast::Item, push: |P<ast::Item>|) { .. }
// This is where I got stuck :(
fn main() {
let hack = StructToHack;
hack.implement_me(); // Must return 42.
}
The questions are:
- how to obtain Trait identifier from the AST context?
- how to get method names from that identifier and how to implement them using syntax extension?