2
구문 확장을 사용하여 프로그래밍 방식으로 특성을 구현하려고 시도했지만 막혔습니다.녹 구조 확장 및 특성
나는 여기에 최소한의 예를 썼다. 누군가가 도울 수 있기를 바랍니다. (또는 올바른 방향으로 나를 가리켜 주길 바랍니다.)
// 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.
}
질문은 다음과 같습니다
- 방법 AST 컨텍스트에서 형질 식별자를 얻기 위해?
- 식별자에서 메소드 이름을 가져 오는 방법과 구문 확장을 사용하여 메소드 이름을 구현하는 방법은 무엇입니까?