From 02ee9708e8f8b31c1448ff9de4486a1a44f3f12d Mon Sep 17 00:00:00 2001 From: Tristan Labelle Date: Sat, 23 Mar 2024 09:33:21 -0400 Subject: [PATCH] Removed cast extensions on IUnknownPointer/IInspectablePointer --- .../SwiftWinRT/Writing/ClassDefinition.swift | 8 ++++---- Support/Sources/COM/COMExport.swift | 2 +- .../Sources/COM/COMExportedInterface.swift | 8 ++++---- Support/Sources/COM/COMReference.swift | 2 -- Support/Sources/COM/IErrorInfo.swift | 3 +-- Support/Sources/COM/IUnknown.swift | 7 +++++-- Support/Sources/COM/IUnknownPointer.swift | 19 ------------------- .../WindowsRuntime/IActivationFactory.swift | 3 +-- .../WindowsRuntime/IBufferByteAccess.swift | 2 +- .../Sources/WindowsRuntime/IInspectable.swift | 5 ++++- .../WindowsRuntime/IInspectablePointer.swift | 12 ------------ .../WindowsRuntime/IRestrictedErrorInfo.swift | 3 +-- .../Sources/WindowsRuntime/IStringable.swift | 3 +-- .../WindowsRuntime/IWeakReference.swift | 3 +-- .../WindowsRuntime/IWeakReferenceSource.swift | 3 +-- .../WindowsRuntime/WinRTComposableClass.swift | 6 +++--- 16 files changed, 28 insertions(+), 61 deletions(-) delete mode 100644 Support/Sources/COM/IUnknownPointer.swift delete mode 100644 Support/Sources/WindowsRuntime/IInspectablePointer.swift diff --git a/Generator/Sources/SwiftWinRT/Writing/ClassDefinition.swift b/Generator/Sources/SwiftWinRT/Writing/ClassDefinition.swift index bebed5c0..a4215532 100644 --- a/Generator/Sources/SwiftWinRT/Writing/ClassDefinition.swift +++ b/Generator/Sources/SwiftWinRT/Writing/ClassDefinition.swift @@ -318,13 +318,13 @@ fileprivate func writeActivatableInitializers( fileprivate func writeSupportComposableInitializers( defaultInterface: BoundInterface, projection: SwiftProjection, to writer: SwiftTypeDefinitionWriter) throws { // public init(_transferringRef pointer: UnsafeMutablePointer) { - // super.init(pointer.cast()) + // super.init(_transferringRef: .init(OpaquePointer(pointer)) // } // Should use a COMReference<> but this runs into compiler bugs. - let param = SwiftParam(label: "_transferringRef", name: "pointer", - type: .unsafeMutablePointer(to: try projection.toABIType(defaultInterface.asBoundType))) + let pointerType: SwiftType = .unsafeMutablePointer(to: try projection.toABIType(defaultInterface.asBoundType)) + let param = SwiftParam(label: "_transferringRef", name: "pointer", type: pointerType) writer.writeInit(visibility: .public, params: [param]) { writer in - writer.writeStatement("super.init(_transferringRef: IUnknownPointer.cast(pointer).cast())") + writer.writeStatement("super.init(_transferringRef: .init(OpaquePointer(pointer)))") } // public init(_compose: Bool, _factory: ComposableFactory) throws { diff --git a/Support/Sources/COM/COMExport.swift b/Support/Sources/COM/COMExport.swift index 380111db..bc64f137 100644 --- a/Support/Sources/COM/COMExport.swift +++ b/Support/Sources/COM/COMExport.swift @@ -67,7 +67,7 @@ open class COMExport: COMExportBase { open var implementation: Projection.SwiftObject { self as! Projection.SwiftObject } public var comPointer: Projection.COMPointer { - comInterface.unknownPointer.cast(to: Projection.COMInterface.self) + Projection.COMPointer(OpaquePointer(comInterface.unknownPointer)) } public init() { diff --git a/Support/Sources/COM/COMExportedInterface.swift b/Support/Sources/COM/COMExportedInterface.swift index 349ca064..379bd934 100644 --- a/Support/Sources/COM/COMExportedInterface.swift +++ b/Support/Sources/COM/COMExportedInterface.swift @@ -38,7 +38,7 @@ public struct COMExportedInterface { public var unknownPointer: IUnknownPointer { mutating get { withUnsafeMutablePointer(to: &comObject) { - IUnknownPointer.cast($0) + IUnknownPointer(OpaquePointer($0)) } } } @@ -73,7 +73,7 @@ extension COMExportedInterface { return 1 } - let unmanaged = toUnmanagedUnsafe(IUnknownPointer.cast(this)) + let unmanaged = toUnmanagedUnsafe(IUnknownPointer(OpaquePointer(this))) _ = unmanaged.retain() // Best effort refcount return UInt32(_getRetainCount(unmanaged.takeUnretainedValue())) @@ -85,7 +85,7 @@ extension COMExportedInterface { return 0 } - let unmanaged = toUnmanagedUnsafe(IUnknownPointer.cast(this)) + let unmanaged = toUnmanagedUnsafe(IUnknownPointer(OpaquePointer(this))) let oldRetainCount = _getRetainCount(unmanaged.takeUnretainedValue()) unmanaged.release() // Best effort refcount @@ -108,7 +108,7 @@ extension COMExportedInterface { return HResult.catchValue { let id = GUIDProjection.toSwift(iid.pointee) - let this = IUnknownPointer.cast(this) + let this = IUnknownPointer(OpaquePointer(this)) let reference = id == markerInterfaceId ? IUnknownReference(addingRef: this) : try (unwrapUnsafe(this) as! IUnknown)._queryInterface(id) diff --git a/Support/Sources/COM/COMReference.swift b/Support/Sources/COM/COMReference.swift index a4259441..c5a4cb71 100644 --- a/Support/Sources/COM/COMReference.swift +++ b/Support/Sources/COM/COMReference.swift @@ -35,5 +35,3 @@ public struct COMReference: ~Copyable /* where Interface: COMIUnknown interop.release() } } - -public typealias IUnknownReference = COMReference \ No newline at end of file diff --git a/Support/Sources/COM/IErrorInfo.swift b/Support/Sources/COM/IErrorInfo.swift index b286c789..e623f70c 100644 --- a/Support/Sources/COM/IErrorInfo.swift +++ b/Support/Sources/COM/IErrorInfo.swift @@ -1,6 +1,7 @@ import WindowsRuntime_ABI import struct Foundation.UUID +public typealias IErrorInfo = any IErrorInfoProtocol public protocol IErrorInfoProtocol: IUnknownProtocol { var guid: Foundation.UUID { get throws } var source: String? { get throws } @@ -9,8 +10,6 @@ public protocol IErrorInfoProtocol: IUnknownProtocol { var helpContext: UInt32 { get throws } } -public typealias IErrorInfo = any IErrorInfoProtocol - public enum IErrorInfoProjection: COMTwoWayProjection { public typealias SwiftObject = IErrorInfo public typealias COMInterface = WindowsRuntime_ABI.SWRT_IErrorInfo diff --git a/Support/Sources/COM/IUnknown.swift b/Support/Sources/COM/IUnknown.swift index 81a267c3..eb83c3d6 100644 --- a/Support/Sources/COM/IUnknown.swift +++ b/Support/Sources/COM/IUnknown.swift @@ -1,9 +1,9 @@ import WindowsRuntime_ABI +public typealias IUnknown = any IUnknownProtocol public protocol IUnknownProtocol: AnyObject { func _queryInterface(_ id: COMInterfaceID) throws -> IUnknownReference } -public typealias IUnknown = any IUnknownProtocol extension IUnknownProtocol { public func _queryInterface(_: Projection.Type) throws -> COMReference { @@ -42,4 +42,7 @@ public enum IUnknownProjection: COMTwoWayProjection { extension WindowsRuntime_ABI.SWRT_IUnknown: /* @retroactive */ COMIUnknownStruct { public static let iid = COMInterfaceID(0x00000000, 0x0000, 0x0000, 0xC000, 0x000000000046) -} \ No newline at end of file +} + +public typealias IUnknownPointer = IUnknownProjection.COMPointer +public typealias IUnknownReference = COMReference \ No newline at end of file diff --git a/Support/Sources/COM/IUnknownPointer.swift b/Support/Sources/COM/IUnknownPointer.swift deleted file mode 100644 index b9d3c3dd..00000000 --- a/Support/Sources/COM/IUnknownPointer.swift +++ /dev/null @@ -1,19 +0,0 @@ -import WindowsRuntime_ABI - -public typealias IUnknownPointer = UnsafeMutablePointer - -extension IUnknownPointer { - public func cast(to type: COMInterface.Type = COMInterface.self) -> UnsafeMutablePointer { - self.withMemoryRebound(to: COMInterface.self, capacity: 1) { $0 } - } - - // UnsafeMutableRawPointer helpers - public static func cast(_ pointer: UnsafeMutableRawPointer) -> IUnknownPointer { - pointer.bindMemory(to: IUnknownPointer.Pointee.self, capacity: 1) - } - - // UnsafeMutablePointer helpers - public static func cast(_ pointer: UnsafeMutablePointer) -> IUnknownPointer { - pointer.withMemoryRebound(to: IUnknownPointer.Pointee.self, capacity: 1) { $0 } - } -} \ No newline at end of file diff --git a/Support/Sources/WindowsRuntime/IActivationFactory.swift b/Support/Sources/WindowsRuntime/IActivationFactory.swift index b50148cf..79b96941 100644 --- a/Support/Sources/WindowsRuntime/IActivationFactory.swift +++ b/Support/Sources/WindowsRuntime/IActivationFactory.swift @@ -18,12 +18,11 @@ public func getActivationFactory(activatableId: String) throws -> COM.COMReferen try getActivationFactory(activatableId: activatableId, id: WindowsRuntime_ABI.SWRT_IActivationFactory.iid) } +public typealias IActivationFactory = any IActivationFactoryProtocol public protocol IActivationFactoryProtocol: IInspectableProtocol { func activateInstance() throws -> IInspectable } -public typealias IActivationFactory = any IActivationFactoryProtocol - public enum IActivationFactoryProjection: WinRTProjection { public typealias SwiftObject = IActivationFactory public typealias COMInterface = WindowsRuntime_ABI.SWRT_IActivationFactory diff --git a/Support/Sources/WindowsRuntime/IBufferByteAccess.swift b/Support/Sources/WindowsRuntime/IBufferByteAccess.swift index da0b1eba..5adb0cab 100644 --- a/Support/Sources/WindowsRuntime/IBufferByteAccess.swift +++ b/Support/Sources/WindowsRuntime/IBufferByteAccess.swift @@ -1,10 +1,10 @@ import WindowsRuntime_ABI import COM +public typealias IBufferByteAccess = any IBufferByteAccessProtocol public protocol IBufferByteAccessProtocol: IUnknownProtocol { var buffer: UnsafeMutablePointer { get throws } } -public typealias IBufferByteAccess = any IBufferByteAccessProtocol public enum IBufferByteAccessProjection: COMTwoWayProjection { public typealias SwiftObject = IBufferByteAccess diff --git a/Support/Sources/WindowsRuntime/IInspectable.swift b/Support/Sources/WindowsRuntime/IInspectable.swift index b9157ed8..2f7b3fac 100644 --- a/Support/Sources/WindowsRuntime/IInspectable.swift +++ b/Support/Sources/WindowsRuntime/IInspectable.swift @@ -2,12 +2,12 @@ import COM import WindowsRuntime_ABI import struct Foundation.UUID +public typealias IInspectable = any IInspectableProtocol public protocol IInspectableProtocol: IUnknownProtocol { func getIids() throws -> [COMInterfaceID] func getRuntimeClassName() throws -> String func getTrustLevel() throws -> TrustLevel } -public typealias IInspectable = any IInspectableProtocol public enum IInspectableProjection: WinRTTwoWayProjection { public typealias SwiftObject = IInspectable @@ -49,6 +49,9 @@ extension WindowsRuntime_ABI.SWRT_IInspectable: /* @retroactive */ COMIInspectab public static let iid = COMInterfaceID(0xAF86E2E0, 0xB12D, 0x4C6A, 0x9C5A, 0xD7AA65101E90) } +public typealias IInspectablePointer = IInspectableProjection.COMPointer +public typealias IInspectableReference = COMReference + extension COMInterop where Interface: /* @retroactive */ COMIInspectableStruct { private var inspectable: UnsafeMutablePointer{ this.withMemoryRebound(to: WindowsRuntime_ABI.SWRT_IInspectable.self, capacity: 1) { $0 } diff --git a/Support/Sources/WindowsRuntime/IInspectablePointer.swift b/Support/Sources/WindowsRuntime/IInspectablePointer.swift deleted file mode 100644 index 695feb8b..00000000 --- a/Support/Sources/WindowsRuntime/IInspectablePointer.swift +++ /dev/null @@ -1,12 +0,0 @@ -import COM -import WindowsRuntime_ABI - -public typealias IInspectablePointer = UnsafeMutablePointer - -extension IInspectablePointer { - public static func cast(_ pointer: UnsafeMutablePointer) -> Self { - pointer.withMemoryRebound(to: Pointee.self, capacity: 1) { $0 } - } -} - -public typealias IInspectableReference = COMReference \ No newline at end of file diff --git a/Support/Sources/WindowsRuntime/IRestrictedErrorInfo.swift b/Support/Sources/WindowsRuntime/IRestrictedErrorInfo.swift index de76c70a..e857460c 100644 --- a/Support/Sources/WindowsRuntime/IRestrictedErrorInfo.swift +++ b/Support/Sources/WindowsRuntime/IRestrictedErrorInfo.swift @@ -1,6 +1,7 @@ import WindowsRuntime_ABI import struct Foundation.UUID +public typealias IRestrictedErrorInfo = any IRestrictedErrorInfoProtocol public protocol IRestrictedErrorInfoProtocol: IUnknownProtocol { func getErrorDetails( description: inout String?, @@ -10,8 +11,6 @@ public protocol IRestrictedErrorInfoProtocol: IUnknownProtocol { var reference: String? { get throws } } -public typealias IRestrictedErrorInfo = any IRestrictedErrorInfoProtocol - public enum IRestrictedErrorInfoProjection: COMTwoWayProjection { public typealias SwiftObject = IRestrictedErrorInfo public typealias COMInterface = WindowsRuntime_ABI.SWRT_IRestrictedErrorInfo diff --git a/Support/Sources/WindowsRuntime/IStringable.swift b/Support/Sources/WindowsRuntime/IStringable.swift index 2f5b73cc..53989eaa 100644 --- a/Support/Sources/WindowsRuntime/IStringable.swift +++ b/Support/Sources/WindowsRuntime/IStringable.swift @@ -1,11 +1,10 @@ import WindowsRuntime_ABI +public typealias IStringable = any IStringableProtocol public protocol IStringableProtocol: IUnknownProtocol { func toString() throws -> String } -public typealias IStringable = any IStringableProtocol - public enum IStringableProjection: COMTwoWayProjection { public typealias SwiftObject = IStringable public typealias COMInterface = WindowsRuntime_ABI.SWRT_IStringable diff --git a/Support/Sources/WindowsRuntime/IWeakReference.swift b/Support/Sources/WindowsRuntime/IWeakReference.swift index 4b365fc4..6d1c670c 100644 --- a/Support/Sources/WindowsRuntime/IWeakReference.swift +++ b/Support/Sources/WindowsRuntime/IWeakReference.swift @@ -1,11 +1,10 @@ import WindowsRuntime_ABI +public typealias IWeakReference = any IWeakReferenceProtocol public protocol IWeakReferenceProtocol: IUnknownProtocol { func resolve() throws -> IInspectable? } -public typealias IWeakReference = any IWeakReferenceProtocol - public enum IWeakReferenceProjection: COMTwoWayProjection { public typealias SwiftObject = IWeakReference public typealias COMInterface = WindowsRuntime_ABI.SWRT_IWeakReference diff --git a/Support/Sources/WindowsRuntime/IWeakReferenceSource.swift b/Support/Sources/WindowsRuntime/IWeakReferenceSource.swift index 2b239164..b283cd99 100644 --- a/Support/Sources/WindowsRuntime/IWeakReferenceSource.swift +++ b/Support/Sources/WindowsRuntime/IWeakReferenceSource.swift @@ -1,11 +1,10 @@ import WindowsRuntime_ABI +public typealias IWeakReferenceSource = any IWeakReferenceSourceProtocol public protocol IWeakReferenceSourceProtocol: IUnknownProtocol { func getWeakReference() throws -> IWeakReference } -public typealias IWeakReferenceSource = any IWeakReferenceSourceProtocol - public enum IWeakReferenceSourceProjection: COMTwoWayProjection { public typealias SwiftObject = IWeakReferenceSource public typealias COMInterface = WindowsRuntime_ABI.SWRT_IWeakReferenceSource diff --git a/Support/Sources/WindowsRuntime/WinRTComposableClass.swift b/Support/Sources/WindowsRuntime/WinRTComposableClass.swift index 9b04d837..308555ce 100644 --- a/Support/Sources/WindowsRuntime/WinRTComposableClass.swift +++ b/Support/Sources/WindowsRuntime/WinRTComposableClass.swift @@ -37,13 +37,13 @@ open class WinRTComposableClass: IInspectableProtocol { // - Factory needs an initialized outer pointer pointing to self // - self.inner needs to be initialized before being able to reference self self.outer = .uninitialized - self.innerWithRef = IInspectablePointer.cast(outer.unknownPointer) // We need to assign inner to something, it doesn't matter what. + self.innerWithRef = IInspectablePointer(OpaquePointer(outer.unknownPointer)) // We need to assign inner to something, it doesn't matter what. self.outer = .init(swiftObject: self, virtualTable: IInspectableProjection.virtualTablePointer) // Like C++/WinRT, discard the returned composed object and only use the inner object // The composed object is useful only when not providing an outer object. var inner: IInspectablePointer? = nil - let composed = try _factory(IInspectablePointer.cast(outer.unknownPointer), &inner) + let composed = try _factory(IInspectablePointer(OpaquePointer(outer.unknownPointer)), &inner) if let composed { COMInterop(composed).release() } guard let inner else { throw HResult.Error.fail } self.innerWithRef = inner @@ -56,7 +56,7 @@ open class WinRTComposableClass: IInspectableProtocol { var inner: IInspectablePointer? = nil defer { IInspectableProjection.release(&inner) } guard let composed = try _factory(nil, &inner) else { throw HResult.Error.fail } - self.innerWithRef = IInspectablePointer.cast(composed) + self.innerWithRef = IInspectablePointer(OpaquePointer(composed)) } }