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

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions stdlib/public/Windows/WinSDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -319,3 +319,31 @@ func _convertWindowsBoolToBool(_ b: WindowsBool) -> Bool {
return b.boolValue
}

// GUID

extension GUID {
@usableFromInline @_transparent
internal var uint128Value: UInt128 {
unsafe withUnsafeBytes(of: self) { buffer in
// GUID is 32-bit-aligned only, so use loadUnaligned().
unsafe buffer.baseAddress!.loadUnaligned(as: UInt128.self)
}
}
}

// These conformances are marked @retroactive because the GUID type nominally
// comes from the _GUIDDef clang module rather than the WinSDK clang module.

extension GUID: @retroactive Equatable {
@_transparent
public static func ==(lhs: Self, rhs: Self) -> Bool {
lhs.uint128Value == rhs.uint128Value
}
}

extension GUID: @retroactive Hashable {
@_transparent
public func hash(into hasher: inout Hasher) {
hasher.combine(uint128Value)
}
}
15 changes: 14 additions & 1 deletion test/stdlib/WinSDK_GUID.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// RUN: %target-build-swift %s
// RUN: %target-build-swift %s -o %t.exe
// RUN: %target-codesign %t.exe
// RUN: %target-run %t.exe
// REQUIRES: executable_test
// REQUIRES: OS=windows-msvc

// Make sure that importing WinSDK brings in the GUID type, which is declared in
Expand All @@ -7,3 +10,13 @@
import WinSDK

public func usesGUID(_ x: GUID) {}

// Make sure equating and hashing GUIDs works.

let guid: GUID = GUID_NULL
assert(guid == guid)
assert(guid.hashValue == guid.hashValue)

let guid2: GUID = IID_IUnknown
assert(guid != guid2)
assert(guid.hashValue != guid2.hashValue) // well, probably