Skip to content

Commit 6c66cf3

Browse files
committed
[stdlib] Make stdlib index types Hashable
This adds Hashable conformance for the String, Dictionary, and Set's index types.
1 parent 16f5e64 commit 6c66cf3

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

stdlib/public/core/HashedCollections.swift.gyb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5967,7 +5967,7 @@ elif Self == 'Dictionary':
59675967

59685968
${SubscriptingWithIndexDoc}
59695969
@_fixed_layout // FIXME(sil-serialize-all)
5970-
public struct Index : Comparable {
5970+
public struct Index : Comparable, Hashable {
59715971
// Index for native buffer is efficient. Index for bridged NS${Self} is
59725972
// not, because neither NSEnumerator nor fast enumeration support moving
59735973
// backwards. Even if they did, there is another issue: NSEnumerator does
@@ -6089,6 +6089,22 @@ extension ${Self}.Index {
60896089
#endif
60906090
}
60916091
}
6092+
6093+
@_inlineable // FIXME(sil-serialize-all)
6094+
public var hashValue: Int {
6095+
if _fastPath(_guaranteedNative) {
6096+
return _nativeIndex.offset
6097+
}
6098+
6099+
switch _value {
6100+
case ._native(let nativeIndex):
6101+
return nativeIndex.offset
6102+
#if _runtime(_ObjC)
6103+
case ._cocoa(let cocoaIndex):
6104+
return cocoaIndex.currentKeyIndex
6105+
#endif
6106+
}
6107+
}
60926108
}
60936109

60946110
#if _runtime(_ObjC)

stdlib/public/core/StringIndex.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ extension String.Index : Comparable {
6767
}
6868
}
6969

70+
extension String.Index : Hashable {
71+
@_inlineable // FIXME(sil-serialize-all)
72+
public var hashValue: Int {
73+
return _compoundOffset.hashValue
74+
}
75+
}
76+
7077
extension String.Index {
7178
internal typealias _Self = String.Index
7279

0 commit comments

Comments
 (0)