Rename ToFfi to IntoFfi

This commit is contained in:
2025-06-24 11:42:11 +10:00
parent cadf0a8551
commit 45db380466
6 changed files with 176 additions and 193 deletions

View File

@@ -60,7 +60,8 @@ fn generate_type(ty: &Ident) -> Result<TokenStream> {
}
}
unsafe impl #ffi::ToFfi for #ty {
// SAFETY: we can always implement `IntoFfi` because it transfers ownership from Rust to Lua
unsafe impl #ffi::IntoFfi for #ty {
type To = Self;
fn convert(self) -> Self::To { self }
}

View File

@@ -231,8 +231,8 @@ fn generate_ffi_wrapper(func: &FfiFunction) -> Result<TokenStream> {
match get_ffi_arg_type(ty) {
FfiArgType::Default => {
params.push(quote! { #name: <#ty as #ffi::FromFfi>::FromArg });
args.push(quote! { <#ty as #ffi::FromFfi>::convert_arg(#name) });
params.push(quote! { #name: <#ty as #ffi::FromFfi>::From });
args.push(quote! { <#ty as #ffi::FromFfi>::convert(#name) });
}
}
}
@@ -240,19 +240,22 @@ fn generate_ffi_wrapper(func: &FfiFunction) -> Result<TokenStream> {
let (ret, call) = if func.ret_by_out {
// make return by out-param the first parameter
let ret = &func.ret;
params.insert(0, quote! { out: *mut #ret });
params.insert(0, quote! { out: *mut <#ret as #ffi::IntoFfi>::To });
(
quote!(()),
quote! { ::std::ptr::write(out, Self::#name(#(#args),*)) },
quote! { ::std::ptr::write(out, <#ret as #ffi::IntoFfi>::convert(Self::#name(#(#args),*))) },
)
} else {
let ret = &func.ret;
(quote! { #ret }, quote! { Self::#name(#(#args),*) })
(
quote! { <#ret as #ffi::IntoFfi>::To },
quote! { <#ret as #ffi::IntoFfi>::convert(Self::#name(#(#args),*)) },
)
};
Ok(quote! {
#[unsafe(export_name = #c_name)]
unsafe extern "C" fn #rust_name(#(#params),*) -> #ret { unsafe { #call } }
unsafe extern "C" fn #rust_name(#(#params),*) -> #ret { #call }
})
}
@@ -275,7 +278,7 @@ fn generate_ffi_register(func: &FfiFunction) -> Result<TokenStream> {
match get_ffi_arg_type(ty) {
FfiArgType::Default => {
params.push(quote! { <#ty as #ffi::FromFfi>::FromArg });
params.push(quote! { <#ty as #ffi::FromFfi>::From });
register.push(quote! { b.param::<#ty>(#name); })
}
};
@@ -291,9 +294,9 @@ fn generate_ffi_register(func: &FfiFunction) -> Result<TokenStream> {
};
let declare = if func.ret_by_out {
quote! { b.declare::<#ffi::UnsafeExternCFn<(*mut #ret, #(#params,)*), ()>>(#c_name); }
quote! { b.declare::<#ffi::UnsafeExternCFn<(*mut <#ret as #ffi::IntoFfi>::To, #(#params,)*), ()>>(#c_name); }
} else {
quote! { b.declare::<#ffi::UnsafeExternCFn<(#(#params,)*), #ret>>(#c_name); }
quote! { b.declare::<#ffi::UnsafeExternCFn<(#(#params,)*), <#ret as #ffi::IntoFfi>::To>>(#c_name); }
};
let register = match func.attrs.metatable {