-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Fix more warnings for deprecation and unsafeBitCast() #953
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Foundation/DateInterval.swift
Outdated
@@ -156,7 +156,10 @@ public struct DateInterval : ReferenceConvertible, Comparable, Hashable { | |||
public var hashValue: Int { | |||
var buf: (UInt, UInt) = (UInt(start.timeIntervalSinceReferenceDate), UInt(end.timeIntervalSinceReferenceDate)) | |||
return withUnsafeMutablePointer(to: &buf) { | |||
return Int(bitPattern: CFHashBytes(unsafeBitCast($0, to: UnsafeMutablePointer<UInt8>.self), CFIndex(MemoryLayout<UInt>.size * 2))) | |||
let count = MemoryLayout<UInt>.size * 2 | |||
return $0.withMemoryRebound(to: UInt8.self, capacity: count) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Repeated use of implicit arguments makes me nervous - if nothing else about readability. Can you give these names?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@CodaFi I've added some names
Foundation/NSArray.swift
Outdated
@@ -130,7 +130,7 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo | |||
// } | |||
let cnt = array.count | |||
let buffer = UnsafeMutablePointer<AnyObject>.allocate(capacity: cnt) | |||
buffer.initialize(from: optionalArray) | |||
_ = UnsafeMutableBufferPointer(start: buffer, count: cnt).initialize(from: optionalArray) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like programming via side-effect. Is there some more explicit way we could express this?
- Use UnsafeMutablePointer.initialize(from:count:) as a better alternative to UnsafeMutableBufferPointer.initialize(from:)
That looks much better, thanks. cc @phausler |
Agreed about the |
@pushkarnk Could you run a test on this PR please |
@swift-ci please test |
1 similar comment
@swift-ci please test |
@parkera Can this be merged now? |
@phausler Could you have a look at this PR? It fixes up quite a few warnings |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall looks fine to me, provided the rebind of the memory does not cause an allocation I don't see an issue with this.
return Int(bitPattern: CFHashBytes(unsafeBitCast($0, to: UnsafeMutablePointer<UInt8>.self), CFIndex(MemoryLayout<UInt>.size * 2))) | ||
return withUnsafeMutablePointer(to: &buf) { bufferPtr in | ||
let count = MemoryLayout<UInt>.size * 2 | ||
return bufferPtr.withMemoryRebound(to: UInt8.self, capacity: count) { bufferBytes in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this potentially allocate?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nothing here seems to indicate an allocation, unless there is something Im not seeing
@@ -155,8 +155,11 @@ public struct DateInterval : ReferenceConvertible, Comparable, Hashable { | |||
|
|||
public var hashValue: Int { | |||
var buf: (UInt, UInt) = (UInt(start.timeIntervalSinceReferenceDate), UInt(end.timeIntervalSinceReferenceDate)) | |||
return withUnsafeMutablePointer(to: &buf) { | |||
return Int(bitPattern: CFHashBytes(unsafeBitCast($0, to: UnsafeMutablePointer<UInt8>.self), CFIndex(MemoryLayout<UInt>.size * 2))) | |||
return withUnsafeMutablePointer(to: &buf) { bufferPtr in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should consider this for the overlay as well
Thanks! |
Convert unsafeBitCast() to bindMemory()/withMemoryRebound()
Convert deprecated initialize(from:) to UnsafeMutableBufferPointer.initialize(from:)