Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Sources/Identity/Identity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,18 @@ extension Identifier: CustomStringConvertible {
}
}

// MARK: - Hashable support

extension Identifier: Hashable where Value.RawIdentifier: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(ObjectIdentifier(Value.self))
hasher.combine(rawValue)
}
}

// MARK: - Compiler-generated protocol support

extension Identifier: Equatable where Value.RawIdentifier: Equatable {}
extension Identifier: Hashable where Value.RawIdentifier: Hashable {}

// MARK: - Codable support

Expand Down
18 changes: 17 additions & 1 deletion Tests/IdentityTests/IdentityTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,21 @@ final class IdentityTests: XCTestCase {
XCTAssertEqual(intID.description, "7")
}

func testIdentifierHashValue() {
struct FirstModel: Identifiable {
let id: ID
}

struct SecondModel: Identifiable {
let id: ID
}

let first = FirstModel(id: "0")
let second = SecondModel(id: "0")

XCTAssertNotEqual(first.id.hashValue, second.id.hashValue)
}

func testAllTestsRunOnLinux() {
verifyAllTestsRunOnLinux()
}
Expand All @@ -88,6 +103,7 @@ extension IdentityTests: LinuxTestable {
("testCodableIdentifier", testCodableIdentifier),
("testIdentifierEncodedAsSingleValue", testIdentifierEncodedAsSingleValue),
("testExpressingIdentifierUsingStringInterpolation", testExpressingIdentifierUsingStringInterpolation),
("testIdentifierDescription", testIdentifierDescription)
("testIdentifierDescription", testIdentifierDescription),
("testIdentifierHashValue", testIdentifierHashValue),
]
}