Last active
July 10, 2018 18:46
-
-
Save ubnt-intrepid/60c6d116427c0d6db7bcc2c13c064798 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[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