Skip to content

Commit 44174d9

Browse files
authored
Merge pull request #128 from apple/unsafe-pointer-fixes
Update for Unsafe(Mutable)Pointer changes.
2 parents 5818041 + 71d50ee commit 44174d9

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/swift/Data.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,10 @@ public struct DispatchData : RandomAccessCollection {
118118
///
119119
/// - parameter buffer: The buffer of bytes to append. The size is calculated from `SourceType` and `buffer.count`.
120120
public mutating func append<SourceType>(_ buffer : UnsafeBufferPointer<SourceType>) {
121-
self.append(UnsafePointer(buffer.baseAddress!), count: buffer.count * sizeof(SourceType.self))
121+
let count = buffer.count * sizeof(SourceType.self)
122+
buffer.baseAddress?.withMemoryRebound(to: UInt8.self, capacity: count) {
123+
self.append($0, count: count)
124+
}
122125
}
123126

124127
private func _copyBytesHelper(to pointer: UnsafeMutablePointer<UInt8>, from range: CountableRange<Index>) {
@@ -176,8 +179,11 @@ public struct DispatchData : RandomAccessCollection {
176179

177180
guard !copyRange.isEmpty else { return 0 }
178181

179-
let pointer : UnsafeMutablePointer<UInt8> = UnsafeMutablePointer<UInt8>(buffer.baseAddress!)
180-
_copyBytesHelper(to: pointer, from: copyRange)
182+
let bufferCapacity = buffer.count * sizeof(DestinationType.self)
183+
buffer.baseAddress?.withMemoryRebound(to: UInt8.self, capacity: bufferCapacity) {
184+
185+
_copyBytesHelper(to: $0, from: copyRange)
186+
}
181187
return copyRange.count
182188
}
183189

0 commit comments

Comments
 (0)