例えば foo というパッケージが公開する諸々に依存したコードを生成するマクロを作りたい場合を考える。まず、foo-derive とは別にコード生成の実体および公開 API を持つ foo-codegen を用意し、foo-derive は foo-codegen が公開している API を用いるだけにしておく。
別のクレート bar の作者は、トレイト foo::Foo を bar::Bar として再エクスポートしたいとする。このとき、foo::Foo に付随しているcustom derive を再エクスポートするのではなく、foo-codegen から公開されているAPIを呼び出すcustom derive を自前で用意するようにする。
-
-
// foo/lib.rs pub trait Foo { .. } // foo-derive/lib.rs #[proc_macro_derive(Bar)] pub fn derive_bar(input:..) -> .. { foo_codegen::parse(input).generate() } // foo-codegen/lib.rs (omit)
Show this thread -
// bar/lib.rs pub use foo::Foo as Bar; // bar-derive/lib.rs #[proc_macro_derive(Bar)] pub fn derive_bar(input:..) -> .. { foo_codegen::parse(input) .trait_path("bar::Bar") .generate() }
Show this thread
End of conversation
New conversation -
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.