Make passing String by value to C++ const

This commit is contained in:
David Tolnay 2020-03-06 10:03:48 -08:00
parent d1e2efc847
commit a46a237fe7
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
2 changed files with 9 additions and 2 deletions

View File

@ -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 {

View File

@ -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)),