Skip to content

Commit 5494424

Browse files
Correct UnsafeBufferPointer's Collection.makeIterator, add _copyContents (#8902)
1 parent 82a2534 commit 5494424

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

stdlib/public/core/UnsafeBufferPointer.swift.gyb

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ public struct Unsafe${Mutable}BufferPointer<Element>
6868

6969
public typealias Index = Int
7070
public typealias IndexDistance = Int
71-
public typealias Iterator =
72-
IndexingIterator<Unsafe${Mutable}BufferPointer<Element>>
71+
public typealias Iterator = UnsafeBufferPointerIterator<Element>
7372

7473
/// The index of the first element in a nonempty buffer.
7574
///
@@ -183,6 +182,28 @@ public struct Unsafe${Mutable}BufferPointer<Element>
183182
return startIndex..<endIndex
184183
}
185184

185+
/// Copies `self` into the supplied buffer.
186+
///
187+
/// - Precondition: The memory in `self` is uninitialized. The buffer must
188+
/// contain sufficient uninitialized memory to accommodate `source.underestimatedCount`.
189+
///
190+
/// - Postcondition: The `Pointee`s at `buffer[startIndex..<returned index]` are
191+
/// initialized.
192+
public func _copyContents(
193+
initializing buffer: UnsafeMutableBufferPointer<Iterator.Element>
194+
) -> (Iterator,UnsafeMutableBufferPointer<Iterator.Element>.Index) {
195+
guard !isEmpty else { return (makeIterator(),buffer.startIndex) }
196+
197+
guard count <= buffer.count, let ptr = buffer.baseAddress else {
198+
fatalError("Insufficient space allocated to copy buffer contents")
199+
}
200+
201+
ptr.initialize(from: baseAddress!, count: self.count)
202+
var it: Iterator = self.makeIterator()
203+
it._position = it._end
204+
return (it,buffer.index(buffer.startIndex, offsetBy: self.count))
205+
}
206+
186207
/// Accesses the element at the specified position.
187208
///
188209
%if Mutable:

validation-test/stdlib/UnsafeBufferPointer.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ ${SelfName}TestSuite.test("AssociatedTypes") {
5353
% if IsRaw:
5454
iteratorType: ${SelfType}.Iterator.self,
5555
% else:
56-
iteratorType: IndexingIterator<${SelfType}>.self,
56+
iteratorType: UnsafeBufferPointerIterator<Float>.self,
5757
% end
5858
subSequenceType: ${'Mutable' if IsMutable else ''}RandomAccessSlice<${SelfType}>.self,
5959
indexType: Int.self,

0 commit comments

Comments
 (0)