From 829ad66167d2ffdb97e82d195ee63b5302d31029 Mon Sep 17 00:00:00 2001 From: Tristan Labelle Date: Tue, 12 Dec 2023 07:41:39 -0500 Subject: [PATCH] Remove "copying" label from toSwift. --- .../SwiftAssemblyModuleFileWriter+structs.swift | 1 - .../SwiftAssemblyModuleFileWriter+virtualTable.swift | 4 +--- Support/Sources/COM/ABIProjection/ABIProjection.swift | 9 ++++++--- Support/Sources/COM/BStrProjection.swift | 7 +------ Support/Sources/COM/COMProjection.swift | 2 +- .../Sources/WindowsRuntime/HStringProjection.swift | 8 +------- .../Sources/WindowsRuntime/WinRTArrayProjection.swift | 11 +++-------- 7 files changed, 13 insertions(+), 29 deletions(-) diff --git a/Generator/Sources/ProjectionGenerator/SwiftAssemblyModuleFileWriter+structs.swift b/Generator/Sources/ProjectionGenerator/SwiftAssemblyModuleFileWriter+structs.swift index 055d1b66..421e745b 100644 --- a/Generator/Sources/ProjectionGenerator/SwiftAssemblyModuleFileWriter+structs.swift +++ b/Generator/Sources/ProjectionGenerator/SwiftAssemblyModuleFileWriter+structs.swift @@ -114,7 +114,6 @@ extension SwiftAssemblyModuleFileWriter { else { typeProjection.projectionType.write(to: &expression) expression += ".toSwift(" - if typeProjection.kind != .inert { expression += "copying: " } expression += "value." SwiftIdentifier.write(field.name, to: &expression) expression += ")" diff --git a/Generator/Sources/ProjectionGenerator/SwiftAssemblyModuleFileWriter+virtualTable.swift b/Generator/Sources/ProjectionGenerator/SwiftAssemblyModuleFileWriter+virtualTable.swift index 7eafaf95..a5d3bc91 100644 --- a/Generator/Sources/ProjectionGenerator/SwiftAssemblyModuleFileWriter+virtualTable.swift +++ b/Generator/Sources/ProjectionGenerator/SwiftAssemblyModuleFileWriter+virtualTable.swift @@ -126,10 +126,8 @@ extension SwiftAssemblyModuleFileWriter { output.write("\(declarator) \(paramProjection.swiftProjectionName) = \(paramProjection.projectionType).toSwift") switch paramProjection.typeProjection.kind { case .identity: fatalError("Case should have been ignored earlier.") - case .inert: + case .inert, .allocating: output.write("(\(paramProjection.name))") - case .allocating: - output.write("(copying: \(paramProjection.name))") case .array: output.write("(pointer: \(paramProjection.name), count: \(paramProjection.arrayLengthName))") } diff --git a/Support/Sources/COM/ABIProjection/ABIProjection.swift b/Support/Sources/COM/ABIProjection/ABIProjection.swift index 116c2a3a..d5f69c22 100644 --- a/Support/Sources/COM/ABIProjection/ABIProjection.swift +++ b/Support/Sources/COM/ABIProjection/ABIProjection.swift @@ -12,7 +12,7 @@ public protocol ABIProjection { /// Converts a value from its ABI to its Swift representation /// without releasing the original value. - static func toSwift(copying value: ABIValue) -> SwiftValue + static func toSwift(_ value: ABIValue) -> SwiftValue /// Converts a value from its ABI to its Swift representation, /// releasing the original value. @@ -27,6 +27,11 @@ public protocol ABIProjection { } extension ABIProjection { + public static func toSwift(consuming value: inout ABIValue) -> SwiftValue { + defer { release(&value) } + return toSwift(value) + } + public static func withABI(_ value: SwiftValue, _ closure: (ABIValue) throws -> Result) throws -> Result { var abiValue = try toABI(value) defer { release(&abiValue) } @@ -39,12 +44,10 @@ extension ABIProjection { /// For conformance convenience. public protocol ABIInertProjection: ABIProjection { static func toABI(_ value: SwiftValue) -> ABIValue // No throw version - static func toSwift(_ value: ABIValue) -> SwiftValue } extension ABIInertProjection { public static func toSwift(consuming value: inout ABIValue) -> SwiftValue { toSwift(value) } - public static func toSwift(copying value: ABIValue) -> SwiftValue { toSwift(value) } public static func release(_ value: inout ABIValue) {} } diff --git a/Support/Sources/COM/BStrProjection.swift b/Support/Sources/COM/BStrProjection.swift index 668aa9d2..97f8912c 100644 --- a/Support/Sources/COM/BStrProjection.swift +++ b/Support/Sources/COM/BStrProjection.swift @@ -6,12 +6,7 @@ public enum BStrProjection: ABIProjection { public static var abiDefaultValue: ABIValue { nil } - public static func toSwift(consuming value: inout CWinRTCore.SWRT_BStr?) -> String? { - defer { release(&value) } - return toSwift(copying: value) - } - - public static func toSwift(copying value: CWinRTCore.SWRT_BStr?) -> String? { + public static func toSwift(_ value: CWinRTCore.SWRT_BStr?) -> String? { guard let value else { return nil } let length = CWinRTCore.SWRT_SysStringLen(value) return String(utf16CodeUnits: value, count: Int(length)) diff --git a/Support/Sources/COM/COMProjection.swift b/Support/Sources/COM/COMProjection.swift index 1f79c72a..e2225d73 100644 --- a/Support/Sources/COM/COMProjection.swift +++ b/Support/Sources/COM/COMProjection.swift @@ -26,7 +26,7 @@ extension COMProjection { // Default ABIProjection implementation public static var abiDefaultValue: ABIValue { nil } - public static func toSwift(copying value: ABIValue) -> SwiftValue { + public static func toSwift(_ value: ABIValue) -> SwiftValue { guard let comPointer = value else { return nil } IUnknownPointer.addRef(comPointer) return toSwift(transferringRef: comPointer) diff --git a/Support/Sources/WindowsRuntime/HStringProjection.swift b/Support/Sources/WindowsRuntime/HStringProjection.swift index b20545db..b7c2adc7 100644 --- a/Support/Sources/WindowsRuntime/HStringProjection.swift +++ b/Support/Sources/WindowsRuntime/HStringProjection.swift @@ -7,13 +7,7 @@ public enum HStringProjection: ABIProjection { public static var abiDefaultValue: ABIValue { nil } - public static func toSwift(consuming value: inout CWinRTCore.SWRT_HString?) -> SwiftValue { - let result = CWinRTCore.SWRT_HString.toStringAndDelete(value) - value = nil - return result - } - - public static func toSwift(copying value: CWinRTCore.SWRT_HString?) -> SwiftValue { + public static func toSwift(_ value: CWinRTCore.SWRT_HString?) -> SwiftValue { value.toString() } diff --git a/Support/Sources/WindowsRuntime/WinRTArrayProjection.swift b/Support/Sources/WindowsRuntime/WinRTArrayProjection.swift index e52ab500..bd6afbaf 100644 --- a/Support/Sources/WindowsRuntime/WinRTArrayProjection.swift +++ b/Support/Sources/WindowsRuntime/WinRTArrayProjection.swift @@ -7,24 +7,19 @@ public enum WinRTArrayProjection: ABIProjectio public static var abiDefaultValue: ABIValue { .null } - public static func toSwift(copying value: ABIValue) -> SwiftValue { + public static func toSwift(_ value: ABIValue) -> SwiftValue { guard value.count > 0 else { return [] } return .init(unsafeUninitializedCapacity: Int(value.count)) { buffer, initializedCount in for i in 0.. SwiftValue { - defer { value.deallocate() } - return toSwift(copying: value) - } - public static func toSwift(pointer: UnsafeMutablePointer?, count: UInt32) -> SwiftValue { if let pointer { - return toSwift(copying: COMArray(pointer: pointer, count: count)) + return toSwift(COMArray(pointer: pointer, count: count)) } else { assert(count == 0) return []