Skip to content

Commit 390cebe

Browse files
authored
Merge pull request #36004 from lorentey/document-copyContents
[stdlib] Document a fatal surprise with `Sequence._copyContents`
2 parents 3fa7590 + aede1cc commit 390cebe

File tree

1 file changed

+44
-7
lines changed

1 file changed

+44
-7
lines changed

stdlib/public/core/Sequence.swift

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,29 @@ public protocol Sequence {
357357
/// in the same order.
358358
__consuming func _copyToContiguousArray() -> ContiguousArray<Element>
359359

360-
/// Copy `self` into an unsafe buffer, returning a partially-consumed
361-
/// iterator with any elements that didn't fit remaining.
360+
/// Copy `self` into an unsafe buffer, initializing its memory.
361+
///
362+
/// The default implementation simply iterates over the elements of the
363+
/// sequence, initializing the buffer one item at a time.
364+
///
365+
/// For sequences whose elements are stored in contiguous chunks of memory,
366+
/// it may be more efficient to copy them in bulk, using the
367+
/// `UnsafeMutablePointer.initialize(from:count:)` method.
368+
///
369+
/// - Parameter ptr: An unsafe buffer addressing uninitialized memory. The
370+
/// buffer must be of sufficient size to accommodate
371+
/// `source.underestimatedCount` elements. (Some implementations trap
372+
/// if given a buffer that's smaller than this.)
373+
///
374+
/// - Returns: `(it, c)`, where `c` is the number of elements copied into the
375+
/// buffer, and `it` is a partially consumed iterator that can be used to
376+
/// retrieve elements that did not fit into the buffer (if any). (This can
377+
/// only happen if `underestimatedCount` turned out to be an actual
378+
/// underestimate, and the buffer did not contain enough space to hold the
379+
/// entire sequence.)
380+
///
381+
/// On return, the memory region in `buffer[0 ..< c]` is initialized to
382+
/// the first `c` elements in the sequence.
362383
__consuming func _copyContents(
363384
initializing ptr: UnsafeMutableBufferPointer<Element>
364385
) -> (Iterator,UnsafeMutableBufferPointer<Element>.Index)
@@ -1093,13 +1114,29 @@ extension Sequence {
10931114
}
10941115

10951116
extension Sequence {
1096-
/// Copies `self` into the supplied buffer.
1117+
/// Copy `self` into an unsafe buffer, initializing its memory.
1118+
///
1119+
/// The default implementation simply iterates over the elements of the
1120+
/// sequence, initializing the buffer one item at a time.
1121+
///
1122+
/// For sequences whose elements are stored in contiguous chunks of memory,
1123+
/// it may be more efficient to copy them in bulk, using the
1124+
/// `UnsafeMutablePointer.initialize(from:count:)` method.
1125+
///
1126+
/// - Parameter ptr: An unsafe buffer addressing uninitialized memory. The
1127+
/// buffer must be of sufficient size to accommodate
1128+
/// `source.underestimatedCount` elements. (Some implementations trap
1129+
/// if given a buffer that's smaller than this.)
10971130
///
1098-
/// - Precondition: The memory in `self` is uninitialized. The buffer must
1099-
/// contain sufficient uninitialized memory to accommodate `source.underestimatedCount`.
1131+
/// - Returns: `(it, c)`, where `c` is the number of elements copied into the
1132+
/// buffer, and `it` is a partially consumed iterator that can be used to
1133+
/// retrieve elements that did not fit into the buffer (if any). (This can
1134+
/// only happen if `underestimatedCount` turned out to be an actual
1135+
/// underestimate, and the buffer did not contain enough space to hold the
1136+
/// entire sequence.)
11001137
///
1101-
/// - Postcondition: The `Pointee`s at `buffer[startIndex..<returned index]` are
1102-
/// initialized.
1138+
/// On return, the memory region in `buffer[0 ..< c]` is initialized to
1139+
/// the first `c` elements in the sequence.
11031140
@inlinable
11041141
public __consuming func _copyContents(
11051142
initializing buffer: UnsafeMutableBufferPointer<Element>

0 commit comments

Comments
 (0)