Skip to content

[stdlib] Correct UnsafeBufferPointer's Collection.makeIterator, add _copyContents #8902

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions stdlib/public/core/UnsafeBufferPointer.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ public struct Unsafe${Mutable}BufferPointer<Element>

public typealias Index = Int
public typealias IndexDistance = Int
public typealias Iterator =
IndexingIterator<Unsafe${Mutable}BufferPointer<Element>>
public typealias Iterator = UnsafeBufferPointerIterator<Element>

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

/// Copies `self` into the supplied buffer.
///
/// - Precondition: The memory in `self` is uninitialized. The buffer must
/// contain sufficient uninitialized memory to accommodate `source.underestimatedCount`.
///
/// - Postcondition: The `Pointee`s at `buffer[startIndex..<returned index]` are
/// initialized.
public func _copyContents(
initializing buffer: UnsafeMutableBufferPointer<Iterator.Element>
) -> (Iterator,UnsafeMutableBufferPointer<Iterator.Element>.Index) {
guard !isEmpty else { return (makeIterator(),buffer.startIndex) }

guard count <= buffer.count, let ptr = buffer.baseAddress else {
fatalError("Insufficient space allocated to copy buffer contents")
}

ptr.initialize(from: baseAddress!, count: self.count)
var it: Iterator = self.makeIterator()
it._position = it._end
return (it,buffer.index(buffer.startIndex, offsetBy: self.count))
}

/// Accesses the element at the specified position.
///
%if Mutable:
Expand Down
2 changes: 1 addition & 1 deletion validation-test/stdlib/UnsafeBufferPointer.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ ${SelfName}TestSuite.test("AssociatedTypes") {
% if IsRaw:
iteratorType: ${SelfType}.Iterator.self,
% else:
iteratorType: IndexingIterator<${SelfType}>.self,
iteratorType: UnsafeBufferPointerIterator<Float>.self,
% end
subSequenceType: ${'Mutable' if IsMutable else ''}RandomAccessSlice<${SelfType}>.self,
indexType: Int.self,
Expand Down