@@ -19,7 +19,6 @@ public struct DispatchData : RandomAccessCollection {
19
19
20
20
public static let empty : DispatchData = DispatchData ( data: _swift_dispatch_data_empty ( ) )
21
21
22
- #if false /* FIXME: dragging in _TMBO (Objective-C) */
23
22
public enum Deallocator {
24
23
/// Use `free`
25
24
case free
@@ -28,7 +27,13 @@ public struct DispatchData : RandomAccessCollection {
28
27
case unmap
29
28
30
29
/// A custom deallocator
31
- case custom( DispatchQueue ? , @convention ( block) ( ) -> Void )
30
+ // FIXME: Want @convention(block) here to minimize the overhead of
31
+ // doing the conversion (once per custom enum instance instead
32
+ // of once per call to DispatchData.init using the enum instance).
33
+ // However, adding the annotation here results in Data.o containing
34
+ // a reference to _TMBO (opaque metadata for Builtin.UnknownObject)
35
+ // which is only made available on platforms with Objective-C.
36
+ case custom( DispatchQueue ? , ( ) -> Void )
32
37
33
38
private var _deallocator : ( DispatchQueue ? , @convention ( block) ( ) -> Void ) {
34
39
switch self {
@@ -38,51 +43,50 @@ public struct DispatchData : RandomAccessCollection {
38
43
}
39
44
}
40
45
}
41
- #endif
42
- internal var __wrapped : dispatch_data_t
46
+
47
+ internal var __wrapped : __DispatchData
43
48
44
49
/// Initialize a `Data` with copied memory content.
45
50
///
46
51
/// - parameter bytes: A pointer to the memory. It will be copied.
47
52
/// - parameter count: The number of bytes to copy.
48
53
public init ( bytes buffer: UnsafeBufferPointer < UInt8 > ) {
49
- __wrapped = dispatch_data_create (
50
- buffer . baseAddress! , buffer . count , nil , _dispatch_data_destructor_default ( ) )
54
+ let d = dispatch_data_create ( buffer . baseAddress! , buffer . count , nil , _dispatch_data_destructor_default ( ) )
55
+ self . init ( data : d )
51
56
}
52
- #if false /* FIXME: dragging in _TMBO (Objective-C) */
57
+
53
58
/// Initialize a `Data` without copying the bytes.
54
59
///
55
- /// - parameter bytes: A pointer to the bytes.
56
- /// - parameter count: The size of the bytes.
60
+ /// - parameter bytes: A buffer pointer containing the data.
57
61
/// - parameter deallocator: Specifies the mechanism to free the indicated buffer.
58
62
public init ( bytesNoCopy bytes: UnsafeBufferPointer < UInt8 > , deallocator: Deallocator = . free) {
59
63
let ( q, b) = deallocator. _deallocator
60
-
61
- __wrapped = dispatch_data_create ( bytes . baseAddress! , bytes . count , q ? . __wrapped , b )
64
+ let d = dispatch_data_create ( bytes . baseAddress! , bytes . count , q ? . __wrapped , b )
65
+ self . init ( data : d )
62
66
}
63
- #endif
67
+
64
68
internal init ( data: dispatch_data_t ) {
65
- __wrapped = data
69
+ __wrapped = __DispatchData ( data: data )
66
70
}
67
71
68
72
public var count : Int {
69
- return CDispatch . dispatch_data_get_size ( __wrapped)
73
+ return CDispatch . dispatch_data_get_size ( __wrapped. __wrapped )
70
74
}
71
75
72
76
public func withUnsafeBytes< Result, ContentType> (
73
77
body: @noescape ( UnsafePointer < ContentType > ) throws -> Result ) rethrows -> Result
74
78
{
75
79
var ptr : UnsafePointer < Void > ? = nil
76
80
var size = 0 ;
77
- let data = CDispatch . dispatch_data_create_map ( __wrapped, & ptr, & size)
81
+ let data = CDispatch . dispatch_data_create_map ( __wrapped. __wrapped , & ptr, & size)
78
82
defer { _fixLifetime ( data) }
79
83
return try body ( UnsafePointer < ContentType > ( ptr!) )
80
84
}
81
85
82
86
public func enumerateBytes(
83
87
block: @noescape ( buffer: UnsafeBufferPointer < UInt8 > , byteIndex: Int , stop: inout Bool ) -> Void )
84
88
{
85
- _swift_dispatch_data_apply ( __wrapped) { ( data: dispatch_data_t , offset: Int , ptr: UnsafePointer < Void > , size: Int ) in
89
+ _swift_dispatch_data_apply ( __wrapped. __wrapped ) { ( data: dispatch_data_t , offset: Int , ptr: UnsafePointer < Void > , size: Int ) in
86
90
let bp = UnsafeBufferPointer ( start: UnsafePointer < UInt8 > ( ptr) , count: size)
87
91
var stop = false
88
92
block ( buffer: bp, byteIndex: offset, stop: & stop)
@@ -103,8 +107,8 @@ public struct DispatchData : RandomAccessCollection {
103
107
///
104
108
/// - parameter data: The data to append to this data.
105
109
public mutating func append( _ other: DispatchData ) {
106
- let data = CDispatch . dispatch_data_create_concat ( __wrapped, other. __wrapped)
107
- __wrapped = data
110
+ let data = CDispatch . dispatch_data_create_concat ( __wrapped. __wrapped , other. __wrapped . __wrapped)
111
+ __wrapped = __DispatchData ( data: data )
108
112
}
109
113
110
114
/// Append a buffer of bytes to the data.
@@ -116,7 +120,7 @@ public struct DispatchData : RandomAccessCollection {
116
120
117
121
private func _copyBytesHelper( to pointer: UnsafeMutablePointer < UInt8 > , from range: CountableRange < Index > ) {
118
122
var copiedCount = 0
119
- _ = CDispatch . dispatch_data_apply ( __wrapped) { ( data: dispatch_data_t , offset: Int , ptr: UnsafePointer < Void > , size: Int ) in
123
+ _ = CDispatch . dispatch_data_apply ( __wrapped. __wrapped ) { ( data: dispatch_data_t , offset: Int , ptr: UnsafePointer < Void > , size: Int ) in
120
124
let limit = Swift . min ( ( range. endIndex - range. startIndex) - copiedCount, size)
121
125
memcpy ( pointer + copiedCount, ptr, limit)
122
126
copiedCount += limit
@@ -177,7 +181,7 @@ public struct DispatchData : RandomAccessCollection {
177
181
/// Sets or returns the byte at the specified index.
178
182
public subscript( index: Index ) -> UInt8 {
179
183
var offset = 0
180
- let subdata = CDispatch . dispatch_data_copy_region ( __wrapped, index, & offset)
184
+ let subdata = CDispatch . dispatch_data_copy_region ( __wrapped. __wrapped , index, & offset)
181
185
182
186
var ptr : UnsafePointer < Void > ? = nil
183
187
var size = 0
@@ -197,13 +201,13 @@ public struct DispatchData : RandomAccessCollection {
197
201
/// - parameter range: The range to copy.
198
202
public func subdata( in range: CountableRange < Index > ) -> DispatchData {
199
203
let subrange = CDispatch . dispatch_data_create_subrange (
200
- __wrapped, range. startIndex, range. endIndex - range. startIndex)
204
+ __wrapped. __wrapped , range. startIndex, range. endIndex - range. startIndex)
201
205
return DispatchData ( data: subrange)
202
206
}
203
207
204
208
public func region( location: Int ) -> ( data: DispatchData , offset: Int ) {
205
209
var offset : Int = 0
206
- let data = CDispatch . dispatch_data_copy_region ( __wrapped, location, & offset)
210
+ let data = CDispatch . dispatch_data_copy_region ( __wrapped. __wrapped , location, & offset)
207
211
return ( DispatchData ( data: data) , offset)
208
212
}
209
213
@@ -237,7 +241,7 @@ public struct DispatchDataIterator : IteratorProtocol, Sequence {
237
241
public init ( _data: DispatchData ) {
238
242
var ptr : UnsafePointer < Void > ?
239
243
self . _count = 0
240
- self . _data = CDispatch . dispatch_data_create_map ( _data. __wrapped, & ptr, & self . _count)
244
+ self . _data = __DispatchData ( data : CDispatch . dispatch_data_create_map ( _data. __wrapped. __wrapped , & ptr, & self . _count) )
241
245
self . _ptr = UnsafePointer ( ptr)
242
246
self . _position = _data. startIndex
243
247
@@ -254,7 +258,7 @@ public struct DispatchDataIterator : IteratorProtocol, Sequence {
254
258
return element
255
259
}
256
260
257
- internal let _data : dispatch_data_t
261
+ internal let _data : __DispatchData
258
262
internal var _ptr : UnsafePointer < UInt8 > !
259
263
internal var _count : Int
260
264
internal var _position : DispatchData . Index
0 commit comments