-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from NLnetLabs/function-macro
Add macro for roto functions to reduce some boilerplate
- Loading branch information
Showing
18 changed files
with
377 additions
and
233 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[package] | ||
name = "roto-macros" | ||
version.workspace = true | ||
edition.workspace = true | ||
authors.workspace = true | ||
rust-version.workspace = true | ||
license.workspace = true | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
proc-macro2 = "1.0.86" | ||
quote = "1.0.37" | ||
syn = { version = "2.0.77", features = ["full"] } |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../LICENSE |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use proc_macro::TokenStream; | ||
use quote::quote; | ||
use syn::{parse_macro_input, ItemFn}; | ||
|
||
/// | ||
/// | ||
/// ```rust,no_run | ||
/// fn foo(a1: A1, a2: A2) -> Ret { | ||
/// /* ... */ | ||
/// } | ||
/// ``` | ||
/// | ||
/// | ||
#[proc_macro_attribute] | ||
pub fn roto_function(_attr: TokenStream, item: TokenStream) -> TokenStream { | ||
let input = parse_macro_input!(item as ItemFn); | ||
|
||
let ItemFn { | ||
attrs, | ||
vis, | ||
sig, | ||
block: _, | ||
} = input.clone(); | ||
|
||
assert!(sig.unsafety.is_none()); | ||
assert!(sig.generics.params.is_empty()); | ||
assert!(sig.generics.where_clause.is_none()); | ||
assert!(sig.variadic.is_none()); | ||
|
||
let ident = sig.ident; | ||
let args = sig.inputs.iter().map(|i| { | ||
let syn::FnArg::Typed(syn::PatType { pat, .. }) = i else { | ||
panic!() | ||
}; | ||
pat | ||
}); | ||
|
||
let inputs = sig.inputs.clone().into_iter(); | ||
let ret = match sig.output { | ||
syn::ReturnType::Default => quote!(()), | ||
syn::ReturnType::Type(_, t) => quote!(#t), | ||
}; | ||
|
||
let input_types: Vec<_> = sig | ||
.inputs | ||
.clone() | ||
.into_iter() | ||
.map(|i| { | ||
let syn::FnArg::Typed(syn::PatType { ty, .. }) = i else { | ||
panic!() | ||
}; | ||
ty | ||
}) | ||
.collect(); | ||
|
||
let arg_types = quote!(*mut #ret, #(#input_types,)*); | ||
|
||
let expanded = quote! { | ||
#[allow(non_upper_case_globals)] | ||
#vis const #ident: extern "C" fn(#arg_types) = { | ||
#(#attrs)* | ||
extern "C" fn #ident ( out: *mut #ret, #(#inputs,)* ) { | ||
#input | ||
|
||
unsafe { *out = #ident(#(#args),*) }; | ||
} | ||
|
||
#ident as extern "C" fn(#arg_types) | ||
}; | ||
}; | ||
|
||
TokenStream::from(expanded) | ||
} |
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
Oops, something went wrong.