Feature Request: Borrowing `std.shared_ptr.pointee`

I often use std::shared_ptr with types that should not be copied.

For example, I have a heavy type from a different library (ArrayBuffer) that can (or should) only be used inside a std::shared_ptr.

When passing this to Swift to call methods on it, I am currently forced to make multiple copies of that type:

func doSum(val: shared_ptr_array_buffer) {
  val.pointee.calculate()
  val.pointee.calculateAgain()
  val.pointee.andCalculateAgain()
  // 3x copy + destructor of ArrayBuffer
}

It would be great to somehow access .pointee without actually copying it - i.e. get const ArrayBuffer& (or borrowing ArrayBuffer/inout ArrayBuffer) instead of ArrayBuffer from Swift.

Is this even possible?

I currently only see two workarounds:

  1. Make ArrayBuffer SWIFT_NONCOPYABLE - but this doesn't work if it's an external type
  2. Add an ArrayBufferHolder type that holds std::shared_ptr<ArrayBuffer> and exposes wrapper methods for each method on ArrayBuffer - lots of work.
5 Likes

I'd also like to know this. I recently started using C++ from Swift and found very quickly that calling .pointee on the std.shared_ptr created copies of the value instead of borrowing it. To work around it I created a function in C++ that takes the std.shared_ptr and calls a function that I need on the pointer...but it's not ideal.

1 Like