Trivial type de/initialization?

Is trivial type de/initialization required? My understanding is that these operations do nothing when a pointer's Pointee is a trivial type, as commonly expressed by the "[...] or the pointer's Pointee must be a trivial type" phrase in UnsafeMutable[Buffer]Pointer's documentation. I'm unsure, however, how far this line of reasoning extends. Let's use String's UTF-8 initializer as an example:

let hello = String(unsafeUninitializedCapacity: 5) { buffer in
    buffer.initialize(fromContentsOf: "hello".utf8)
}

let world = String(unsafeUninitializedCapacity: 5) { buffer in
    buffer.update(fromContentsOf: "world".utf8)
}

The "unsafeUninitializedCapacity" label and "[...] initializes that memory, and returns the number of initialized elements" closure documentation leads me to believe that only the string with initialized elements is valid. This conclusion clashes with my mental model, where both are valid because UInt8 is a trivial type. If somebody intimately familiar with Swift's memory model can confirm or deny their validity - that would be swell.

1 Like

I think the following reply may help:

1 Like