diff --git a/Sources/SwiftProtobuf/HashVisitor.swift b/Sources/SwiftProtobuf/HashVisitor.swift index 78add788f..4a03e278f 100644 --- a/Sources/SwiftProtobuf/HashVisitor.swift +++ b/Sources/SwiftProtobuf/HashVisitor.swift @@ -34,6 +34,7 @@ internal struct HashVisitor: Visitor { hashValue = (hashValue ^ hash) &* i_16777619 } +#if !swift(>=4.2) private mutating func mixMap(map: Dictionary) { var mapHash = 0 for (k, v) in map { @@ -42,7 +43,7 @@ internal struct HashVisitor: Visitor { } mix(mapHash) } - +#endif init() {} @@ -81,7 +82,7 @@ internal struct HashVisitor: Visitor { } mutating func visitSingularEnumField(value: E, - fieldNumber: Int) { + fieldNumber: Int) { mix(fieldNumber) mix(value.hashValue) } @@ -91,32 +92,236 @@ internal struct HashVisitor: Visitor { mix(value.hashValue) } + mutating func visitRepeatedFloatField(value: [Float], fieldNumber: Int) throws { + mix(fieldNumber) + #if swift(>=4.2) + mix(value.hashValue) + #else + for v in value { + mix(v.hashValue) + } + #endif + } + + mutating func visitRepeatedDoubleField(value: [Double], fieldNumber: Int) throws { + mix(fieldNumber) + #if swift(>=4.2) + mix(value.hashValue) + #else + for v in value { + mix(v.hashValue) + } + #endif + } + + mutating func visitRepeatedInt32Field(value: [Int32], fieldNumber: Int) throws { + mix(fieldNumber) + #if swift(>=4.2) + mix(value.hashValue) + #else + for v in value { + mix(v.hashValue) + } + #endif + } + + mutating func visitRepeatedInt64Field(value: [Int64], fieldNumber: Int) throws { + mix(fieldNumber) + #if swift(>=4.2) + mix(value.hashValue) + #else + for v in value { + mix(v.hashValue) + } + #endif + } + + mutating func visitRepeatedUInt32Field(value: [UInt32], fieldNumber: Int) throws { + mix(fieldNumber) + #if swift(>=4.2) + mix(value.hashValue) + #else + for v in value { + mix(v.hashValue) + } + #endif + } + + mutating func visitRepeatedUInt64Field(value: [UInt64], fieldNumber: Int) throws { + mix(fieldNumber) + #if swift(>=4.2) + mix(value.hashValue) + #else + for v in value { + mix(v.hashValue) + } + #endif + } + + mutating func visitRepeatedSInt32Field(value: [Int32], fieldNumber: Int) throws { + mix(fieldNumber) + #if swift(>=4.2) + mix(value.hashValue) + #else + for v in value { + mix(v.hashValue) + } + #endif + } + + mutating func visitRepeatedSInt64Field(value: [Int64], fieldNumber: Int) throws { + mix(fieldNumber) + #if swift(>=4.2) + mix(value.hashValue) + #else + for v in value { + mix(v.hashValue) + } + #endif + } + + mutating func visitRepeatedFixed32Field(value: [UInt32], fieldNumber: Int) throws { + mix(fieldNumber) + #if swift(>=4.2) + mix(value.hashValue) + #else + for v in value { + mix(v.hashValue) + } + #endif + } + + mutating func visitRepeatedFixed64Field(value: [UInt64], fieldNumber: Int) throws { + mix(fieldNumber) + #if swift(>=4.2) + mix(value.hashValue) + #else + for v in value { + mix(v.hashValue) + } + #endif + } + + mutating func visitRepeatedSFixed32Field(value: [Int32], fieldNumber: Int) throws { + mix(fieldNumber) + #if swift(>=4.2) + mix(value.hashValue) + #else + for v in value { + mix(v.hashValue) + } + #endif + } + + mutating func visitRepeatedSFixed64Field(value: [Int64], fieldNumber: Int) throws { + mix(fieldNumber) + #if swift(>=4.2) + mix(value.hashValue) + #else + for v in value { + mix(v.hashValue) + } + #endif + } + + mutating func visitRepeatedBoolField(value: [Bool], fieldNumber: Int) throws { + mix(fieldNumber) + #if swift(>=4.2) + mix(value.hashValue) + #else + for v in value { + mix(v.hashValue) + } + #endif + } + + mutating func visitRepeatedStringField(value: [String], fieldNumber: Int) throws { + mix(fieldNumber) + #if swift(>=4.2) + mix(value.hashValue) + #else + for v in value { + mix(v.hashValue) + } + #endif + } + + mutating func visitRepeatedBytesField(value: [Data], fieldNumber: Int) throws { + mix(fieldNumber) + #if swift(>=4.2) + mix(value.hashValue) + #else + for v in value { + mix(v.hashValue) + } + #endif + } + + mutating func visitRepeatedEnumField(value: [E], fieldNumber: Int) throws { + mix(fieldNumber) + for v in value { + mix(v.hashValue) + } + } + + mutating func visitRepeatedMessageField(value: [M], fieldNumber: Int) throws { + mix(fieldNumber) + #if swift(>=4.2) + mix(value.hashValue) + #else + for v in value { + mix(v.hashValue) + } + #endif + } + + mutating func visitRepeatedGroupField(value: [G], fieldNumber: Int) throws { + mix(fieldNumber) + #if swift(>=4.2) + mix(value.hashValue) + #else + for v in value { + mix(v.hashValue) + } + #endif + } + mutating func visitMapField( fieldType: _ProtobufMap.Type, value: _ProtobufMap.BaseType, fieldNumber: Int ) throws { mix(fieldNumber) - mixMap(map: value) + #if swift(>=4.2) + mix(value.hashValue) + #else + mixMap(map: value) + #endif } - mutating func visitMapField( fieldType: _ProtobufEnumMap.Type, value: _ProtobufEnumMap.BaseType, fieldNumber: Int ) throws where ValueType.RawValue == Int { mix(fieldNumber) - mixMap(map: value) + #if swift(>=4.2) + mix(value.hashValue) + #else + mixMap(map: value) + #endif } - mutating func visitMapField( fieldType: _ProtobufMessageMap.Type, value: _ProtobufMessageMap.BaseType, fieldNumber: Int ) throws { mix(fieldNumber) - mixMap(map: value) + #if swift(>=4.2) + mix(value.hashValue) + #else + mixMap(map: value) + #endif } }