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.
1 Like