SharedPtr<T> does not support T being an opaque Rust type. You should use a
Box<T> (C++ rust::Box<T>) instead for transferring ownership of
opaque Rust types on the language boundary.
// src/main.rsuse std::ops::Deref;
use std::ptr;
#[cxx::bridge]mod ffi {
unsafeextern"C++" {
include!("example/include/example.h");
typeObject;
fncreate_shared_ptr() -> SharedPtr<Object>;
}
}
fnmain() {
let ptr1 = ffi::create_shared_ptr();
{
// Create a second shared_ptr holding shared ownership of the same// object. There is still only one Object but two SharedPtr<Object>.// Both pointers point to the same object on the heap.let ptr2 = ptr1.clone();
assert!(ptr::eq(ptr1.deref(), ptr2.deref()));
// ptr2 goes out of scope, but Object is not destroyed yet.
}
println!("say goodbye to Object");
// ptr1 goes out of scope and Object is destroyed.
}