Skip to content

Commit 3e230d3

Browse files
authored
Merge pull request #3832 from parkera/data_unsaferawpointer
Adapt to UnsafeRawPointer changes in Data initializer and deallocator…
2 parents 77df286 + ef99d39 commit 3e230d3

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

stdlib/public/SDK/Foundation/Data.swift

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ internal final class _SwiftNSData : _SwiftNativeNSData, _SwiftNativeFoundationTy
6868

6969
This type provides "copy-on-write" behavior, and is also bridged to the Objective-C `NSData` class. You can wrap an instance of a custom subclass of `NSData` in `struct Data` by converting it using `myData as Data`.
7070

71-
`Data` can be initialized with an `UnsafePointer` and count, an array of `UInt8` (the primitive byte type), or an `UnsafeBufferPointer`. The buffer-oriented functions provide an extra measure of safety by automatically performing the size calculation, as the type is known at compile time.
71+
`Data` can be initialized with an `UnsafeRawPointer` and count, an array of `UInt8` (the primitive byte type), an `UnsafeBufferPointer`, the contents of a file, or base-64 encoded data or strings. The buffer-oriented functions provide an extra measure of safety by automatically performing the size calculation, as the type is known at compile time.
7272
*/
7373
public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessCollection, MutableCollection, RangeReplaceableCollection, _MutablePairBoxing {
7474
/// The Objective-C bridged type of `Data`.
@@ -102,7 +102,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
102102
case none
103103

104104
/// A custom deallocator.
105-
case custom((UnsafeMutablePointer<UInt8>, Int) -> Void)
105+
case custom((UnsafeMutableRawPointer, Int) -> Void)
106106

107107
fileprivate var _deallocator : ((UnsafeMutableRawPointer, Int) -> Void)? {
108108
switch self {
@@ -116,9 +116,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
116116
return nil
117117
case .custom(let b):
118118
return { (ptr, len) in
119-
// Bind memory to UInt8 since that is what the public deallocation function expects.
120-
let bytePtr = ptr.bindMemory(to: UInt8.self, capacity: len)
121-
b(bytePtr, len)
119+
b(ptr, len)
122120
}
123121
}
124122
}
@@ -131,7 +129,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
131129
///
132130
/// - parameter bytes: A pointer to the memory. It will be copied.
133131
/// - parameter count: The number of bytes to copy.
134-
public init(bytes: UnsafePointer<UInt8>, count: Int) {
132+
public init(bytes: UnsafeRawPointer, count: Int) {
135133
_wrapped = _SwiftNSData(immutableObject: NSData(bytes: bytes, length: count))
136134
}
137135

@@ -207,7 +205,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
207205
/// - parameter bytes: A pointer to the bytes.
208206
/// - parameter count: The size of the bytes.
209207
/// - parameter deallocator: Specifies the mechanism to free the indicated buffer, or `.none`.
210-
public init(bytesNoCopy bytes: UnsafeMutablePointer<UInt8>, count: Int, deallocator: Deallocator) {
208+
public init(bytesNoCopy bytes: UnsafeMutableRawPointer, count: Int, deallocator: Deallocator) {
211209
let whichDeallocator = deallocator._deallocator
212210
_wrapped = _SwiftNSData(immutableObject: NSData(bytesNoCopy: bytes, length: count, deallocator: whichDeallocator))
213211
}

0 commit comments

Comments
 (0)