Skip to content

Commit 426c007

Browse files
ktopley-appledas
authored andcommitted
Add support for UnsafeRawBufferPointer to DispatchData
(Radar 30699743) Signed-off-by: Daniel A. Steffen <dsteffen@apple.com>
1 parent 2590ec7 commit 426c007

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

src/swift/Data.swift

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,49 @@ public struct DispatchData : RandomAccessCollection {
4949
/// Initialize a `Data` with copied memory content.
5050
///
5151
/// - parameter bytes: A pointer to the memory. It will be copied.
52+
@available(swift, deprecated: 4, message: "Use init(bytes: UnsafeRawBufferPointer) instead")
5253
public init(bytes buffer: UnsafeBufferPointer<UInt8>) {
5354
let d = buffer.baseAddress == nil ? _swift_dispatch_data_empty()
5455
: dispatch_data_create(buffer.baseAddress!, buffer.count, nil,
5556
_dispatch_data_destructor_default())
5657
self.init(data: d)
5758
}
5859

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+
5971
/// Initialize a `Data` without copying the bytes.
6072
///
6173
/// - parameter bytes: A buffer pointer containing the data.
6274
/// - parameter deallocator: Specifies the mechanism to free the indicated buffer.
75+
@available(swift, deprecated: 4, message: "Use init(bytes: UnsafeRawBufferPointer, deallocater: Deallocator) instead")
6376
public init(bytesNoCopy bytes: UnsafeBufferPointer<UInt8>, deallocator: Deallocator = .free) {
6477
let (q, b) = deallocator._deallocator
6578
let d = bytes.baseAddress == nil ? _swift_dispatch_data_empty()
6679
: dispatch_data_create(bytes.baseAddress!, bytes.count, q?.__wrapped, b)
6780
self.init(data: d)
6881
}
6982

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+
7095
internal init(data: dispatch_data_t) {
7196
__wrapped = __DispatchData(data: data, owned: true)
7297
}
@@ -113,11 +138,23 @@ public struct DispatchData : RandomAccessCollection {
113138
///
114139
/// - parameter bytes: A pointer to the bytes to copy in to the data.
115140
/// - parameter count: The number of bytes to copy.
141+
@available(swift, deprecated: 4, message: "Use append(_: UnsafeRawBufferPointer) instead")
116142
public mutating func append(_ bytes: UnsafePointer<UInt8>, count: Int) {
117143
let data = dispatch_data_create(bytes, count, nil, _dispatch_data_destructor_default())
118144
self.append(DispatchData(data: data))
119145
}
120146

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+
121158
/// Append data to the data.
122159
///
123160
/// - parameter data: The data to append to this data.
@@ -156,19 +193,41 @@ public struct DispatchData : RandomAccessCollection {
156193
/// - parameter pointer: A pointer to the buffer you wish to copy the bytes into.
157194
/// - parameter count: The number of bytes to copy.
158195
/// - 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")
159197
public func copyBytes(to pointer: UnsafeMutablePointer<UInt8>, count: Int) {
160198
_copyBytesHelper(to: pointer, from: 0..<count)
161199
}
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+
}
162210

163211
/// Copy a subset of the contents of the data to a pointer.
164212
///
165213
/// - parameter pointer: A pointer to the buffer you wish to copy the bytes into.
166214
/// - parameter range: The range in the `Data` to copy.
167215
/// - 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")
168217
public func copyBytes(to pointer: UnsafeMutablePointer<UInt8>, from range: CountableRange<Index>) {
169218
_copyBytesHelper(to: pointer, from: range)
170219
}
171220

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+
172231
/// Copy the contents of the data into a buffer.
173232
///
174233
/// 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

Comments
 (0)