Allow to define a proc macro as
fn(TokenStream, TokenStream, &Source) -> TokenStream where
fn get_item(item: &[String]) -> Option<TokenStream>, which returns the definition of
source.get_item(&["std", "option", "Option"]) would return the definition of
Option type. It’d work with any type or trait in any crate.
Proc macro is invoked as function-like macro:
There is delegation RFC, Inherent trait implementation RFC. Both request to add new syntax to the language which could be implemented as a proc macro, if this feature was available today. One could e.g. write
delegate!(MyTrait to MyType::inner) to delegate the
inner field of
MyType. Similarly, one could write
inherent!(MyType, MyTrait) to make trait methods of
MyTriat inherent to
Further, it’d help when writing (de)serializers. Let’s say I have my own
MySerialize trait and associated derive macro. The crate implementing derive macro could also provide
foreign_derive!() one, and reuse the code of classic derive macro. Then the crate defining
MySerialize could easily implement it for types from other crates.
Similarly, a macro using newtype pattern could implement foreign traits for “foreign” types.
This would allow us experiment with delegation, inherent impls and possibly other ideas the same way we could experiment with
try!() macro before implementing
What do you think? Is this approach feasible? Do you see some problems? Anyone willing to help me with writing proper RFC?