dtolnay / cxx

Safe interop between Rust and C++
https://cxx.rs
Apache License 2.0
5.68k stars 320 forks source link

Is there a way to expose a C++ class that is itself a wrapper on `std::shared_ptr`? #1359

Open lenianiva opened 2 days ago

lenianiva commented 2 days ago

For example, consider a class like this:

namespace internal {
  class Result;
}
class Result {
public:
  Result();
private:
  std::shared_ptr<internal::Result> d_result;
};

Is there a way to expose this in Rust in a way equivalent to

struct Result {
  p: SharedPtr<Opaque>,
}

An implementation like this example gives the error "type Result should be trivially move constructible and trivially destructible in C++ to be used as a return value of newResult in Rust"

I cannot change the C++ header and I don't want to add another layer of indirection by wrapping Result as an opaque class in a shared pointer.