Pointer arithmetic with Builtin.RawPointer?

Yes, the documentation for load states that it will copy the memory to initialise a new, independent T.

As for raw/typed pointer, unfortunately we don't really have great high-level documentation about it. The official language guide doesn't mention the distinction at all, and I couldn't find any design documents in the compiler source repository. If you go back to the Swift 3 migration notes though, it does say this is okay:

In general, developer’s should not make layout assumptions. However, some “obvious” cases can be safely assumed, including homogeneous arrays and tuples, and structs with homogeneous stored properties. Imported C structs naturally follow the layout rules of the platform’s C ABI.

As you've seen though, the interface is more awkward once you need to add byte-offsets and read values of different types. This kind of parsing of binary layouts is largely why the raw APIs exist.

Also check out @Andrew_Trick's recent post on correct use of these APIs.


You can use withUnsafe(Mutable)Pointer(to: inout T) on the tuple, transform it in to a fixed-capacity buffer as shown here, then use String.init(cString:).

1 Like