Skip to content

[SE-0101] Migration #129

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
Aug 1, 2016
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
25 changes: 12 additions & 13 deletions src/swift/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public struct DispatchData : RandomAccessCollection {
var size = 0
let data = CDispatch.dispatch_data_create_map(__wrapped.__wrapped, &ptr, &size)
let contentPtr = ptr!.bindMemory(
to: ContentType.self, capacity: size / strideof(ContentType.self))
to: ContentType.self, capacity: size / MemoryLayout<ContentType>.stride)
defer { _fixLifetime(data) }
return try body(contentPtr)
}
Expand Down Expand Up @@ -118,10 +118,10 @@ public struct DispatchData : RandomAccessCollection {
///
/// - parameter buffer: The buffer of bytes to append. The size is calculated from `SourceType` and `buffer.count`.
public mutating func append<SourceType>(_ buffer : UnsafeBufferPointer<SourceType>) {
let count = buffer.count * sizeof(SourceType.self)
buffer.baseAddress?.withMemoryRebound(to: UInt8.self, capacity: count) {
self.append($0, count: count)
}
let count = buffer.count * MemoryLayout<SourceType>.stride;
buffer.baseAddress?.withMemoryRebound(to: UInt8.self, capacity: count) {
self.append($0, count: count)
}
}

private func _copyBytesHelper(to pointer: UnsafeMutablePointer<UInt8>, from range: CountableRange<Index>) {
Expand Down Expand Up @@ -154,7 +154,7 @@ public struct DispatchData : RandomAccessCollection {

/// Copy the contents of the data into a buffer.
///
/// This function copies the bytes in `range` from the data into the buffer. If the count of the `range` is greater than `sizeof(DestinationType) * buffer.count` then the first N bytes will be copied into the buffer.
/// This function copies the bytes in `range` from the data into the buffer. If the count of the `range` is greater than `MemoryLayout<DestinationType>.stride * buffer.count` then the first N bytes will be copied into the buffer.
/// - precondition: The range must be within the bounds of the data. Otherwise `fatalError` is called.
/// - parameter buffer: A buffer to copy the data into.
/// - parameter range: A range in the data to copy into the buffer. If the range is empty, this function will return 0 without copying anything. If the range is nil, as much data as will fit into `buffer` is copied.
Expand All @@ -172,18 +172,17 @@ public struct DispatchData : RandomAccessCollection {
precondition(r.endIndex >= 0)
precondition(r.endIndex <= cnt, "The range is outside the bounds of the data")

copyRange = r.startIndex..<(r.startIndex + Swift.min(buffer.count * sizeof(DestinationType.self), r.count))
copyRange = r.startIndex..<(r.startIndex + Swift.min(buffer.count * MemoryLayout<DestinationType>.stride, r.count))
} else {
copyRange = 0..<Swift.min(buffer.count * sizeof(DestinationType.self), cnt)
copyRange = 0..<Swift.min(buffer.count * MemoryLayout<DestinationType>.stride, cnt)
}

guard !copyRange.isEmpty else { return 0 }

let bufferCapacity = buffer.count * sizeof(DestinationType.self)
buffer.baseAddress?.withMemoryRebound(to: UInt8.self, capacity: bufferCapacity) {

_copyBytesHelper(to: $0, from: copyRange)
}
let bufferCapacity = buffer.count * MemoryLayout<DestinationType>.stride
buffer.baseAddress?.withMemoryRebound(to: UInt8.self, capacity: bufferCapacity) {
_copyBytesHelper(to: $0, from: copyRange)
}
return copyRange.count
}

Expand Down