@@ -49,24 +49,49 @@ public struct DispatchData : RandomAccessCollection {
49
49
/// Initialize a `Data` with copied memory content.
50
50
///
51
51
/// - parameter bytes: A pointer to the memory. It will be copied.
52
+ @available ( swift, deprecated: 4 , message: " Use init(bytes: UnsafeRawBufferPointer) instead " )
52
53
public init ( bytes buffer: UnsafeBufferPointer < UInt8 > ) {
53
54
let d = buffer. baseAddress == nil ? _swift_dispatch_data_empty ( )
54
55
: dispatch_data_create ( buffer. baseAddress!, buffer. count, nil ,
55
56
_dispatch_data_destructor_default ( ) )
56
57
self . init ( data: d)
57
58
}
58
59
60
+ /// Initialize a `Data` with copied memory content.
61
+ ///
62
+ /// - parameter bytes: A pointer to the memory. It will be copied.
63
+ /// - parameter count: The number of bytes to copy.
64
+ public init ( bytes buffer: UnsafeRawBufferPointer ) {
65
+ let d = buffer. baseAddress == nil ? _swift_dispatch_data_empty ( )
66
+ : dispatch_data_create ( buffer. baseAddress!, buffer. count, nil ,
67
+ _dispatch_data_destructor_default ( ) )
68
+ self . init ( data: d)
69
+ }
70
+
59
71
/// Initialize a `Data` without copying the bytes.
60
72
///
61
73
/// - parameter bytes: A buffer pointer containing the data.
62
74
/// - parameter deallocator: Specifies the mechanism to free the indicated buffer.
75
+ @available ( swift, deprecated: 4 , message: " Use init(bytes: UnsafeRawBufferPointer, deallocater: Deallocator) instead " )
63
76
public init ( bytesNoCopy bytes: UnsafeBufferPointer < UInt8 > , deallocator: Deallocator = . free) {
64
77
let ( q, b) = deallocator. _deallocator
65
78
let d = bytes. baseAddress == nil ? _swift_dispatch_data_empty ( )
66
79
: dispatch_data_create ( bytes. baseAddress!, bytes. count, q? . __wrapped, b)
67
80
self . init ( data: d)
68
81
}
69
82
83
+ /// Initialize a `Data` without copying the bytes.
84
+ ///
85
+ /// - parameter bytes: A pointer to the bytes.
86
+ /// - parameter count: The size of the bytes.
87
+ /// - parameter deallocator: Specifies the mechanism to free the indicated buffer.
88
+ public init ( bytesNoCopy bytes: UnsafeRawBufferPointer , deallocator: Deallocator = . free) {
89
+ let ( q, b) = deallocator. _deallocator
90
+ let d = bytes. baseAddress == nil ? _swift_dispatch_data_empty ( )
91
+ : dispatch_data_create ( bytes. baseAddress!, bytes. count, q? . __wrapped, b)
92
+ self . init ( data: d)
93
+ }
94
+
70
95
internal init ( data: dispatch_data_t ) {
71
96
__wrapped = __DispatchData ( data: data, owned: true )
72
97
}
@@ -113,11 +138,23 @@ public struct DispatchData : RandomAccessCollection {
113
138
///
114
139
/// - parameter bytes: A pointer to the bytes to copy in to the data.
115
140
/// - parameter count: The number of bytes to copy.
141
+ @available ( swift, deprecated: 4 , message: " Use append(_: UnsafeRawBufferPointer) instead " )
116
142
public mutating func append( _ bytes: UnsafePointer < UInt8 > , count: Int ) {
117
143
let data = dispatch_data_create ( bytes, count, nil , _dispatch_data_destructor_default ( ) )
118
144
self . append ( DispatchData ( data: data) )
119
145
}
120
146
147
+ /// Append bytes to the data.
148
+ ///
149
+ /// - parameter bytes: A pointer to the bytes to copy in to the data.
150
+ /// - parameter count: The number of bytes to copy.
151
+ public mutating func append( _ bytes: UnsafeRawBufferPointer ) {
152
+ // Nil base address does nothing.
153
+ guard bytes. baseAddress != nil else { return }
154
+ let data = dispatch_data_create ( bytes. baseAddress!, bytes. count, nil , _dispatch_data_destructor_default ( ) )
155
+ self . append ( DispatchData ( data: data) )
156
+ }
157
+
121
158
/// Append data to the data.
122
159
///
123
160
/// - parameter data: The data to append to this data.
@@ -156,19 +193,41 @@ public struct DispatchData : RandomAccessCollection {
156
193
/// - parameter pointer: A pointer to the buffer you wish to copy the bytes into.
157
194
/// - parameter count: The number of bytes to copy.
158
195
/// - warning: This method does not verify that the contents at pointer have enough space to hold `count` bytes.
196
+ @available ( swift, deprecated: 4 , message: " Use copyBytes(to: UnsafeMutableRawBufferPointer, count: Int) instead " )
159
197
public func copyBytes( to pointer: UnsafeMutablePointer < UInt8 > , count: Int ) {
160
198
_copyBytesHelper ( to: pointer, from: 0 ..< count)
161
199
}
200
+
201
+ /// Copy the contents of the data to a pointer.
202
+ ///
203
+ /// - parameter pointer: A pointer to the buffer you wish to copy the bytes into.
204
+ /// - parameter count: The number of bytes to copy.
205
+ /// - warning: This method does not verify that the contents at pointer have enough space to hold `count` bytes.
206
+ public func copyBytes( to pointer: UnsafeMutableRawBufferPointer , count: Int ) {
207
+ guard pointer. baseAddress != nil else { return }
208
+ _copyBytesHelper ( to: pointer. baseAddress!, from: 0 ..< count)
209
+ }
162
210
163
211
/// Copy a subset of the contents of the data to a pointer.
164
212
///
165
213
/// - parameter pointer: A pointer to the buffer you wish to copy the bytes into.
166
214
/// - parameter range: The range in the `Data` to copy.
167
215
/// - warning: This method does not verify that the contents at pointer have enough space to hold the required number of bytes.
216
+ @available ( swift, deprecated: 4 , message: " Use copyBytes(to: UnsafeMutableRawBufferPointer, from: CountableRange<Index>) instead " )
168
217
public func copyBytes( to pointer: UnsafeMutablePointer < UInt8 > , from range: CountableRange < Index > ) {
169
218
_copyBytesHelper ( to: pointer, from: range)
170
219
}
171
220
221
+ /// Copy a subset of the contents of the data to a pointer.
222
+ ///
223
+ /// - parameter pointer: A pointer to the buffer you wish to copy the bytes into.
224
+ /// - parameter range: The range in the `Data` to copy.
225
+ /// - warning: This method does not verify that the contents at pointer have enough space to hold the required number of bytes.
226
+ public func copyBytes( to pointer: UnsafeMutableRawBufferPointer , from range: CountableRange < Index > ) {
227
+ guard pointer. baseAddress != nil else { return }
228
+ _copyBytesHelper ( to: pointer. baseAddress!, from: range)
229
+ }
230
+
172
231
/// Copy the contents of the data into a buffer.
173
232
///
174
233
/// This function copies the bytes in `range` from the data into the buffer. If the count of the `range` is greater than `MemoryLayout<DestinationType>.stride * buffer.count` then the first N bytes will be copied into the buffer.
0 commit comments