Skip to content

Commit 992afa2

Browse files
committed
Move Enum and _NameMap.Name to the Swift 4.2 Hashable approach.
1 parent 70469c8 commit 992afa2

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

Sources/SwiftProtobuf/Enum.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,15 @@ public protocol Enum: RawRepresentable, Hashable {
3939
}
4040

4141
extension Enum {
42+
#if swift(>=4.2)
43+
public func hash(into hasher: inout Hasher) {
44+
hasher.combine(rawValue)
45+
}
46+
#else // swift(>=4.2)
4247
public var hashValue: Int {
4348
return rawValue
4449
}
50+
#endif // swift(>=4.2)
4551

4652
/// Internal convenience property representing the name of the enum value (or
4753
/// `nil` if it is an `UNRECOGNIZED` value or doesn't provide names).

Sources/SwiftProtobuf/NameMap.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,11 @@ fileprivate class InternPool {
6868
}
6969
}
7070

71+
#if !swift(>=4.2)
7172
// Constants for FNV hash http://tools.ietf.org/html/draft-eastlake-fnv-03
7273
private let i_2166136261 = Int(bitPattern: 2166136261)
7374
private let i_16777619 = Int(16777619)
75+
#endif
7476

7577
/// An immutable bidirectional mapping between field/enum-case names
7678
/// and numbers, used to record field names for text-based
@@ -127,13 +129,21 @@ public struct _NameMap: ExpressibleByDictionaryLiteral {
127129
}
128130
}
129131

132+
#if swift(>=4.2)
133+
public func hash(into hasher: inout Hasher) {
134+
for byte in utf8Buffer {
135+
hasher.combine(byte)
136+
}
137+
}
138+
#else // swift(>=4.2)
130139
public var hashValue: Int {
131140
var h = i_2166136261
132141
for byte in utf8Buffer {
133142
h = (h ^ Int(byte)) &* i_16777619
134143
}
135144
return h
136145
}
146+
#endif // swift(>=4.2)
137147

138148
public static func ==(lhs: Name, rhs: Name) -> Bool {
139149
if lhs.utf8Buffer.count != rhs.utf8Buffer.count {

0 commit comments

Comments
 (0)