Skip to content

Commit

Permalink
Remove "copying" label from toSwift.
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanlabelle committed Dec 12, 2023
1 parent 1632fe8 commit 829ad66
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 += ")"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))")
}
Expand Down
9 changes: 6 additions & 3 deletions Support/Sources/COM/ABIProjection/ABIProjection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<Result>(_ value: SwiftValue, _ closure: (ABIValue) throws -> Result) throws -> Result {
var abiValue = try toABI(value)
defer { release(&abiValue) }
Expand All @@ -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) {}
}

Expand Down
7 changes: 1 addition & 6 deletions Support/Sources/COM/BStrProjection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion Support/Sources/COM/COMProjection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 1 addition & 7 deletions Support/Sources/WindowsRuntime/HStringProjection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
11 changes: 3 additions & 8 deletions Support/Sources/WindowsRuntime/WinRTArrayProjection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,19 @@ public enum WinRTArrayProjection<ElementProjection: ABIProjection>: 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..<Int(value.count) {
buffer.initializeElement(at: i, to: ElementProjection.toSwift(copying: value[i]))
buffer.initializeElement(at: i, to: ElementProjection.toSwift(value[i]))
initializedCount = i + 1
}
}
}

public static func toSwift(consuming value: inout ABIValue) -> SwiftValue {
defer { value.deallocate() }
return toSwift(copying: value)
}

public static func toSwift(pointer: UnsafeMutablePointer<ElementProjection.ABIValue>?, 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 []
Expand Down

0 comments on commit 829ad66

Please sign in to comment.