Skip to content

Commit

Permalink
Drop the Hashable support for < 4.2
Browse files Browse the repository at this point in the history
Progress on #1210
  • Loading branch information
thomasvl committed Apr 7, 2022
1 parent bed7f97 commit 41410bc
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 439 deletions.
15 changes: 0 additions & 15 deletions Sources/SwiftProtobuf/AnyMessageStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@

import Foundation

#if !swift(>=4.2)
private let i_2166136261 = Int(bitPattern: 2166136261)
private let i_16777619 = Int(16777619)
#endif

fileprivate func serializeAnyJSON(
for message: Message,
typeURL: String,
Expand Down Expand Up @@ -323,7 +318,6 @@ extension AnyMessageStorage {
/// test. Of course, regardless of the above, we must guarantee that
/// hashValue is compatible with equality.
extension AnyMessageStorage {
#if swift(>=4.2)
// Can't use _valueData for a few reasons:
// 1. Since decode is done on demand, two objects could be equal
// but created differently (one from JSON, one for Message, etc.),
Expand All @@ -338,15 +332,6 @@ extension AnyMessageStorage {
hasher.combine(_typeURL)
}
}
#else // swift(>=4.2)
var hashValue: Int {
var hash: Int = i_2166136261
if !_typeURL.isEmpty {
hash = (hash &* i_16777619) ^ _typeURL.hashValue
}
return hash
}
#endif // swift(>=4.2)

func isEqualTo(other: AnyMessageStorage) -> Bool {
if (_typeURL != other._typeURL) {
Expand Down
6 changes: 0 additions & 6 deletions Sources/SwiftProtobuf/Enum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,9 @@ public protocol Enum: RawRepresentable, Hashable, CaseIterable, _ProtoSendable {
}

extension Enum {
#if swift(>=4.2)
public func hash(into hasher: inout Hasher) {
hasher.combine(rawValue)
}
#else // swift(>=4.2)
public var hashValue: Int {
return rawValue
}
#endif // swift(>=4.2)

/// Internal convenience property representing the name of the enum value (or
/// `nil` if it is an `UNRECOGNIZED` value or doesn't provide names).
Expand Down
11 changes: 0 additions & 11 deletions Sources/SwiftProtobuf/ExtensionFieldValueSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public struct ExtensionFieldValueSet: Hashable {

public init() {}

#if swift(>=4.2)
public func hash(into hasher: inout Hasher) {
// AnyExtensionField is not Hashable, and the Self constraint that would
// add breaks some of the uses of it; so the only choice is to manually
Expand All @@ -56,16 +55,6 @@ public struct ExtensionFieldValueSet: Hashable {
}
hasher.combine(hash)
}
#else // swift(>=4.2)
public var hashValue: Int {
var hash = 16777619
for (fieldNumber, v) in values {
// Note: This calculation cannot depend on the order of the items.
hash = hash &+ fieldNumber &+ v.hashValue
}
return hash
}
#endif // swift(>=4.2)

public func traverse<V: Visitor>(visitor: inout V, start: Int, end: Int) throws {
let validIndexes = values.keys.filter {$0 >= start && $0 < end}
Expand Down
101 changes: 0 additions & 101 deletions Sources/SwiftProtobuf/ExtensionFields.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@
///
// -----------------------------------------------------------------------------

#if !swift(>=4.2)
private let i_2166136261 = Int(bitPattern: 2166136261)
private let i_16777619 = Int(16777619)
#endif

// TODO: `AnyExtensionField` should require `Sendable` but we cannot do so yet without possibly breaking compatibility.

//
Expand All @@ -30,11 +25,7 @@ private let i_16777619 = Int(16777619)
// so you can't actually access the contained value itself.
//
public protocol AnyExtensionField: CustomDebugStringConvertible {
#if swift(>=4.2)
func hash(into hasher: inout Hasher)
#else
var hashValue: Int { get }
#endif
var protobufExtension: AnyMessageExtension { get }
func isEqual(other: AnyExtensionField) -> Bool

Expand Down Expand Up @@ -89,15 +80,9 @@ public struct OptionalExtensionField<T: FieldType>: ExtensionField {
}
}

#if swift(>=4.2)
public func hash(into hasher: inout Hasher) {
hasher.combine(value)
}
#else // swift(>=4.2)
public var hashValue: Int {
get { return value.hashValue }
}
#endif // swift(>=4.2)

public func isEqual(other: AnyExtensionField) -> Bool {
let o = other as! OptionalExtensionField<T>
Expand Down Expand Up @@ -146,21 +131,9 @@ public struct RepeatedExtensionField<T: FieldType>: ExtensionField {
self.value = value
}

#if swift(>=4.2)
public func hash(into hasher: inout Hasher) {
hasher.combine(value)
}
#else // swift(>=4.2)
public var hashValue: Int {
get {
var hash = i_2166136261
for e in value {
hash = (hash &* i_16777619) ^ e.hashValue
}
return hash
}
}
#endif // swift(>=4.2)

public func isEqual(other: AnyExtensionField) -> Bool {
let o = other as! RepeatedExtensionField<T>
Expand Down Expand Up @@ -210,21 +183,9 @@ public struct PackedExtensionField<T: FieldType>: ExtensionField {
self.value = value
}

#if swift(>=4.2)
public func hash(into hasher: inout Hasher) {
hasher.combine(value)
}
#else // swift(>=4.2)
public var hashValue: Int {
get {
var hash = i_2166136261
for e in value {
hash = (hash &* i_16777619) ^ e.hashValue
}
return hash
}
}
#endif // swift(>=4.2)

public func isEqual(other: AnyExtensionField) -> Bool {
let o = other as! PackedExtensionField<T>
Expand Down Expand Up @@ -277,15 +238,9 @@ public struct OptionalEnumExtensionField<E: Enum>: ExtensionField where E.RawVal
}
}

#if swift(>=4.2)
public func hash(into hasher: inout Hasher) {
hasher.combine(value)
}
#else // swift(>=4.2)
public var hashValue: Int {
get { return value.hashValue }
}
#endif // swift(>=4.2)

public func isEqual(other: AnyExtensionField) -> Bool {
let o = other as! OptionalEnumExtensionField<E>
Expand Down Expand Up @@ -336,21 +291,9 @@ public struct RepeatedEnumExtensionField<E: Enum>: ExtensionField where E.RawVal
self.value = value
}

#if swift(>=4.2)
public func hash(into hasher: inout Hasher) {
hasher.combine(value)
}
#else // swift(>=4.2)
public var hashValue: Int {
get {
var hash = i_2166136261
for e in value {
hash = (hash &* i_16777619) ^ e.hashValue
}
return hash
}
}
#endif // swift(>=4.2)

public func isEqual(other: AnyExtensionField) -> Bool {
let o = other as! RepeatedEnumExtensionField<E>
Expand Down Expand Up @@ -402,21 +345,9 @@ public struct PackedEnumExtensionField<E: Enum>: ExtensionField where E.RawValue
self.value = value
}

#if swift(>=4.2)
public func hash(into hasher: inout Hasher) {
hasher.combine(value)
}
#else // swift(>=4.2)
public var hashValue: Int {
get {
var hash = i_2166136261
for e in value {
hash = (hash &* i_16777619) ^ e.hashValue
}
return hash
}
}
#endif // swift(>=4.2)

public func isEqual(other: AnyExtensionField) -> Bool {
let o = other as! PackedEnumExtensionField<E>
Expand Down Expand Up @@ -472,13 +403,9 @@ public struct OptionalMessageExtensionField<M: Message & Equatable>:
}
}

#if swift(>=4.2)
public func hash(into hasher: inout Hasher) {
value.hash(into: &hasher)
}
#else // swift(>=4.2)
public var hashValue: Int {return value.hashValue}
#endif // swift(>=4.2)

public func isEqual(other: AnyExtensionField) -> Bool {
let o = other as! OptionalMessageExtensionField<M>
Expand Down Expand Up @@ -530,23 +457,11 @@ public struct RepeatedMessageExtensionField<M: Message & Equatable>:
self.value = value
}

#if swift(>=4.2)
public func hash(into hasher: inout Hasher) {
for e in value {
e.hash(into: &hasher)
}
}
#else // swift(>=4.2)
public var hashValue: Int {
get {
var hash = i_2166136261
for e in value {
hash = (hash &* i_16777619) ^ e.hashValue
}
return hash
}
}
#endif // swift(>=4.2)

public func isEqual(other: AnyExtensionField) -> Bool {
let o = other as! RepeatedMessageExtensionField<M>
Expand Down Expand Up @@ -602,13 +517,9 @@ public struct OptionalGroupExtensionField<G: Message & Hashable>:
self.value = value
}

#if swift(>=4.2)
public func hash(into hasher: inout Hasher) {
hasher.combine(value)
}
#else // swift(>=4.2)
public var hashValue: Int {return value.hashValue}
#endif // swift(>=4.2)

public var debugDescription: String { get {return value.debugDescription} }

Expand Down Expand Up @@ -662,21 +573,9 @@ public struct RepeatedGroupExtensionField<G: Message & Hashable>:
self.value = value
}

#if swift(>=4.2)
public func hash(into hasher: inout Hasher) {
hasher.combine(value)
}
#else // swift(>=4.2)
public var hashValue: Int {
get {
var hash = i_2166136261
for e in value {
hash = (hash &* i_16777619) ^ e.hashValue
}
return hash
}
}
#endif // swift(>=4.2)

public var debugDescription: String {
return "[" + value.map{$0.debugDescription}.joined(separator: ",") + "]"
Expand Down
6 changes: 0 additions & 6 deletions Sources/SwiftProtobuf/Google_Protobuf_Any+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,9 @@ extension Google_Protobuf_Any {
return _storage.isA(type)
}

#if swift(>=4.2)
public func hash(into hasher: inout Hasher) {
_storage.hash(into: &hasher)
}
#else // swift(>=4.2)
public var hashValue: Int {
return _storage.hashValue
}
#endif // swift(>=4.2)
}

extension Google_Protobuf_Any {
Expand Down
Loading

0 comments on commit 41410bc

Please sign in to comment.