Test returning unique_ptr<string> from Rust to C++
diff --git a/tests/ffi/lib.rs b/tests/ffi/lib.rs
index 2df3337..c709678 100644
--- a/tests/ffi/lib.rs
+++ b/tests/ffi/lib.rs
@@ -41,7 +41,7 @@
fn r_return_ref(shared: &Shared) -> &usize;
fn r_return_str(shared: &Shared) -> &str;
fn r_return_rust_string() -> String;
- //TODO fn r_return_unique_ptr_string() -> UniquePtr<CxxString>;
+ fn r_return_unique_ptr_string() -> UniquePtr<CxxString>;
fn r_take_primitive(n: usize);
fn r_take_shared(shared: Shared);
@@ -89,6 +89,13 @@
"2020".to_owned()
}
+fn r_return_unique_ptr_string() -> UniquePtr<CxxString> {
+ extern "C" {
+ fn cxx_test_suite_get_unique_ptr_string() -> *mut CxxString;
+ }
+ unsafe { UniquePtr::from_raw(cxx_test_suite_get_unique_ptr_string()) }
+}
+
fn r_take_primitive(n: usize) {
assert_eq!(n, 2020);
}
diff --git a/tests/ffi/tests.cc b/tests/ffi/tests.cc
index c7d3c87..350cd7d 100644
--- a/tests/ffi/tests.cc
+++ b/tests/ffi/tests.cc
@@ -94,6 +94,10 @@
return std::unique_ptr<C>(new C{2020}).release();
}
+extern "C" std::string *cxx_test_suite_get_unique_ptr_string() noexcept {
+ return std::unique_ptr<std::string>(new std::string("2020")).release();
+}
+
extern "C" const char *cxx_run_test() noexcept {
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
@@ -111,6 +115,7 @@
ASSERT(r_return_ref(Shared{2020}) == 2020);
ASSERT(std::string(r_return_str(Shared{2020})) == "2020");
ASSERT(std::string(r_return_rust_string()) == "2020");
+ ASSERT(*r_return_unique_ptr_string() == "2020");
r_take_primitive(2020);
r_take_shared(Shared{2020});