Skip to content

Commit 91d9250

Browse files
committed
Move Message over to hash(into:) for Swift 4.2.
1 parent a381a62 commit 91d9250

File tree

5 files changed

+199
-85
lines changed

5 files changed

+199
-85
lines changed

Sources/SwiftProtobuf/AnyMessageStorage.swift

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515

1616
import Foundation
1717

18+
#if !swift(>=4.2)
1819
private let i_2166136261 = Int(bitPattern: 2166136261)
1920
private let i_16777619 = Int(16777619)
21+
#endif
2022

2123
fileprivate func serializeAnyJSON(for message: Message, typeURL: String) throws -> String {
2224
var visitor = try JSONEncodingVisitor(message: message)
@@ -295,21 +297,30 @@ extension AnyMessageStorage {
295297
/// test. Of course, regardless of the above, we must guarantee that
296298
/// hashValue is compatible with equality.
297299
extension AnyMessageStorage {
300+
#if swift(>=4.2)
301+
// Can't use _valueData for a few reasons:
302+
// 1. Since decode is done on demand, two objects could be equal
303+
// but created differently (one from JSON, one for Message, etc.),
304+
// and the hash values have to be equal even if we don't have data
305+
// yet.
306+
// 2. map<> serialization order is undefined. At the time of writing
307+
// the Swift, Objective-C, and Go runtimes all tend to have random
308+
// orders, so the messages could be identical, but in binary form
309+
// they could differ.
310+
public func hash(into hasher: inout Hasher) {
311+
if !_typeURL.isEmpty {
312+
hasher.combine(_typeURL)
313+
}
314+
}
315+
#else // swift(>=4.2)
298316
var hashValue: Int {
299317
var hash: Int = i_2166136261
300318
if !_typeURL.isEmpty {
301319
hash = (hash &* i_16777619) ^ _typeURL.hashValue
302320
}
303-
// Can't use _valueData for a few reasons:
304-
// 1. Since decode is done on demand, two objects could be equal
305-
// but created differently (one from JSON, one for Message, etc.),
306-
// and the hashes have to be equal even if we don't have data yet.
307-
// 2. map<> serialization order is undefined. At the time of writing
308-
// the Swift, Objective-C, and Go runtimes all tend to have random
309-
// orders, so the messages could be identical, but in binary form
310-
// they could differ.
311321
return hash
312322
}
323+
#endif // swift(>=4.2)
313324

314325
func isEqualTo(other: AnyMessageStorage) -> Bool {
315326
if (_typeURL != other._typeURL) {

Sources/SwiftProtobuf/ExtensionFields.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ public struct OptionalMessageExtensionField<M: Message & Equatable>:
472472

473473
#if swift(>=4.2)
474474
public func hash(into hasher: inout Hasher) {
475-
hasher.combine(value.hashValue) // TODO(thomasvl): Fix this once message is updated!
475+
value.hash(into: &hasher)
476476
}
477477
#else // swift(>=4.2)
478478
public var hashValue: Int {return value.hashValue}
@@ -531,7 +531,7 @@ public struct RepeatedMessageExtensionField<M: Message & Equatable>:
531531
#if swift(>=4.2)
532532
public func hash(into hasher: inout Hasher) {
533533
for e in value {
534-
hasher.combine(e.hashValue) // TODO(thomasvl): Fix this once message is updated!
534+
e.hash(into: &hasher)
535535
}
536536
}
537537
#else // swift(>=4.2)
@@ -602,7 +602,7 @@ public struct OptionalGroupExtensionField<G: Message & Hashable>:
602602

603603
#if swift(>=4.2)
604604
public func hash(into hasher: inout Hasher) {
605-
hasher.combine(value.hashValue) // TODO(thomasvl): Fix this once message is updated!
605+
hasher.combine(value)
606606
}
607607
#else // swift(>=4.2)
608608
public var hashValue: Int {return value.hashValue}
@@ -662,9 +662,7 @@ public struct RepeatedGroupExtensionField<G: Message & Hashable>:
662662

663663
#if swift(>=4.2)
664664
public func hash(into hasher: inout Hasher) {
665-
for e in value {
666-
hasher.combine(e.hashValue) // TODO(thomasvl): Fix this once message is updated!
667-
}
665+
hasher.combine(value)
668666
}
669667
#else // swift(>=4.2)
670668
public var hashValue: Int {

Sources/SwiftProtobuf/Google_Protobuf_Any+Extensions.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,15 @@ public extension Google_Protobuf_Any {
9393
return _storage.isA(type)
9494
}
9595

96+
#if swift(>=4.2)
97+
public func hash(into hasher: inout Hasher) {
98+
_storage.hash(into: &hasher)
99+
}
100+
#else // swift(>=4.2)
96101
public var hashValue: Int {
97102
return _storage.hashValue
98103
}
104+
#endif // swift(>=4.2)
99105
}
100106

101107
extension Google_Protobuf_Any {

0 commit comments

Comments
 (0)