Skip to content

Instantly share code, notes, and snippets.

@ubnt-intrepid
Last active July 10, 2018 18:46
Show Gist options
  • Save ubnt-intrepid/60c6d116427c0d6db7bcc2c13c064798 to your computer and use it in GitHub Desktop.
Save ubnt-intrepid/60c6d116427c0d6db7bcc2c13c064798 to your computer and use it in GitHub Desktop.
#[proc_macro_derive(SomeTrait)]
pub fn derive_some_trait_from_def(item: TokenStream) -> TokenStream {
let item: syn::DeriveInput = syn::parse(item).unwrap();
...
}
#[proc_macro_attribute]
pub fn derive_some_trait_from_impl(attr: TokenStream, item: TokenStream) -> TokenStream {
let item: syn::ItemImpl = syn::parse(item).unwrap();
...
}
#[derive(SomeTrait)]
struct Foo {
x: u32,
y: u32,
}
#[derive_some_trait_from_impl]
impl Foo {
fn cartesian(&self) -> f64 {
let x = self.x as f64;
let y = self.y as f64;
(x*x + y*y).sqrt()
}
}
trait SomeTrait {
fn dump_schema(&self, w: &mut Write) -> fmt::Result;
}
#[proc_macro]
pub fn derive_some_trait(tts: TokenStream) -> TokenStream {
let CustomInput { def_, impl_ } = syn::parse(tts).unwrap();
...
}
derive_some_trait! {
#[derive(Debug, SomeTrait)]
struct Foo { x: u32, y: u32 }
#[derive_some_trait_from_impl]
impl Foo {
fn cartesian(&self) -> f64 { ... }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment