Skip to content

Commit 94a5cdb

Browse files
authored
Merge pull request #14 from dtolnay/rustarg
Implement special case types in extern Rust argument position
2 parents 84f232e + 17955e2 commit 94a5cdb

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

gen/write.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,11 @@ fn write_rust_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) {
308308
write!(out, "&");
309309
}
310310
write!(out, "{}", arg.ident);
311+
match arg.ty {
312+
Type::RustBox(_) => write!(out, ".into_raw()"),
313+
Type::UniquePtr(_) => write!(out, ".release()"),
314+
_ => {}
315+
}
311316
}
312317
if indirect_return {
313318
if !efn.args.is_empty() {

macro/src/expand.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,21 @@ fn expand_rust_function_shim(namespace: &Namespace, efn: &ExternFn, types: &Type
231231
let args = efn.args.iter().map(|arg| expand_extern_arg(arg, types));
232232
let vars = efn.args.iter().map(|arg| {
233233
let ident = &arg.ident;
234-
if types.needs_indirect_abi(&arg.ty) {
234+
let var = if types.needs_indirect_abi(&arg.ty) {
235235
quote!(::std::ptr::read(#ident))
236236
} else {
237237
quote!(#ident)
238+
};
239+
match &arg.ty {
240+
Type::Ident(ident) if ident == "String" => quote!(#var.into_string()),
241+
Type::RustBox(_) => quote!(::std::boxed::Box::from_raw(#var)),
242+
Type::UniquePtr(_) => quote!(::cxx::UniquePtr::from_raw(#var)),
243+
Type::Ref(ty) => match &ty.inner {
244+
Type::Ident(ident) if ident == "String" => quote!(#var.as_string()),
245+
_ => var,
246+
},
247+
Type::Str(_) => quote!(#var.as_str()),
248+
_ => var,
238249
}
239250
});
240251
let mut outparam = None;

0 commit comments

Comments
 (0)