Skip to content

Commit a601e3f

Browse files
committed
[Foundation] UUID: Modernize hashing
1 parent 76d56b9 commit a601e3f

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

Darwin/Foundation-swiftoverlay-Tests/TestUUID.swift

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,28 @@ class TestUUID : TestUUIDSuper {
8585
}
8686

8787
func test_hash() {
88-
let ref = NSUUID()
89-
let val = UUID(uuidString: ref.uuidString)!
90-
expectEqual(ref.hashValue, val.hashValue, "Hashes of references and values should be identical")
88+
let values: [UUID] = [
89+
// This list takes a UUID and tweaks every byte while
90+
// leaving the version/variant intact.
91+
UUID(uuidString: "a53baa1c-b4f5-48db-9467-9786b76b256c")!,
92+
UUID(uuidString: "a63baa1c-b4f5-48db-9467-9786b76b256c")!,
93+
UUID(uuidString: "a53caa1c-b4f5-48db-9467-9786b76b256c")!,
94+
UUID(uuidString: "a53bab1c-b4f5-48db-9467-9786b76b256c")!,
95+
UUID(uuidString: "a53baa1d-b4f5-48db-9467-9786b76b256c")!,
96+
UUID(uuidString: "a53baa1c-b5f5-48db-9467-9786b76b256c")!,
97+
UUID(uuidString: "a53baa1c-b4f6-48db-9467-9786b76b256c")!,
98+
UUID(uuidString: "a53baa1c-b4f5-49db-9467-9786b76b256c")!,
99+
UUID(uuidString: "a53baa1c-b4f5-48dc-9467-9786b76b256c")!,
100+
UUID(uuidString: "a53baa1c-b4f5-48db-9567-9786b76b256c")!,
101+
UUID(uuidString: "a53baa1c-b4f5-48db-9468-9786b76b256c")!,
102+
UUID(uuidString: "a53baa1c-b4f5-48db-9467-9886b76b256c")!,
103+
UUID(uuidString: "a53baa1c-b4f5-48db-9467-9787b76b256c")!,
104+
UUID(uuidString: "a53baa1c-b4f5-48db-9467-9786b86b256c")!,
105+
UUID(uuidString: "a53baa1c-b4f5-48db-9467-9786b76c256c")!,
106+
UUID(uuidString: "a53baa1c-b4f5-48db-9467-9786b76b266c")!,
107+
UUID(uuidString: "a53baa1c-b4f5-48db-9467-9786b76b256d")!,
108+
]
109+
checkHashable(values, equalityOracle: { $0 == $1 })
91110
}
92111

93112
func test_AnyHashableContainingUUID() {

Darwin/Foundation-swiftoverlay/UUID.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,12 @@ public struct UUID : ReferenceConvertible, Hashable, Equatable, CustomStringConv
7474
}
7575
}
7676

77-
public var hashValue: Int {
78-
return withUnsafePointer(to: uuid) {
79-
$0.withMemoryRebound(to: UInt8.self, capacity: 16) {
80-
return Int(bitPattern: CFHashBytes(UnsafeMutablePointer(mutating: $0), CFIndex(MemoryLayout<uuid_t>.size)))
81-
}
77+
public func hash(into hasher: inout Hasher) {
78+
withUnsafeBytes(of: uuid) { buffer in
79+
hasher.combine(bytes: buffer)
8280
}
8381
}
84-
82+
8583
public var description: String {
8684
return uuidString
8785
}

0 commit comments

Comments
 (0)