From a46a237fe7a9391a21b8251b3ff8ab4ceef2f461 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Fri, 6 Mar 2020 10:03:48 -0800 Subject: [PATCH] Make passing String by value to C++ const --- gen/write.rs | 5 +++++ macro/src/expand.rs | 6 ++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/gen/write.rs b/gen/write.rs index e6fb4e9a..33898ce5 100644 --- a/gen/write.rs +++ b/gen/write.rs @@ -169,6 +169,9 @@ fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) { if i > 0 { write!(out, ", "); } + if arg.ty == RustString { + write!(out, "const "); + } write_extern_arg(out, arg, types); } if indirect_return { @@ -213,6 +216,8 @@ fn write_cxx_function_shim(out: &mut OutFile, efn: &ExternFn, types: &Types) { } else if let Type::UniquePtr(_) = &arg.ty { write_type(out, &arg.ty); write!(out, "({})", arg.ident); + } else if arg.ty == RustString { + write!(out, "::rust::String(::rust::unsafe_bitcopy, *{})", arg.ident); } else if types.needs_indirect_abi(&arg.ty) { write!(out, "::std::move(*{})", arg.ident); } else { diff --git a/macro/src/expand.rs b/macro/src/expand.rs index 1237892b..9ebb6235 100644 --- a/macro/src/expand.rs +++ b/macro/src/expand.rs @@ -126,7 +126,9 @@ fn expand_cxx_function_decl(namespace: &Namespace, efn: &ExternFn, types: &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) { + if arg.ty == RustString { + quote!(#ident: *const #ty) + } else if types.needs_indirect_abi(&arg.ty) { quote!(#ident: *mut #ty) } else { quote!(#ident: #ty) @@ -157,7 +159,7 @@ fn expand_cxx_function_shim(namespace: &Namespace, efn: &ExternFn, types: &Types let var = &arg.ident; match &arg.ty { Type::Ident(ident) if ident == RustString => { - quote!(#var.as_mut_ptr() as *mut ::cxx::private::RustString) + quote!(#var.as_mut_ptr() as *const ::cxx::private::RustString) } Type::RustBox(_) => quote!(::std::boxed::Box::into_raw(#var)), Type::UniquePtr(_) => quote!(::cxx::UniquePtr::into_raw(#var)),