Skip to content

[stdlib] Correct UnsafeBufferPointer._copyContents #10740

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 2 commits into from
Jul 7, 2017
Merged
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
30 changes: 12 additions & 18 deletions stdlib/public/core/UnsafeBufferPointer.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -180,26 +180,20 @@ public struct Unsafe${Mutable}BufferPointer<Element>
return startIndex..<endIndex
}

/// Copies `self` into the supplied buffer.
/// Initializes the memory at `destination.baseAddress` with elements of `self`,
/// stopping when either `self` or `destination` is exhausted.
///
/// - 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.
/// - Returns: an iterator over any remaining elements of `self` and the
/// number of elements initialized.
public func _copyContents(
initializing buffer: UnsafeMutableBufferPointer<Element>
) -> (Iterator,UnsafeMutableBufferPointer<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))
initializing destination: UnsafeMutableBufferPointer<Element>
) -> (Iterator, UnsafeMutableBufferPointer<Element>.Index) {
guard !isEmpty && !destination.isEmpty else { return (makeIterator(), 0) }
let s = self.baseAddress._unsafelyUnwrappedUnchecked
let d = destination.baseAddress._unsafelyUnwrappedUnchecked
let n = Swift.min(destination.count, self.count)
d.initialize(from: s, count: n)
return (Iterator(_position: s + n, _end: _end), n)
}

/// Accesses the element at the specified position.
Expand Down