@@ -16,11 +16,11 @@ import Darwin
16
16
import Glibc
17
17
#endif
18
18
19
- internal func __NSDataInvokeDeallocatorUnmap( _ mem: UnsafeMutablePointer < Void > , _ length: Int ) -> Void {
19
+ internal func __NSDataInvokeDeallocatorUnmap( _ mem: UnsafeMutableRawPointer , _ length: Int ) -> Void {
20
20
munmap ( mem, length)
21
21
}
22
22
23
- internal func __NSDataInvokeDeallocatorFree( _ mem: UnsafeMutablePointer < Void > , _ length: Int ) -> Void {
23
+ internal func __NSDataInvokeDeallocatorFree( _ mem: UnsafeMutableRawPointer , _ length: Int ) -> Void {
24
24
free ( mem)
25
25
}
26
26
@@ -34,30 +34,30 @@ internal final class _SwiftNSData : NSData, _SwiftNativeFoundationType {
34
34
// Take ownership.
35
35
__wrapped = . Immutable( Unmanaged . passRetained ( _unsafeReferenceCast ( immutableObject, to: ImmutableType . self) ) )
36
36
37
- let dummyPointer = unsafeBitCast ( NSData . self, to: UnsafeMutablePointer < Void > . self )
37
+ let dummyPointer = unsafeBitCast ( NSData . self, to: UnsafeMutableRawPointer . self)
38
38
super. init ( bytes: dummyPointer, length: 0 , copy: false , deallocator: nil )
39
39
}
40
40
41
41
init ( mutableObject: AnyObject ) {
42
42
// Take ownership.
43
43
__wrapped = . Mutable( Unmanaged . passRetained ( _unsafeReferenceCast ( mutableObject, to: MutableType . self) ) )
44
- let dummyPointer = unsafeBitCast ( NSData . self, to: UnsafeMutablePointer < Void > . self )
44
+ let dummyPointer = unsafeBitCast ( NSData . self, to: UnsafeMutableRawPointer . self)
45
45
super. init ( bytes: dummyPointer, length: 0 , copy: false , deallocator: nil )
46
46
}
47
47
48
48
internal required init ( unmanagedImmutableObject: Unmanaged < ImmutableType > ) {
49
49
// Take ownership.
50
50
__wrapped = . Immutable( unmanagedImmutableObject)
51
51
52
- let dummyPointer = unsafeBitCast ( NSData . self, to: UnsafeMutablePointer < Void > . self )
52
+ let dummyPointer = unsafeBitCast ( NSData . self, to: UnsafeMutableRawPointer . self)
53
53
super. init ( bytes: dummyPointer, length: 0 , copy: false , deallocator: nil )
54
54
}
55
55
56
56
internal required init ( unmanagedMutableObject: Unmanaged < MutableType > ) {
57
57
// Take ownership.
58
58
__wrapped = . Mutable( unmanagedMutableObject)
59
59
60
- let dummyPointer = unsafeBitCast ( NSData . self, to: UnsafeMutablePointer < Void > . self )
60
+ let dummyPointer = unsafeBitCast ( NSData . self, to: UnsafeMutableRawPointer . self)
61
61
super. init ( bytes: dummyPointer, length: 0 , copy: false , deallocator: nil )
62
62
}
63
63
@@ -82,19 +82,19 @@ internal final class _SwiftNSData : NSData, _SwiftNativeFoundationType {
82
82
}
83
83
}
84
84
85
- override var bytes : UnsafePointer < Void > {
85
+ override var bytes : UnsafeRawPointer {
86
86
return _mapUnmanaged { $0. bytes }
87
87
}
88
88
89
89
// override func subdata(with range: NSRange) -> Data {
90
90
// return _mapUnmanaged { $0.subdata(with: range) }
91
91
// }
92
92
//
93
- // override func getBytes(_ buffer: UnsafeMutablePointer<Void> , length: Int) {
93
+ // override func getBytes(_ buffer: UnsafeMutableRawPointer , length: Int) {
94
94
// return _mapUnmanaged { $0.getBytes(buffer, length: length) }
95
95
// }
96
96
//
97
- // override func getBytes(_ buffer: UnsafeMutablePointer<Void> , range: NSRange) {
97
+ // override func getBytes(_ buffer: UnsafeMutableRawPointer , range: NSRange) {
98
98
// return _mapUnmanaged { $0.getBytes(buffer, range: range) }
99
99
// }
100
100
//
@@ -112,7 +112,7 @@ internal final class _SwiftNSData : NSData, _SwiftNativeFoundationType {
112
112
// }
113
113
// }
114
114
//
115
- // override func enumerateByteRanges(using block: @noescape (UnsafePointer<Void> , NSRange, UnsafeMutablePointer<ObjCBool>) -> Void) {
115
+ // override func enumerateByteRanges(using block: @noescape (UnsafeRawPointer , NSRange, UnsafeMutablePointer<ObjCBool>) -> Void) {
116
116
// return _mapUnmanaged { $0.enumerateBytes(block) }
117
117
// }
118
118
//
@@ -163,7 +163,7 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
163
163
/// A custom deallocator.
164
164
case custom( ( UnsafeMutablePointer < UInt8 > , Int ) -> Void )
165
165
166
- fileprivate var _deallocator : ( ( UnsafeMutablePointer < Void > , Int ) -> Void ) ? {
166
+ fileprivate var _deallocator : ( ( UnsafeMutableRawPointer , Int ) -> Void ) ? {
167
167
switch self {
168
168
case . unmap:
169
169
return { __NSDataInvokeDeallocatorUnmap ( $0, $1) }
@@ -173,7 +173,8 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
173
173
return nil
174
174
case . custom( let b) :
175
175
return { ( ptr, len) in
176
- b ( UnsafeMutablePointer < UInt8 > ( ptr) , len)
176
+ let bytePtr = ptr. bindMemory ( to: UInt8 . self, capacity: len)
177
+ b ( bytePtr, len)
177
178
}
178
179
}
179
180
}
@@ -186,7 +187,7 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
186
187
///
187
188
/// - parameter bytes: A pointer to the memory. It will be copied.
188
189
/// - parameter count: The number of bytes to copy.
189
- public init ( bytes: UnsafePointer < Void > , count: Int ) {
190
+ public init ( bytes: UnsafeRawPointer , count: Int ) {
190
191
_wrapped = _SwiftNSData ( immutableObject: NSData ( bytes: bytes, length: count) )
191
192
}
192
193
@@ -279,8 +280,8 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
279
280
}
280
281
281
282
public init ? ( count: Int ) {
282
- if let memory = malloc ( count) {
283
- self . init ( bytesNoCopy: UnsafeMutablePointer < UInt8 > ( memory) , count: count, deallocator: . free)
283
+ if let memory = malloc ( count) ? . bindMemory ( to : UInt8 . self , capacity : count ) {
284
+ self . init ( bytesNoCopy: memory, count: count, deallocator: . free)
284
285
} else {
285
286
return nil
286
287
}
@@ -304,7 +305,7 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
304
305
}
305
306
}
306
307
307
- private func _getUnsafeBytesPointer( ) -> UnsafePointer < Void > {
308
+ private func _getUnsafeBytesPointer( ) -> UnsafeRawPointer {
308
309
return _mapUnmanaged { return $0. bytes }
309
310
}
310
311
@@ -314,10 +315,11 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
314
315
public func withUnsafeBytes< ResultType, ContentType> ( _ body: @noescape ( UnsafePointer < ContentType > ) throws -> ResultType ) rethrows -> ResultType {
315
316
let bytes = _getUnsafeBytesPointer ( )
316
317
defer { _fixLifetime ( self ) }
317
- return try body ( UnsafePointer ( bytes) )
318
+ let contentPtr = bytes. bindMemory ( to: ContentType . self, capacity: count / strideof( ContentType . self) )
319
+ return try body ( contentPtr)
318
320
}
319
321
320
- private mutating func _getUnsafeMutableBytesPointer( ) -> UnsafeMutablePointer < Void > {
322
+ private mutating func _getUnsafeMutableBytesPointer( ) -> UnsafeMutableRawPointer {
321
323
return _applyUnmanagedMutation {
322
324
return $0. mutableBytes
323
325
}
@@ -330,7 +332,8 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
330
332
public mutating func withUnsafeMutableBytes< ResultType, ContentType> ( _ body: @noescape ( UnsafeMutablePointer < ContentType > ) throws -> ResultType ) rethrows -> ResultType {
331
333
let mutableBytes = _getUnsafeMutableBytesPointer ( )
332
334
defer { _fixLifetime ( self ) }
333
- return try body ( UnsafeMutablePointer ( mutableBytes) )
335
+ let contentPtr = mutableBytes. bindMemory ( to: ContentType . self, capacity: count / strideof( ContentType . self) )
336
+ return try body ( contentPtr)
334
337
}
335
338
336
339
// MARK: -
@@ -437,7 +440,8 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
437
440
_mapUnmanaged {
438
441
$0. enumerateBytes { ( ptr, range, stop) in
439
442
var stopv = false
440
- block ( buffer: UnsafeBufferPointer ( start: UnsafePointer < UInt8 > ( ptr) , count: range. length) , byteIndex: range. length, stop: & stopv)
443
+ let bytePtr = ptr. bindMemory ( to: UInt8 . self, capacity: range. length)
444
+ block ( buffer: UnsafeBufferPointer ( start: bytePtr, count: range. length) , byteIndex: range. length, stop: & stopv)
441
445
if stopv {
442
446
stop. pointee = true
443
447
}
0 commit comments