diff --git a/gen/src/write.rs b/gen/src/write.rs index c2d79c0e5..7f60501a8 100644 --- a/gen/src/write.rs +++ b/gen/src/write.rs @@ -1776,6 +1776,18 @@ fn write_shared_ptr(out: &mut OutFile, key: NamedImplKey) { writeln!(out, "}}"); } begin_function_definition(out); + writeln!( + out, + "void cxxbridge1$shared_ptr${}$from_unmanaged(::std::shared_ptr<{}>* ptr, void* data) noexcept {{", + instance, inner, + ); + writeln!( + out, + "new (ptr) std::shared_ptr<{}>(static_cast<{}*>(data));", + inner, inner + ); + writeln!(out, "}}"); + begin_function_definition(out); writeln!( out, "void cxxbridge1$shared_ptr${}$clone(const ::std::shared_ptr<{}>& self, ::std::shared_ptr<{}> *ptr) noexcept {{", diff --git a/tests/test.rs b/tests/test.rs index 5d2e58fa9..5ba1cc49b 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -281,7 +281,7 @@ fn test_shared_ptr_weak_ptr() { } #[test] -fn test_unique_to_shared_ptr() { +fn test_unique_to_shared_ptr_string() { let unique = ffi::c_return_unique_ptr_string(); let ptr = &*unique as *const _; let shared = unique.to_shared(); @@ -289,6 +289,14 @@ fn test_unique_to_shared_ptr() { assert_eq!(&*shared, "2020"); } +#[test] +fn test_unique_to_shared_ptr_cpp_type() { + let unique = ffi::c_return_unique_ptr(); + let ptr = &*unique as *const _; + let shared = unique.to_shared(); + assert_eq!(&*shared as *const _, ptr); +} + #[test] fn test_c_ns_method_calls() { let unique_ptr = ffi2::ns_c_return_unique_ptr_ns();