Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix return Result<Box<T>> from Rust to C++ #310

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions gen/src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -775,9 +775,11 @@ fn write_rust_function_shim_impl(
write!(out, "extern$");
}
write!(out, ")");
if let Some(ret) = &sig.ret {
if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
write!(out, ")");
if !indirect_return {
if let Some(ret) = &sig.ret {
if let Type::RustBox(_) | Type::UniquePtr(_) = ret {
write!(out, ")");
}
}
}
writeln!(out, ";");
Expand Down
37 changes: 21 additions & 16 deletions macro/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,42 +580,47 @@ fn expand_rust_function_shim_impl(
};
call.extend(quote! { (#(#vars),*) });

let mut expr = sig
let conversion = sig
.ret
.as_ref()
.and_then(|ret| match ret {
Type::Ident(ident) if ident == RustString => {
Some(quote!(::cxx::private::RustString::from(#call)))
Some(quote!(::cxx::private::RustString::from))
}
Type::RustBox(_) => Some(quote!(::std::boxed::Box::into_raw(#call))),
Type::RustBox(_) => Some(quote!(::std::boxed::Box::into_raw)),
Type::RustVec(vec) => {
if vec.inner == RustString {
Some(quote!(::cxx::private::RustVec::from_vec_string(#call)))
Some(quote!(::cxx::private::RustVec::from_vec_string))
} else {
Some(quote!(::cxx::private::RustVec::from(#call)))
Some(quote!(::cxx::private::RustVec::from))
}
}
Type::UniquePtr(_) => Some(quote!(::cxx::UniquePtr::into_raw(#call))),
Type::UniquePtr(_) => Some(quote!(::cxx::UniquePtr::into_raw)),
Type::Ref(ty) => match &ty.inner {
Type::Ident(ident) if ident == RustString => match ty.mutability {
None => Some(quote!(::cxx::private::RustString::from_ref(#call))),
Some(_) => Some(quote!(::cxx::private::RustString::from_mut(#call))),
None => Some(quote!(::cxx::private::RustString::from_ref)),
Some(_) => Some(quote!(::cxx::private::RustString::from_mut)),
},
Type::RustVec(vec) if vec.inner == RustString => match ty.mutability {
None => Some(quote!(::cxx::private::RustVec::from_ref_vec_string(#call))),
Some(_) => Some(quote!(::cxx::private::RustVec::from_mut_vec_string(#call))),
None => Some(quote!(::cxx::private::RustVec::from_ref_vec_string)),
Some(_) => Some(quote!(::cxx::private::RustVec::from_mut_vec_string)),
},
Type::RustVec(_) => match ty.mutability {
None => Some(quote!(::cxx::private::RustVec::from_ref(#call))),
Some(_) => Some(quote!(::cxx::private::RustVec::from_mut(#call))),
None => Some(quote!(::cxx::private::RustVec::from_ref)),
Some(_) => Some(quote!(::cxx::private::RustVec::from_mut)),
},
_ => None,
},
Type::Str(_) => Some(quote!(::cxx::private::RustStr::from(#call))),
Type::SliceRefU8(_) => Some(quote!(::cxx::private::RustSliceU8::from(#call))),
Type::Str(_) => Some(quote!(::cxx::private::RustStr::from)),
Type::SliceRefU8(_) => Some(quote!(::cxx::private::RustSliceU8::from)),
_ => None,
})
.unwrap_or(call);
});

let mut expr = match (conversion, sig.throws) {
(None, false) | (None, true) => call,
(Some(conversion), false) => quote!(#conversion(#call)),
(Some(conversion), true) => quote!(::std::result::Result::map(#call, #conversion)),
};

let mut outparam = None;
let indirect_return = indirect_return(sig, types);
Expand Down
5 changes: 5 additions & 0 deletions tests/ffi/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ pub mod ffi {

fn r_try_return_void() -> Result<()>;
fn r_try_return_primitive() -> Result<usize>;
fn r_try_return_box() -> Result<Box<R>>;
fn r_fail_return_primitive() -> Result<usize>;

fn r_return_r2(n: usize) -> Box<R2>;
Expand Down Expand Up @@ -334,6 +335,10 @@ fn r_try_return_primitive() -> Result<usize, Error> {
Ok(2020)
}

fn r_try_return_box() -> Result<Box<R>, Error> {
Ok(Box::new(2020))
}

fn r_fail_return_primitive() -> Result<usize, Error> {
Err(Error)
}
Expand Down