Skip to content

Commit 9fd42ec

Browse files
committed
Add Equatable and Hashable conformance to GUID.
On Windows, `GUID` is a currency type and (along with its various typedefs) is used pervasively Windows offers `IsEqualGUID()` and `UuidHash()` for comparing and hashing them, respectively, but `IsEqualGUID()` is a macro in C mode and `UuidHash()` only provides a 16-bit hash. We should provide conformance to these protocols in the WinSDK overlay to provide equivalent functionality in Swift.
1 parent 0cbcd56 commit 9fd42ec

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

stdlib/public/Windows/WinSDK.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,3 +319,24 @@ func _convertWindowsBoolToBool(_ b: WindowsBool) -> Bool {
319319
return b.boolValue
320320
}
321321

322+
// GUID
323+
324+
extension GUID: Equatable, Hashable {
325+
@usableFromInline @_transparent
326+
private var uint128Value: UInt128 {
327+
unsafe withUnsafeBytes(of: self) { buffer in
328+
// GUID is 32-bit-aligned only, so use loadUnaligned().
329+
unsafe buffer.baseAddress!.loadUnaligned(as: UInt128.self)
330+
}
331+
}
332+
333+
@_transparent
334+
public static func ==(lhs: Self, rhs: Self) -> Bool {
335+
lhs.uint128Value == rhs.uint128Value
336+
}
337+
338+
@_transparent
339+
public func hash(into: inout Hasher) {
340+
hasher.combine(uint128Value)
341+
}
342+
}

0 commit comments

Comments
 (0)