Make `Defaults.AnyKey` conform to `Equatable` and `Hashable` (#104)

This commit is contained in:
Sindre Sorhus 2022-06-04 19:08:34 +07:00 committed by GitHub
parent d1e2109fc9
commit 001adc694b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 0 deletions

View File

@ -90,3 +90,17 @@ extension Defaults.Key {
self.init(key, default: nil, suite: suite)
}
}
extension Defaults.AnyKey: Equatable {
public static func == (lhs: Defaults.AnyKey, rhs: Defaults.AnyKey) -> Bool {
lhs.name == rhs.name
&& lhs.suite == rhs.suite
}
}
extension Defaults.AnyKey: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(name)
hasher.combine(suite)
}
}

View File

@ -639,4 +639,12 @@ final class DefaultsTests: XCTestCase {
cancellable.cancel()
waitForExpectations(timeout: 10)
}
func testKeyEquatable() {
XCTAssertEqual(Defaults.Key<Bool>("equatableKeyTest", default: false), Defaults.Key<Bool>("equatableKeyTest", default: false))
}
func testKeyHashable() {
_ = Set([Defaults.Key<Bool>("hashableKeyTest", default: false)])
}
}