Skip to content

Commit 6b39ae4

Browse files
committed
Add custom AnyHashable representations to Foundation types
This ensures bridgable values are interchangeable with their bridged counterparts when converted to AnyHashable, improving the usefulness of AnyHashable and increasing consistency across platforms.
1 parent 707896f commit 6b39ae4

20 files changed

+130
-21
lines changed

Foundation/AffineTransform.swift

+6
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,12 @@ extension NSAffineTransform : _StructTypeBridgeable {
517517
}
518518
}
519519

520+
extension NSAffineTransform : _HasCustomAnyHashableRepresentation {
521+
public func _toCustomAnyHashable() -> AnyHashable? {
522+
return AnyHashable(self as AffineTransform)
523+
}
524+
}
525+
520526
extension AffineTransform : Codable {
521527
public init(from decoder: Decoder) throws {
522528
var container = try decoder.unkeyedContainer()

Foundation/Data.swift

+1-3
Original file line numberDiff line numberDiff line change
@@ -1935,10 +1935,8 @@ extension Data : _ObjectiveCBridgeable {
19351935
}
19361936

19371937
extension NSData : _HasCustomAnyHashableRepresentation {
1938-
// Must be @nonobjc to avoid infinite recursion during bridging.
1939-
@nonobjc
19401938
public func _toCustomAnyHashable() -> AnyHashable? {
1941-
return AnyHashable(Data._unconditionallyBridgeFromObjectiveC(self))
1939+
return AnyHashable(self as Data)
19421940
}
19431941
}
19441942

Foundation/IndexPath.swift

+1-3
Original file line numberDiff line numberDiff line change
@@ -800,10 +800,8 @@ extension IndexPath : _ObjectiveCBridgeable {
800800
}
801801

802802
extension NSIndexPath : _HasCustomAnyHashableRepresentation {
803-
// Must be @nonobjc to avoid infinite recursion during bridging.
804-
@nonobjc
805803
public func _toCustomAnyHashable() -> AnyHashable? {
806-
return AnyHashable(IndexPath(nsIndexPath: self))
804+
return AnyHashable(self as IndexPath)
807805
}
808806
}
809807

Foundation/IndexSet.swift

+1-3
Original file line numberDiff line numberDiff line change
@@ -824,10 +824,8 @@ extension IndexSet : _ObjectiveCBridgeable {
824824
}
825825

826826
extension NSIndexSet : _HasCustomAnyHashableRepresentation {
827-
// Must be @nonobjc to avoid infinite recursion during bridging.
828-
@nonobjc
829827
public func _toCustomAnyHashable() -> AnyHashable? {
830-
return AnyHashable(IndexSet(reference: self))
828+
return AnyHashable(self as IndexSet)
831829
}
832830
}
833831

Foundation/Measurement.swift

-6
Original file line numberDiff line numberDiff line change
@@ -234,14 +234,8 @@ extension Measurement : _ObjectiveCBridgeable {
234234

235235
@available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
236236
extension NSMeasurement : _HasCustomAnyHashableRepresentation {
237-
// Must be @nonobjc to avoid infinite recursion during bridging.
238-
@nonobjc
239237
public func _toCustomAnyHashable() -> AnyHashable? {
240-
#if DEPLOYMENT_RUNTIME_SWIFT
241-
return AnyHashable(Measurement._unconditionallyBridgeFromObjectiveC(self))
242-
#else
243238
return AnyHashable(self as Measurement)
244-
#endif
245239
}
246240
}
247241

Foundation/NSArray.swift

+6
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,12 @@ open class NSMutableArray : NSArray {
935935
}
936936
}
937937

938+
extension NSArray : _HasCustomAnyHashableRepresentation {
939+
public func _toCustomAnyHashable() -> AnyHashable? {
940+
return AnyHashable(self as! Array<AnyHashable>)
941+
}
942+
}
943+
938944
extension NSArray : Sequence {
939945
final public func makeIterator() -> Iterator {
940946
return Iterator(self)

Foundation/NSCalendar.swift

+12
Original file line numberDiff line numberDiff line change
@@ -1834,3 +1834,15 @@ extension NSDateComponents : _StructTypeBridgeable {
18341834
return DateComponents._unconditionallyBridgeFromObjectiveC(self)
18351835
}
18361836
}
1837+
1838+
extension NSCalendar : _HasCustomAnyHashableRepresentation {
1839+
public func _toCustomAnyHashable() -> AnyHashable? {
1840+
return AnyHashable(self as Calendar)
1841+
}
1842+
}
1843+
1844+
extension NSDateComponents : _HasCustomAnyHashableRepresentation {
1845+
public func _toCustomAnyHashable() -> AnyHashable? {
1846+
return AnyHashable(_bridgeToSwift())
1847+
}
1848+
}

Foundation/NSCharacterSet.swift

+6
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,9 @@ extension NSCharacterSet : _StructTypeBridgeable {
362362
return CharacterSet._unconditionallyBridgeFromObjectiveC(self)
363363
}
364364
}
365+
366+
extension NSCharacterSet : _HasCustomAnyHashableRepresentation {
367+
public func _toCustomAnyHashable() -> AnyHashable? {
368+
return AnyHashable(self as CharacterSet)
369+
}
370+
}

Foundation/NSDate.swift

+11
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,11 @@ extension Date : _NSBridgeable, _CFBridgeable {
246246
internal var _cfObject: CFType { return _nsObject._cfObject }
247247
}
248248

249+
extension NSDate : _HasCustomAnyHashableRepresentation {
250+
public func _toCustomAnyHashable() -> AnyHashable? {
251+
return AnyHashable(self as Date)
252+
}
253+
}
249254

250255
open class NSDateInterval : NSObject, NSCopying, NSSecureCoding {
251256

@@ -419,3 +424,9 @@ extension NSDateInterval : _StructTypeBridgeable {
419424
return DateInterval._unconditionallyBridgeFromObjectiveC(self)
420425
}
421426
}
427+
428+
extension NSDateInterval : _HasCustomAnyHashableRepresentation {
429+
public func _toCustomAnyHashable() -> AnyHashable? {
430+
return AnyHashable(self as DateInterval)
431+
}
432+
}

Foundation/NSDictionary.swift

+6
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,12 @@ extension Dictionary : _NSBridgeable, _CFBridgeable {
571571
internal var _cfObject: CFDictionary { return _nsObject._cfObject }
572572
}
573573

574+
extension NSDictionary : _HasCustomAnyHashableRepresentation {
575+
public func _toCustomAnyHashable() -> AnyHashable? {
576+
return AnyHashable(self as! Dictionary<AnyHashable, AnyHashable>)
577+
}
578+
}
579+
574580
open class NSMutableDictionary : NSDictionary {
575581

576582
open func removeObject(forKey aKey: Any) {

Foundation/NSLocale.swift

+6
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,9 @@ extension NSLocale : _StructTypeBridgeable {
253253
return Locale._unconditionallyBridgeFromObjectiveC(self)
254254
}
255255
}
256+
257+
extension NSLocale : _HasCustomAnyHashableRepresentation {
258+
public func _toCustomAnyHashable() -> AnyHashable? {
259+
return AnyHashable(self as Locale)
260+
}
261+
}

Foundation/NSNotification.swift

+6
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,9 @@ open class NotificationCenter: NSObject {
211211
}
212212

213213
}
214+
215+
extension NSNotification : _HasCustomAnyHashableRepresentation {
216+
public func _toCustomAnyHashable() -> AnyHashable? {
217+
return AnyHashable(self as Notification)
218+
}
219+
}

Foundation/NSNumber.swift

+24
Original file line numberDiff line numberDiff line change
@@ -1098,3 +1098,27 @@ protocol _NSNumberCastingWithoutBridging {
10981098
}
10991099

11001100
extension NSNumber: _NSNumberCastingWithoutBridging {}
1101+
1102+
extension NSNumber : _HasCustomAnyHashableRepresentation {
1103+
public func _toCustomAnyHashable() -> AnyHashable? {
1104+
// Note: This needs to be kept in sync with the Swift stdlib.
1105+
// Don't reorder checks or add new cases without also updating
1106+
// the AnyHashable representations of Swift's native integer
1107+
// types.
1108+
if let nsDecimalNumber: NSDecimalNumber = self as? NSDecimalNumber {
1109+
return AnyHashable(nsDecimalNumber.decimalValue)
1110+
} else if self === kCFBooleanTrue {
1111+
return AnyHashable(true)
1112+
} else if self === kCFBooleanFalse {
1113+
return AnyHashable(false)
1114+
} else if NSNumber(value: int64Value) == self {
1115+
return AnyHashable(int64Value)
1116+
} else if NSNumber(value: uint64Value) == self {
1117+
return AnyHashable(uint64Value)
1118+
} else if NSNumber(value: doubleValue) == self {
1119+
return AnyHashable(doubleValue)
1120+
} else {
1121+
return nil
1122+
}
1123+
}
1124+
}

Foundation/NSSet.swift

+6
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,12 @@ extension Set : _NSBridgeable, _CFBridgeable {
298298
internal var _cfObject: CFSet { return _nsObject._cfObject }
299299
}
300300

301+
extension NSSet : _HasCustomAnyHashableRepresentation {
302+
public func _toCustomAnyHashable() -> AnyHashable? {
303+
return AnyHashable(self as! Set<AnyHashable>)
304+
}
305+
}
306+
301307
extension NSSet : Sequence {
302308
public typealias Iterator = NSEnumerator.Iterator
303309
public func makeIterator() -> Iterator {

Foundation/NSString.swift

+6
Original file line numberDiff line numberDiff line change
@@ -1324,6 +1324,12 @@ extension NSString {
13241324

13251325
extension NSString : ExpressibleByStringLiteral { }
13261326

1327+
extension NSString : _HasCustomAnyHashableRepresentation {
1328+
public func _toCustomAnyHashable() -> AnyHashable? {
1329+
return AnyHashable(self as String)
1330+
}
1331+
}
1332+
13271333
open class NSMutableString : NSString {
13281334
open func replaceCharacters(in range: NSRange, with aString: String) {
13291335
guard type(of: self) === NSString.self || type(of: self) === NSMutableString.self else {

Foundation/NSTimeZone.swift

+6
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,12 @@ extension TimeZone : _NSBridgeable, _CFBridgeable {
276276
var _cfObject : CFTimeZone { return _nsObject._cfObject }
277277
}
278278

279+
extension NSTimeZone : _HasCustomAnyHashableRepresentation {
280+
public func _toCustomAnyHashable() -> AnyHashable? {
281+
return AnyHashable(self._swiftObject)
282+
}
283+
}
284+
279285
extension NSTimeZone {
280286

281287
public enum NameStyle : Int {

Foundation/NSURL.swift

+18
Original file line numberDiff line numberDiff line change
@@ -1321,3 +1321,21 @@ extension NSURLQueryItem : _StructTypeBridgeable {
13211321
}
13221322
}
13231323

1324+
1325+
extension NSURL : _HasCustomAnyHashableRepresentation {
1326+
public func _toCustomAnyHashable() -> AnyHashable? {
1327+
return AnyHashable(self as URL)
1328+
}
1329+
}
1330+
1331+
extension NSURLComponents : _HasCustomAnyHashableRepresentation {
1332+
public func _toCustomAnyHashable() -> AnyHashable? {
1333+
return AnyHashable(self as URLComponents)
1334+
}
1335+
}
1336+
1337+
extension NSURLQueryItem : _HasCustomAnyHashableRepresentation {
1338+
public func _toCustomAnyHashable() -> AnyHashable? {
1339+
return AnyHashable(self as URLQueryItem)
1340+
}
1341+
}

Foundation/NSURLRequest.swift

+6
Original file line numberDiff line numberDiff line change
@@ -567,3 +567,9 @@ extension NSURLRequest : _StructTypeBridgeable {
567567
return URLRequest._unconditionallyBridgeFromObjectiveC(self)
568568
}
569569
}
570+
571+
extension NSURLRequest : _HasCustomAnyHashableRepresentation {
572+
public func _toCustomAnyHashable() -> AnyHashable? {
573+
return AnyHashable(self as URLRequest)
574+
}
575+
}

Foundation/PersonNameComponents.swift

+1-3
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,8 @@ extension PersonNameComponents : _ObjectiveCBridgeable {
131131
}
132132

133133
extension NSPersonNameComponents : _HasCustomAnyHashableRepresentation {
134-
// Must be @nonobjc to avoid infinite recursion during bridging.
135-
@nonobjc
136134
public func _toCustomAnyHashable() -> AnyHashable? {
137-
return AnyHashable(self._bridgeToSwift())
135+
return AnyHashable(self as PersonNameComponents)
138136
}
139137
}
140138

Foundation/UUID.swift

+1-3
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,8 @@ extension UUID : _ObjectiveCBridgeable {
156156
}
157157

158158
extension NSUUID : _HasCustomAnyHashableRepresentation {
159-
// Must be @nonobjc to avoid infinite recursion during bridging.
160-
@nonobjc
161159
public func _toCustomAnyHashable() -> AnyHashable? {
162-
return AnyHashable(UUID._unconditionallyBridgeFromObjectiveC(self))
160+
return AnyHashable(self as UUID)
163161
}
164162
}
165163

0 commit comments

Comments
 (0)