10
10
//
11
11
//===----------------------------------------------------------------------===//
12
12
13
+ import SwiftShims
14
+
13
15
public struct DispatchData : RandomAccessCollection , _ObjectiveCBridgeable {
14
16
public typealias Iterator = DispatchDataIterator
15
17
public typealias Index = Int
@@ -43,8 +45,8 @@ public struct DispatchData : RandomAccessCollection, _ObjectiveCBridgeable {
43
45
/// - parameter bytes: A pointer to the memory. It will be copied.
44
46
/// - parameter count: The number of bytes to copy.
45
47
public init ( bytes buffer: UnsafeBufferPointer < UInt8 > ) {
46
- __wrapped = __dispatch_data_create (
47
- buffer. baseAddress!, buffer. count, nil , _dispatch_data_destructor_default ( ) )
48
+ __wrapped = _swift_dispatch_data_create (
49
+ buffer. baseAddress!, buffer. count, nil , _dispatch_data_destructor_default ( ) ) as! __DispatchData
48
50
}
49
51
50
52
/// Initialize a `Data` without copying the bytes.
@@ -55,8 +57,8 @@ public struct DispatchData : RandomAccessCollection, _ObjectiveCBridgeable {
55
57
public init ( bytesNoCopy bytes: UnsafeBufferPointer < UInt8 > , deallocator: Deallocator = . free) {
56
58
let ( q, b) = deallocator. _deallocator
57
59
58
- __wrapped = __dispatch_data_create (
59
- bytes. baseAddress!, bytes. count, q, b)
60
+ __wrapped = _swift_dispatch_data_create (
61
+ bytes. baseAddress!, bytes. count, q, b) as! __DispatchData
60
62
}
61
63
62
64
internal init ( data: __DispatchData ) {
@@ -93,15 +95,15 @@ public struct DispatchData : RandomAccessCollection, _ObjectiveCBridgeable {
93
95
/// - parameter bytes: A pointer to the bytes to copy in to the data.
94
96
/// - parameter count: The number of bytes to copy.
95
97
public mutating func append( _ bytes: UnsafePointer < UInt8 > , count: Int ) {
96
- let data = __dispatch_data_create ( bytes, count, nil , _dispatch_data_destructor_default ( ) )
98
+ let data = _swift_dispatch_data_create ( bytes, count, nil , _dispatch_data_destructor_default ( ) ) as! __DispatchData
97
99
self . append ( DispatchData ( data: data) )
98
100
}
99
101
100
102
/// Append data to the data.
101
103
///
102
104
/// - parameter data: The data to append to this data.
103
105
public mutating func append( _ other: DispatchData ) {
104
- let data = __dispatch_data_create_concat ( __wrapped, other as __DispatchData )
106
+ let data = __dispatch_data_create_concat ( __wrapped, other. __wrapped )
105
107
__wrapped = data
106
108
}
107
109
@@ -237,14 +239,15 @@ public struct DispatchDataIterator : IteratorProtocol, Sequence {
237
239
self . _count = 0
238
240
self . _data = __dispatch_data_create_map (
239
241
_data as __DispatchData , & ptr, & self . _count)
240
- self . _ptr = UnsafePointer ( ptr! )
242
+ self . _ptr = UnsafePointer ( ptr)
241
243
self . _position = _data. startIndex
244
+
245
+ // The only time we expect a 'nil' pointer is when the data is empty.
246
+ assert ( self . _ptr != nil || self . _count == self . _position)
242
247
}
243
248
244
249
/// Advance to the next element and return it, or `nil` if no next
245
250
/// element exists.
246
- ///
247
- /// - Precondition: No preceding call to `self.next()` has returned `nil`.
248
251
public mutating func next( ) -> DispatchData . _Element ? {
249
252
if _position == _count { return nil }
250
253
let element = _ptr [ _position]
@@ -253,7 +256,7 @@ public struct DispatchDataIterator : IteratorProtocol, Sequence {
253
256
}
254
257
255
258
internal let _data : __DispatchData
256
- internal var _ptr : UnsafePointer < UInt8 >
259
+ internal var _ptr : UnsafePointer < UInt8 > !
257
260
internal var _count : Int
258
261
internal var _position : DispatchData . Index
259
262
}
0 commit comments