Skip to content

Commit 4dc2830

Browse files
committed
UnsafeRawPointer fixes.
1 parent ca4b5e9 commit 4dc2830

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/swift/Data.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,20 @@ public struct DispatchData : RandomAccessCollection {
7777
body: @noescape (UnsafePointer<ContentType>) throws -> Result) rethrows -> Result
7878
{
7979
var ptr: UnsafeRawPointer? = nil
80-
var size = 0;
80+
var size = 0
8181
let data = CDispatch.dispatch_data_create_map(__wrapped.__wrapped, &ptr, &size)
82+
let contentPtr = ptr!.bindMemory(
83+
to: ContentType.self, capacity: size / strideof(ContentType.self))
8284
defer { _fixLifetime(data) }
83-
return try body(UnsafePointer<ContentType>(ptr!))
85+
return try body(contentPtr)
8486
}
8587

8688
public func enumerateBytes(
8789
block: @noescape (buffer: UnsafeBufferPointer<UInt8>, byteIndex: Int, stop: inout Bool) -> Void)
8890
{
8991
_swift_dispatch_data_apply(__wrapped.__wrapped) { (data: dispatch_data_t, offset: Int, ptr: UnsafeRawPointer, size: Int) in
90-
let bp = UnsafeBufferPointer(start: UnsafePointer<UInt8>(ptr), count: size)
92+
let bytePtr = ptr.bindMemory(to: UInt8.self, capacity: size)
93+
let bp = UnsafeBufferPointer(start: bytePtr, count: size)
9194
var stop = false
9295
block(buffer: bp, byteIndex: offset, stop: &stop)
9396
return !stop
@@ -188,8 +191,7 @@ public struct DispatchData : RandomAccessCollection {
188191
let map = CDispatch.dispatch_data_create_map(subdata, &ptr, &size)
189192
defer { _fixLifetime(map) }
190193

191-
let pptr = UnsafePointer<UInt8>(ptr!)
192-
return pptr[index - offset]
194+
return ptr!.load(fromByteOffset: index - offset, as: UInt8.self)
193195
}
194196

195197
public subscript(bounds: Range<Int>) -> RandomAccessSlice<DispatchData> {

0 commit comments

Comments
 (0)