diff --git a/src/swift/Data.swift b/src/swift/Data.swift index ca4c3e90e..e5d03e973 100644 --- a/src/swift/Data.swift +++ b/src/swift/Data.swift @@ -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.stride) defer { _fixLifetime(data) } return try body(contentPtr) } @@ -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(_ buffer : UnsafeBufferPointer) { - 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.stride; + buffer.baseAddress?.withMemoryRebound(to: UInt8.self, capacity: count) { + self.append($0, count: count) + } } private func _copyBytesHelper(to pointer: UnsafeMutablePointer, from range: CountableRange) { @@ -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.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. @@ -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.stride, r.count)) } else { - copyRange = 0...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.stride + buffer.baseAddress?.withMemoryRebound(to: UInt8.self, capacity: bufferCapacity) { + _copyBytesHelper(to: $0, from: copyRange) + } return copyRange.count }