Skip to content

Instantly share code, notes, and snippets.

@cmsd2
Created July 26, 2019 17:32
Show Gist options
  • Save cmsd2/cc2013a01a9318ca3e9a53ef56cf9b5a to your computer and use it in GitHub Desktop.
Save cmsd2/cc2013a01a9318ca3e9a53ef56cf9b5a to your computer and use it in GitHub Desktop.
rust procedural macro to generate symbol resolver helper functions
extern crate proc_macro;
use proc_macro::TokenStream;
use proc_macro2::{Ident, Span};
use syn::{ItemType, LitStr};
use quote::quote;
#[proc_macro_attribute]
pub fn hello(_attr: TokenStream, item: TokenStream) -> TokenStream {
let input = syn::parse_macro_input!(item as ItemType);
let fn_name_str = format!("resolve_{}", input.ident);
let fn_name = Ident::new(&fn_name_str, Span::call_site());
let fn_type = input.ty.clone();
let fn_name_lit_str = LitStr::new(&fn_name_str, Span::call_site());
let result = quote! {
#input
unsafe fn #fn_name() -> Option<#fn_type> {
unimplemented!()
}
};
result.into()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment