Skip to content

Commit d5642f7

Browse files
authored
Add descriptive doc comments to withMemoryRebound(to:capacity:_:) and… (#5072)
* Add descriptive doc comments to withMemoryRebound(to:capacity:_:) and bindMemory(to:capacity:). SR-2480: Improve documentation for withMemoryRebound(to:capacity:_:) rdar://problem/28440528 * Nate's feedback
1 parent 9af74fe commit d5642f7

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

stdlib/public/core/UnsafePointer.swift.gyb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,12 +366,25 @@ public struct ${Self}<Pointee>
366366
% end
367367

368368
/// Rebinds memory at `self` to type `T` with capacity to hold `count`
369-
/// adjacent `T` values while executing the `body` closure. After executing
370-
/// the closure, rebinds memory back to `Pointee`.
369+
/// adjacent `T` values while executing the `body` closure.
370+
///
371+
/// After executing the closure, rebinds memory back to `Pointee`.
371372
///
372373
/// - Precondition: Type 'T' is layout compatible with type 'Pointee'.
373374
/// - Precondition: The memory `self..<self + count * MemoryLayout<T>.stride`
374375
/// is bound to `Pointee`.
376+
///
377+
/// Accessing `${Self}<T>.pointee` requires that the memory be "bound" to
378+
/// type `T`. A memory location may only be bound to one type at a time, so
379+
/// accessing the same memory as an unrelated type without first rebinding the
380+
/// memory is undefined. `self` may not be accessed within the `body` closure
381+
/// because memory is no longer bound to `Pointee` while it executes. The
382+
/// closure's `${Self}<T>` argument must not escape the closure because memory
383+
/// is only temporarily bound to `T`.
384+
///
385+
/// To persistently bind this memory to a different type, first obtain a
386+
/// raw pointer to the memory, then invoke the `bindMemory` API:
387+
/// `UnsafeRawPointer(typedPointer).bindMemory(to:capacity:)`.
375388
public func withMemoryRebound<T, Result>(to: T.Type, capacity count: Int,
376389
_ body: (${Self}<T>) throws -> Result
377390
) rethrows -> Result {

stdlib/public/core/UnsafeRawPointer.swift.gyb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,9 @@ public struct Unsafe${Mutable}RawPointer : Strideable, Hashable, _Pointer {
173173
/// that is layout compatible with `T`.
174174
/// - Postcondition: The memory is bound to 'T' starting at `self` continuing
175175
/// through `self` + `count` * `MemoryLayout<T>.stride`
176-
/// - Warning: Binding memory to a type is potentially undefined if the
177-
/// memory is ever accessed as an unrelated type.
176+
/// - Warning: A memory location may only be bound to one type at a time.
177+
/// The behavior of accessing memory as type `U` while it is bound to an
178+
/// unrelated type `T` is undefined.
178179
@_transparent
179180
@discardableResult
180181
public func bindMemory<T>(to type: T.Type, capacity count: Int)

0 commit comments

Comments
 (0)