Skip to content

Commit 522a73f

Browse files
committed
UnsafeRawPointer fixes.
1 parent ca4b5e9 commit 522a73f

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/swift/Data.swift

Lines changed: 10 additions & 8 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> {
@@ -242,7 +244,7 @@ public struct DispatchDataIterator : IteratorProtocol, Sequence {
242244
var ptr: UnsafeRawPointer?
243245
self._count = 0
244246
self._data = __DispatchData(data: CDispatch.dispatch_data_create_map(_data.__wrapped.__wrapped, &ptr, &self._count))
245-
self._ptr = UnsafePointer(ptr)
247+
self._ptr = ptr
246248
self._position = _data.startIndex
247249

248250
// The only time we expect a 'nil' pointer is when the data is empty.
@@ -253,13 +255,13 @@ public struct DispatchDataIterator : IteratorProtocol, Sequence {
253255
/// element exists.
254256
public mutating func next() -> DispatchData._Element? {
255257
if _position == _count { return nil }
256-
let element = _ptr[_position];
258+
let element = _ptr.load(fromByteOffset: _position, as: UInt8.self)
257259
_position = _position + 1
258260
return element
259261
}
260262

261263
internal let _data: __DispatchData
262-
internal var _ptr: UnsafePointer<UInt8>!
264+
internal var _ptr: UnsafeRawPointer!
263265
internal var _count: Int
264266
internal var _position: DispatchData.Index
265267
}

0 commit comments

Comments
 (0)