diff --git a/Sources/CSObjectMonitor.swift b/Sources/CSObjectMonitor.swift index 71304b4..6776513 100644 --- a/Sources/CSObjectMonitor.swift +++ b/Sources/CSObjectMonitor.swift @@ -102,8 +102,10 @@ public final class CSObjectMonitor: NSObject { // MARK: NSObject public override var hash: Int { - - return self.bridgeToSwift.hashValue + + var hasher = Hasher() + self.bridgeToSwift.hash(into: &hasher) + return hasher.finalize() } public override func isEqual(_ object: Any?) -> Bool { diff --git a/Sources/CoreStoreError.swift b/Sources/CoreStoreError.swift index 58c3078..824dcd2 100644 --- a/Sources/CoreStoreError.swift +++ b/Sources/CoreStoreError.swift @@ -173,9 +173,6 @@ public enum CoreStoreError: Error, CustomNSError, Hashable { case (let error1 as NSError, let error2 as NSError): return error1.isEqual(error2) - - default: - return false // shouldn't happen } case (.userCancelled, .userCancelled): @@ -188,35 +185,37 @@ public enum CoreStoreError: Error, CustomNSError, Hashable { // MARK: Hashable - - public var hashValue: Int { - - let code = self._code + + public func hash(into hasher: inout Hasher) { + + hasher.combine(self._code) switch self { - + case .unknown: - return code.hashValue - + break + case .differentStorageExistsAtURL(let existingPersistentStoreURL): - return code.hashValue ^ existingPersistentStoreURL.hashValue - + hasher.combine(existingPersistentStoreURL) + case .mappingModelNotFound(let localStoreURL, let targetModel, let targetModelVersion): - return code.hashValue ^ localStoreURL.hashValue ^ targetModel.hashValue ^ targetModelVersion.hashValue - + hasher.combine(localStoreURL) + hasher.combine(targetModel) + hasher.combine(targetModelVersion) + case .progressiveMigrationRequired(let localStoreURL): - return code.hashValue ^ localStoreURL.hashValue - + hasher.combine(localStoreURL) + case .internalError(let nsError): - return code.hashValue ^ nsError.hashValue - + hasher.combine(nsError) + case .userError(let error): - return code.hashValue ^ (error as NSError).hashValue - + hasher.combine(error as NSError) + case .userCancelled: - return code.hashValue + break } } - + // MARK: Internal diff --git a/Sources/CoreStoreObject.swift b/Sources/CoreStoreObject.swift index 339cf46..6662c80 100644 --- a/Sources/CoreStoreObject.swift +++ b/Sources/CoreStoreObject.swift @@ -100,11 +100,12 @@ open /*abstract*/ class CoreStoreObject: DynamicObject, Hashable { // MARK: Hashable - - public var hashValue: Int { - - return ObjectIdentifier(self).hashValue - ^ (self.isMeta ? 0 : self.rawObject!.hashValue) + + public func hash(into hasher: inout Hasher) { + + hasher.combine(self.isMeta) + hasher.combine(ObjectIdentifier(self)) + hasher.combine(self.rawObject) } diff --git a/Sources/CustomSchemaMappingProvider.swift b/Sources/CustomSchemaMappingProvider.swift index 36497ad..f4a90a7 100644 --- a/Sources/CustomSchemaMappingProvider.swift +++ b/Sources/CustomSchemaMappingProvider.swift @@ -146,24 +146,28 @@ public class CustomSchemaMappingProvider: Hashable, SchemaMappingProvider { // MARK: Hashable - - public var hashValue: Int { + + public func hash(into hasher: inout Hasher) { switch self { case .deleteEntity(let sourceEntity): - return sourceEntity.hashValue + hasher.combine(0) + hasher.combine(sourceEntity) case .insertEntity(let destinationEntity): - return destinationEntity.hashValue + hasher.combine(1) + hasher.combine(destinationEntity) case .copyEntity(let sourceEntity, let destinationEntity): - return sourceEntity.hashValue - ^ destinationEntity.hashValue + hasher.combine(2) + hasher.combine(sourceEntity) + hasher.combine(destinationEntity) case .transformEntity(let sourceEntity, let destinationEntity, _): - return sourceEntity.hashValue - ^ destinationEntity.hashValue + hasher.combine(3) + hasher.combine(sourceEntity) + hasher.combine(destinationEntity) } } @@ -324,16 +328,17 @@ public class CustomSchemaMappingProvider: Hashable, SchemaMappingProvider { return lhs.sourceVersion == rhs.sourceVersion && lhs.destinationVersion == rhs.destinationVersion - && type(of: lhs) == type(of: rhs) + && cs_dynamicType(of: lhs) == cs_dynamicType(of: rhs) } // MARK: Hashable - - public var hashValue: Int { - - return self.sourceVersion.hashValue - ^ self.destinationVersion.hashValue + + public func hash(into hasher: inout Hasher) { + + hasher.combine(self.sourceVersion) + hasher.combine(self.destinationVersion) + hasher.combine(ObjectIdentifier(cs_dynamicType(of: self))) } diff --git a/Sources/Entity.swift b/Sources/Entity.swift index 0c7c3be..506893a 100644 --- a/Sources/Entity.swift +++ b/Sources/Entity.swift @@ -167,15 +167,13 @@ public /*abstract*/ class DynamicEntity: Hashable { } // MARK: Hashable - - public var hashValue: Int { - - return ObjectIdentifier(self.type).hashValue - ^ self.entityName.hashValue - ^ self.isAbstract.hashValue - ^ (self.versionHashModifier ?? "").hashValue -// ^ self.indexes.hashValue -// ^ self.uniqueConstraints.hashValue + + public func hash(into hasher: inout Hasher) { + + hasher.combine(ObjectIdentifier(self.type)) + hasher.combine(self.entityName) + hasher.combine(self.isAbstract) + hasher.combine(self.versionHashModifier ?? "") } diff --git a/Sources/EntityIdentifier.swift b/Sources/EntityIdentifier.swift index 3f0c29b..4975214 100644 --- a/Sources/EntityIdentifier.swift +++ b/Sources/EntityIdentifier.swift @@ -85,22 +85,4 @@ internal struct EntityIdentifier: Hashable { self.interfacedClassName = entityDescription.managedObjectClassName! } } - - - // MARK: Equatable - - static func == (lhs: EntityIdentifier, rhs: EntityIdentifier) -> Bool { - - return lhs.category == rhs.category - && lhs.interfacedClassName == rhs.interfacedClassName - } - - - // MARK: Hashable - - var hashValue: Int { - - return self.category.hashValue - ^ self.interfacedClassName.hashValue - } } diff --git a/Sources/GroupBy.swift b/Sources/GroupBy.swift index e65e3a9..9193080 100644 --- a/Sources/GroupBy.swift +++ b/Sources/GroupBy.swift @@ -96,10 +96,10 @@ public struct GroupBy: GroupByClause, QueryClause, Hashable { // MARK: Hashable - - public var hashValue: Int { - - return (self.keyPaths as NSArray).hashValue + + public func hash(into hasher: inout Hasher) { + + hasher.combine(self.keyPaths) } } diff --git a/Sources/InferredSchemaMappingProvider.swift b/Sources/InferredSchemaMappingProvider.swift index ba3416b..0fc6639 100644 --- a/Sources/InferredSchemaMappingProvider.swift +++ b/Sources/InferredSchemaMappingProvider.swift @@ -43,10 +43,10 @@ public final class InferredSchemaMappingProvider: Hashable, SchemaMappingProvide // MARK: Hashable - - public var hashValue: Int { - - return ObjectIdentifier(type(of: self)).hashValue + + public func hash(into hasher: inout Hasher) { + + hasher.combine(ObjectIdentifier(type(of: self))) } diff --git a/Sources/Into.swift b/Sources/Into.swift index 0da98bc..a2fa9ec 100644 --- a/Sources/Into.swift +++ b/Sources/Into.swift @@ -112,12 +112,12 @@ public struct Into: Hashable { // MARK: Hashable - - public var hashValue: Int { - - return ObjectIdentifier(self.entityClass).hashValue - ^ (self.configuration?.hashValue ?? 0) - ^ self.inferStoreIfPossible.hashValue + + public func hash(into hasher: inout Hasher) { + + hasher.combine(ObjectIdentifier(self.entityClass)) + hasher.combine(self.configuration) + hasher.combine(self.inferStoreIfPossible) } diff --git a/Sources/ListMonitor.swift b/Sources/ListMonitor.swift index 1c59125..86ce96e 100644 --- a/Sources/ListMonitor.swift +++ b/Sources/ListMonitor.swift @@ -617,10 +617,10 @@ public final class ListMonitor: Hashable { // MARK: Hashable - - public var hashValue: Int { - - return ObjectIdentifier(self).hashValue + + public func hash(into hasher: inout Hasher) { + + hasher.combine(ObjectIdentifier(self)) } diff --git a/Sources/MigrationResult.swift b/Sources/MigrationResult.swift index 21cd58c..b1bff03 100644 --- a/Sources/MigrationResult.swift +++ b/Sources/MigrationResult.swift @@ -68,40 +68,6 @@ public enum MigrationResult: Hashable { } - // MARK: Equatable - - public static func == (lhs: MigrationResult, rhs: MigrationResult) -> Bool { - - switch (lhs, rhs) { - - case (.success(let migrationTypes1), .success(let migrationTypes2)): - return migrationTypes1 == migrationTypes2 - - case (.failure(let error1), .failure(let error2)): - return error1 == error2 - - default: - return false - } - } - - - // MARK: Hashable - - public var hashValue: Int { - - switch self { - - case .success(let migrationTypes): - return true.hashValue - ^ migrationTypes.map { $0.hashValue }.reduce(0, ^).hashValue - - case .failure(let error): - return false.hashValue ^ error.hashValue - } - } - - // MARK: Internal internal init(_ migrationTypes: [MigrationType]) { diff --git a/Sources/MigrationType.swift b/Sources/MigrationType.swift index c8f474c..5cd92da 100644 --- a/Sources/MigrationType.swift +++ b/Sources/MigrationType.swift @@ -144,20 +144,23 @@ public enum MigrationType: Hashable { // MARK: Hashable - - public var hashValue: Int { - - let preHash = self.hasMigration.hashValue ^ self.isHeavyweightMigration.hashValue + + public func hash(into hasher: inout Hasher) { + + hasher.combine(self.hasMigration) + hasher.combine(self.isHeavyweightMigration) switch self { case .none(let version): - return preHash ^ version.hashValue + hasher.combine(version) case .lightweight(let sourceVersion, let destinationVersion): - return preHash ^ sourceVersion.hashValue ^ destinationVersion.hashValue + hasher.combine(sourceVersion) + hasher.combine(destinationVersion) case .heavyweight(let sourceVersion, let destinationVersion): - return preHash ^ sourceVersion.hashValue ^ destinationVersion.hashValue + hasher.combine(sourceVersion) + hasher.combine(destinationVersion) } } } diff --git a/Sources/ObjectMonitor.swift b/Sources/ObjectMonitor.swift index dd244d9..3bfa47e 100644 --- a/Sources/ObjectMonitor.swift +++ b/Sources/ObjectMonitor.swift @@ -149,10 +149,10 @@ public final class ObjectMonitor: Equatable { // MARK: Hashable - - public var hashValue: Int { - - return ObjectIdentifier(self).hashValue + + public func hash(into hasher: inout Hasher) { + + hasher.combine(ObjectIdentifier(self)) } diff --git a/Sources/OrderBy.swift b/Sources/OrderBy.swift index 2f95665..5bbee52 100644 --- a/Sources/OrderBy.swift +++ b/Sources/OrderBy.swift @@ -132,10 +132,10 @@ public struct OrderBy: OrderByClause, FetchClause, QueryClause // MARK: Hashable - - public var hashValue: Int { - - return (self.sortDescriptors as NSArray).hashValue + + public func hash(into hasher: inout Hasher) { + + hasher.combine(self.sortDescriptors) } diff --git a/Sources/SaveResult.swift b/Sources/SaveResult.swift index 4390e5c..46ee49a 100644 --- a/Sources/SaveResult.swift +++ b/Sources/SaveResult.swift @@ -44,39 +44,6 @@ public enum SaveResult: Hashable { } - // MARK: Equatable - - public static func == (lhs: SaveResult, rhs: SaveResult) -> Bool { - - switch (lhs, rhs) { - - case (.success(let hasChanges1), .success(let hasChanges2)): - return hasChanges1 == hasChanges2 - - case (.failure(let error1), .failure(let error2)): - return error1 == error2 - - default: - return false - } - } - - - // MARK: Hashable - - public var hashValue: Int { - - switch self { - - case .success(let hasChanges): - return self.boolValue.hashValue ^ hasChanges.hashValue - - case .failure(let error): - return self.boolValue.hashValue ^ error.hashValue - } - } - - // MARK: Internal internal init(hasChanges: Bool) { diff --git a/Sources/Select.swift b/Sources/Select.swift index 8a82621..b3cb8b1 100644 --- a/Sources/Select.swift +++ b/Sources/Select.swift @@ -257,19 +257,26 @@ public enum SelectTerm: ExpressibleByStringLiteral, Hashable { // MARK: Hashable - - public var hashValue: Int { - + + public func hash(into hasher: inout Hasher) { + switch self { case ._attribute(let keyPath): - return 0 ^ keyPath.hashValue + hasher.combine(0) + hasher.combine(keyPath) case ._aggregate(let function, let keyPath, let alias, let nativeType): - return 1 ^ function.hashValue ^ keyPath.hashValue ^ alias.hashValue ^ nativeType.hashValue + hasher.combine(1) + hasher.combine(function) + hasher.combine(keyPath) + hasher.combine(alias) + hasher.combine(nativeType) case ._identity(let alias, let nativeType): - return 3 ^ alias.hashValue ^ nativeType.hashValue + hasher.combine(2) + hasher.combine(alias) + hasher.combine(nativeType) } } @@ -700,10 +707,10 @@ public struct Select: SelectClause, Hasha // MARK: Hashable - - public var hashValue: Int { - - return self.selectTerms.map { $0.hashValue }.reduce(0, ^) + + public func hash(into hasher: inout Hasher) { + + hasher.combine(self.selectTerms) } diff --git a/Sources/SetupResult.swift b/Sources/SetupResult.swift index fb37aaa..2a83dbf 100644 --- a/Sources/SetupResult.swift +++ b/Sources/SetupResult.swift @@ -105,16 +105,18 @@ public enum SetupResult: Hashable { // MARK: Hashable - - public var hashValue: Int { + + public func hash(into hasher: inout Hasher) { switch self { case .success(let storage): - return true.hashValue ^ ObjectIdentifier(storage).hashValue + hasher.combine(true) + hasher.combine(ObjectIdentifier(storage)) case .failure(let error): - return false.hashValue ^ error.hashValue + hasher.combine(false) + hasher.combine(error) } } diff --git a/Sources/Where.swift b/Sources/Where.swift index b345a92..ae98cc5 100644 --- a/Sources/Where.swift +++ b/Sources/Where.swift @@ -287,10 +287,10 @@ public struct Where: WhereClauseType, FetchClause, QueryClause // MARK: Hashable - - public var hashValue: Int { - - return self.predicate.hashValue + + public func hash(into hasher: inout Hasher) { + + hasher.combine(self.predicate) } } diff --git a/Sources/XcodeSchemaMappingProvider.swift b/Sources/XcodeSchemaMappingProvider.swift index 512394c..56c92b5 100644 --- a/Sources/XcodeSchemaMappingProvider.swift +++ b/Sources/XcodeSchemaMappingProvider.swift @@ -75,11 +75,11 @@ public final class XcodeSchemaMappingProvider: Hashable, SchemaMappingProvider { // MARK: Hashable - - public var hashValue: Int { - - return self.sourceVersion.hashValue - ^ self.destinationVersion.hashValue + + public func hash(into hasher: inout Hasher) { + + hasher.combine(self.sourceVersion) + hasher.combine(self.destinationVersion) }