I'm working on a suite of memory mappable data structures. I'm wondering if I have to bind their memory eagerly, or if I'm allowed to do it lazily. The latter is drastically simpler and removes any need for manual memory binding APIs. The former separates initialization into at least three categories and is comparatively dreadful in every which way you can think of.
Question: Is repeatedly binding memory to a fixed type well defined behavior in Swift?
extension MyLazilyMemoryBindingArray {
subscript(index: Int) -> Element {
unsafeAddress {
startOfElements.advanced(by: index).bindMemory(to: Element.self, capacity: 1)
}
}
}
The documentation for UnsafeMutableRawPointer/bindMemory(to:capacity:) only warns about accessing unrelated types. Meanwhile, your favorite AI insists that rebinding T to T is undefined and super scary according to fake.quotes.com. I read the following statement (and the lack of other disclaimers) as a thumbs up…?
A memory location may only be bound to one type at a time. The behavior of accessing memory as a type unrelated to its bound type is undefined.