Separate the two uses of expand_extern_arg
These are going to need to diverge shortly. Indirect args can pass from
Rust to C++ as *const T from which C++ will do an unsafe ptr::read, but
need to pass from C++ to Rust as *mut T to leave a zero value in the old
location for when the C++ destructor runs.
diff --git a/macro/src/expand.rs b/macro/src/expand.rs
index 16dab26..3d6eee0 100644
--- a/macro/src/expand.rs
+++ b/macro/src/expand.rs
@@ -1,6 +1,6 @@
use crate::namespace::Namespace;
use crate::syntax::atom::Atom;
-use crate::syntax::{self, check, Api, ExternFn, ExternType, Struct, Type, Types, Var};
+use crate::syntax::{self, check, Api, ExternFn, ExternType, Struct, Type, Types};
use proc_macro2::{Ident, Span, TokenStream};
use quote::{format_ident, quote, quote_spanned};
use syn::{Error, ItemMod, Result, Token};
@@ -123,7 +123,15 @@
fn expand_cxx_function_decl(namespace: &Namespace, efn: &ExternFn, types: &Types) -> TokenStream {
let ident = &efn.ident;
- let args = efn.args.iter().map(|arg| expand_extern_arg(arg, types));
+ let args = efn.args.iter().map(|arg| {
+ let ident = &arg.ident;
+ let ty = expand_extern_type(&arg.ty);
+ if types.needs_indirect_abi(&arg.ty) {
+ quote!(#ident: *mut #ty)
+ } else {
+ quote!(#ident: #ty)
+ }
+ });
let ret = expand_extern_return_type(&efn.ret, types);
let mut outparam = None;
if indirect_return(&efn.ret, types) {
@@ -230,7 +238,15 @@
fn expand_rust_function_shim(namespace: &Namespace, efn: &ExternFn, types: &Types) -> TokenStream {
let ident = &efn.ident;
- let args = efn.args.iter().map(|arg| expand_extern_arg(arg, types));
+ let args = efn.args.iter().map(|arg| {
+ let ident = &arg.ident;
+ let ty = expand_extern_type(&arg.ty);
+ if types.needs_indirect_abi(&arg.ty) {
+ quote!(#ident: *mut #ty)
+ } else {
+ quote!(#ident: #ty)
+ }
+ });
let vars = efn.args.iter().map(|arg| {
let ident = &arg.ident;
match &arg.ty {
@@ -444,13 +460,3 @@
let ty = expand_extern_type(ret);
quote!(-> #ty)
}
-
-fn expand_extern_arg(arg: &Var, types: &Types) -> TokenStream {
- let ident = &arg.ident;
- let ty = expand_extern_type(&arg.ty);
- if types.needs_indirect_abi(&arg.ty) {
- quote!(#ident: *mut #ty)
- } else {
- quote!(#ident: #ty)
- }
-}