Api traits test code (#487)

Add initial templated API test support. This needs to be improved to use an attribute macro, but that will require some major surgery :(
This commit is contained in:
aiwhskruht 2025-09-02 08:57:54 -07:00 committed by GitHub
commit 4752fcdcf2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 115 additions and 4 deletions

View file

@ -10,7 +10,7 @@ use syn::punctuated::Punctuated;
use syn::visit_mut::VisitMut;
use syn::{
bracketed, parse_macro_input, File, ForeignItem, ForeignItemFn, Ident, Item, Path, Signature,
Token,
Token, token
};
const CUDA_RS: &'static str = include_str! {"cuda.rs"};
@ -308,3 +308,40 @@ fn join(
}
}
#[proc_macro]
pub fn generate_api_macro(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as ApiMacroInput);
let ApiMacroInput(_, trait_name, _, type_name, _, macro_name) = input;
let expanded = quote! {
struct #type_name;
macro_rules! #macro_name {
($($abi:literal fn $fn_name:ident( $( $arg_id:ident : $arg_type:ty ),* ) -> $ret_type:ty;)*) => {
impl #trait_name for #type_name {
fn new() -> Self { Self }
$(
#[inline(always)]
fn $fn_name(&self, $( $arg_id : $arg_type ),* ) -> $ret_type {
unsafe { super::$fn_name( $( $arg_id ),* ) }
}
)*
}
};
}
};
TokenStream::from(expanded)
}
#[allow(dead_code)]
struct ApiMacroInput(token::Impl, Path, token::For, Path, token::Use, Ident);
impl syn::parse::Parse for ApiMacroInput {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
Ok(ApiMacroInput(
input.parse()?, // impl
input.parse()?, // trait
input.parse()?, // for
input.parse()?, // type
input.parse()?, // using
input.parse()? // macro
))
}
}