Skip to content

Commit 124680b

Browse files
spevansparkera
authored andcommitted
Fix more warnings for deprecation and unsafeBitCast() (#953)
* Replace unsafeBitCast() with bindMemory()/withMemoryRebound() * Convert deprecated initialize(from:) to UnsafeMutableBufferPointer.initialize(from:) * warnings: Use names for implict closure arguments * warnings: UnsafeMutablePointer.initialize(from:) deprecation - Use UnsafeMutablePointer.initialize(from:count:) as a better alternative to UnsafeMutableBufferPointer.initialize(from:)
1 parent a6a4e79 commit 124680b

File tree

5 files changed

+42
-33
lines changed

5 files changed

+42
-33
lines changed

Foundation/DateInterval.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,11 @@ public struct DateInterval : ReferenceConvertible, Comparable, Hashable {
155155

156156
public var hashValue: Int {
157157
var buf: (UInt, UInt) = (UInt(start.timeIntervalSinceReferenceDate), UInt(end.timeIntervalSinceReferenceDate))
158-
return withUnsafeMutablePointer(to: &buf) {
159-
return Int(bitPattern: CFHashBytes(unsafeBitCast($0, to: UnsafeMutablePointer<UInt8>.self), CFIndex(MemoryLayout<UInt>.size * 2)))
158+
return withUnsafeMutablePointer(to: &buf) { bufferPtr in
159+
let count = MemoryLayout<UInt>.size * 2
160+
return bufferPtr.withMemoryRebound(to: UInt8.self, capacity: count) { bufferBytes in
161+
return Int(bitPattern: CFHashBytes(bufferBytes, CFIndex(count)))
162+
}
160163
}
161164
}
162165

Foundation/NSArray.swift

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
130130
// }
131131
let cnt = array.count
132132
let buffer = UnsafeMutablePointer<AnyObject>.allocate(capacity: cnt)
133-
buffer.initialize(from: optionalArray)
133+
buffer.initialize(from: optionalArray, count: cnt)
134134
self.init(objects: buffer, count: cnt)
135135
buffer.deinitialize(count: cnt)
136136
buffer.deallocate(capacity: cnt)
@@ -372,16 +372,17 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
372372
}
373373

374374
open var sortedArrayHint: Data {
375-
let size = count
376-
let buffer = UnsafeMutablePointer<Int32>.allocate(capacity: size)
377-
for idx in 0..<count {
378-
let item = object(at: idx) as! NSObject
379-
let hash = item.hash
380-
buffer.advanced(by: idx).pointee = Int32(hash).littleEndian
375+
let size = MemoryLayout<Int32>.size * count
376+
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: size)
377+
buffer.withMemoryRebound(to: Int32.self, capacity: count) { (ptr) in
378+
for idx in 0..<count {
379+
let item = object(at: idx) as! NSObject
380+
let hash = item.hash
381+
ptr.advanced(by: idx).pointee = Int32(hash).littleEndian
382+
}
381383
}
382-
return Data(bytesNoCopy: unsafeBitCast(buffer, to: UnsafeMutablePointer<UInt8>.self), count: count * MemoryLayout<Int>.size, deallocator: .custom({ _ in
384+
return Data(bytesNoCopy: buffer, count: size, deallocator: .custom({ _ in
383385
buffer.deallocate(capacity: size)
384-
buffer.deinitialize(count: size)
385386
}))
386387
}
387388

Foundation/NSDictionary.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,10 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
124124

125125
public convenience init(objects: [Any], forKeys keys: [NSObject]) {
126126
let keyBuffer = UnsafeMutablePointer<NSObject>.allocate(capacity: keys.count)
127-
keyBuffer.initialize(from: keys)
127+
keyBuffer.initialize(from: keys, count: keys.count)
128128

129129
let valueBuffer = UnsafeMutablePointer<AnyObject>.allocate(capacity: objects.count)
130-
valueBuffer.initialize(from: objects.map { _SwiftValue.store($0) })
130+
valueBuffer.initialize(from: objects.map { _SwiftValue.store($0) }, count: objects.count)
131131

132132
self.init(objects: valueBuffer, forKeys:keyBuffer, count: keys.count)
133133

Foundation/Process.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ open class Process: NSObject {
223223
let argv : UnsafeMutablePointer<UnsafeMutablePointer<Int8>?> = args.withUnsafeBufferPointer {
224224
let array : UnsafeBufferPointer<String> = $0
225225
let buffer = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>.allocate(capacity: array.count + 1)
226-
buffer.initialize(from: array.map { $0.withCString(strdup) })
226+
buffer.initialize(from: array.map { $0.withCString(strdup) }, count: array.count)
227227
buffer[array.count] = nil
228228
return buffer
229229
}
@@ -241,7 +241,7 @@ open class Process: NSObject {
241241
if let env = environment {
242242
let nenv = env.count
243243
envp = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>.allocate(capacity: 1 + nenv)
244-
envp.initialize(from: env.map { strdup("\($0)=\($1)") })
244+
envp.initialize(from: env.map { strdup("\($0)=\($1)") }, count: 1 + nenv)
245245
envp[env.count] = nil
246246
} else {
247247
envp = _CFEnviron()

TestFoundation/TestNSData.swift

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -761,8 +761,9 @@ extension TestNSData {
761761
let expectedSize = MemoryLayout<UInt8>.stride * a.count
762762
XCTAssertEqual(expectedSize, data.count)
763763

764-
let underlyingBuffer = unsafeBitCast(malloc(expectedSize - 1)!, to: UnsafeMutablePointer<UInt8>.self)
765-
let buffer = UnsafeMutableBufferPointer(start: underlyingBuffer, count: expectedSize - 1)
764+
let size = expectedSize - 1
765+
let underlyingBuffer = malloc(size)!
766+
let buffer = UnsafeMutableBufferPointer(start: underlyingBuffer.bindMemory(to: UInt8.self, capacity: size), count: size)
766767

767768
// We should only copy in enough bytes that can fit in the buffer
768769
let copiedCount = data.copyBytes(to: buffer)
@@ -784,9 +785,10 @@ extension TestNSData {
784785
}
785786
let expectedSize = MemoryLayout<Int32>.stride * a.count
786787
XCTAssertEqual(expectedSize, data.count)
787-
788-
let underlyingBuffer = unsafeBitCast(malloc(expectedSize + 1)!, to: UnsafeMutablePointer<UInt8>.self)
789-
let buffer = UnsafeMutableBufferPointer(start: underlyingBuffer, count: expectedSize + 1)
788+
789+
let size = expectedSize + 1
790+
let underlyingBuffer = malloc(size)!
791+
let buffer = UnsafeMutableBufferPointer(start: underlyingBuffer.bindMemory(to: UInt8.self, capacity: size), count: size)
790792

791793
let copiedCount = data.copyBytes(to: buffer)
792794
XCTAssertEqual(expectedSize, copiedCount)
@@ -802,9 +804,10 @@ extension TestNSData {
802804
var data = a.withUnsafeBufferPointer {
803805
return Data(buffer: $0)
804806
}
805-
806-
let underlyingBuffer = unsafeBitCast(malloc(data.count)!, to: UnsafeMutablePointer<UInt8>.self)
807-
let buffer = UnsafeMutableBufferPointer(start: underlyingBuffer, count: data.count)
807+
808+
let size = data.count
809+
let underlyingBuffer = malloc(size)!
810+
let buffer = UnsafeMutableBufferPointer(start: underlyingBuffer.bindMemory(to: UInt8.self, capacity: size), count: size)
808811

809812
var copiedCount : Int
810813

@@ -831,10 +834,11 @@ extension TestNSData {
831834
let data = a.withUnsafeBufferPointer {
832835
return Data(buffer: $0)
833836
}
834-
835-
let underlyingBuffer = unsafeBitCast(malloc(10)!, to: UnsafeMutablePointer<UInt8>.self)
836-
let buffer = UnsafeMutableBufferPointer(start: underlyingBuffer, count: 10)
837-
837+
838+
let size = 10
839+
let underlyingBuffer = malloc(size)!
840+
let buffer = UnsafeMutableBufferPointer(start: underlyingBuffer.bindMemory(to: UInt8.self, capacity: size), count: size)
841+
838842
var copiedCount : Int
839843

840844
copiedCount = data.copyBytes(to: buffer, from: 0..<3)
@@ -854,9 +858,10 @@ extension TestNSData {
854858
let data = a.withUnsafeBufferPointer {
855859
return Data(buffer: $0)
856860
}
857-
858-
let underlyingBuffer = unsafeBitCast(malloc(4)!, to: UnsafeMutablePointer<UInt8>.self)
859-
let buffer = UnsafeMutableBufferPointer(start: underlyingBuffer, count: 4)
861+
862+
let size = 4
863+
let underlyingBuffer = malloc(size)!
864+
let buffer = UnsafeMutableBufferPointer(start: underlyingBuffer.bindMemory(to: UInt8.self, capacity: size), count: size)
860865

861866
var copiedCount : Int
862867

@@ -983,9 +988,9 @@ extension TestNSData {
983988
expectedSize += MemoryLayout<Bool>.stride * 2
984989
XCTAssertEqual(expectedSize, data.count)
985990

986-
let underlyingBuffer = unsafeBitCast(malloc(expectedSize)!, to: UnsafeMutablePointer<UInt8>.self)
987-
988-
let buffer = UnsafeMutableBufferPointer(start: underlyingBuffer, count: expectedSize)
991+
let size = expectedSize
992+
let underlyingBuffer = malloc(size)!
993+
let buffer = UnsafeMutableBufferPointer(start: underlyingBuffer.bindMemory(to: UInt8.self, capacity: size), count: size)
989994
let copiedCount = data.copyBytes(to: buffer)
990995
XCTAssertEqual(copiedCount, expectedSize)
991996

0 commit comments

Comments
 (0)