project: use Swift 5.6 existentials

This commit is contained in:
osy 2022-07-20 17:48:27 -07:00
parent ba453e1e88
commit d90a7b5601
19 changed files with 47 additions and 41 deletions

View File

@ -51,7 +51,7 @@ struct QEMUArgumentBuilder {
[.init(string)]
}
static func buildExpression(_ constant: QEMUConstant) -> [QEMUArgumentFragment] {
static func buildExpression(_ constant: any QEMUConstant) -> [QEMUArgumentFragment] {
[.init(constant.rawValue)]
}

View File

@ -20,7 +20,7 @@ import Metal
// MARK: QEMUConstant protocol
/// A QEMU constant is a enum that can be generated externally
protocol QEMUConstant: Codable {
protocol QEMUConstant: Codable, RawRepresentable, CaseIterable where RawValue == String, AllCases == [Self] {
static var allRawValues: [String] { get }
static var allPrettyValues: [String] { get }
var prettyValue: String { get }
@ -83,6 +83,8 @@ struct AnyQEMUConstant: QEMUConstant, RawRepresentable {
static var allPrettyValues: [String] { [] }
static var allCases: [AnyQEMUConstant] { [] }
var prettyValue: String { rawValue }
let rawValue: String
@ -267,6 +269,10 @@ struct QEMUTerminalFont: QEMUConstant {
}()
#endif
static var allCases: [QEMUTerminalFont] {
Self.allRawValues.map { Self(rawValue: $0) }
}
var prettyValue: String {
guard let index = Self.allRawValues.firstIndex(of: rawValue) else {
return rawValue

View File

@ -9653,7 +9653,7 @@ enum QEMUSerialDevice_xtensaeb: String, CaseIterable, QEMUSerialDevice {
}
extension QEMUArchitecture {
var cpuType: QEMUCPU.Type {
var cpuType: any QEMUCPU.Type {
switch self {
case .alpha: return QEMUCPU_alpha.self
case .arm: return QEMUCPU_arm.self
@ -9688,7 +9688,7 @@ extension QEMUArchitecture {
}
}
var cpuFlagType: QEMUCPUFlag.Type {
var cpuFlagType: any QEMUCPUFlag.Type {
switch self {
case .alpha: return QEMUCPUFlag_alpha.self
case .arm: return QEMUCPUFlag_arm.self
@ -9723,7 +9723,7 @@ extension QEMUArchitecture {
}
}
var targetType: QEMUTarget.Type {
var targetType: any QEMUTarget.Type {
switch self {
case .alpha: return QEMUTarget_alpha.self
case .arm: return QEMUTarget_arm.self
@ -9758,7 +9758,7 @@ extension QEMUArchitecture {
}
}
var displayDeviceType: QEMUDisplayDevice.Type {
var displayDeviceType: any QEMUDisplayDevice.Type {
switch self {
case .alpha: return QEMUDisplayDevice_alpha.self
case .arm: return QEMUDisplayDevice_arm.self
@ -9793,7 +9793,7 @@ extension QEMUArchitecture {
}
}
var networkDeviceType: QEMUNetworkDevice.Type {
var networkDeviceType: any QEMUNetworkDevice.Type {
switch self {
case .alpha: return QEMUNetworkDevice_alpha.self
case .arm: return QEMUNetworkDevice_arm.self
@ -9828,7 +9828,7 @@ extension QEMUArchitecture {
}
}
var soundDeviceType: QEMUSoundDevice.Type {
var soundDeviceType: any QEMUSoundDevice.Type {
switch self {
case .alpha: return QEMUSoundDevice_alpha.self
case .arm: return QEMUSoundDevice_arm.self
@ -9863,7 +9863,7 @@ extension QEMUArchitecture {
}
}
var serialDeviceType: QEMUSerialDevice.Type {
var serialDeviceType: any QEMUSerialDevice.Type {
switch self {
case .alpha: return QEMUSerialDevice_alpha.self
case .arm: return QEMUSerialDevice_arm.self

View File

@ -90,7 +90,7 @@ private final class UTMConfigurationStub: Decodable {
extension UTMConfiguration {
static var dataDirectoryName: String { "Data" }
static func load(from packageURL: URL) throws -> AnyObject { // FIXME: `any UTMConfiguration`
static func load(from packageURL: URL) throws -> any UTMConfiguration {
let dataURL = packageURL.appendingPathComponent(Self.dataDirectoryName)
let configURL = packageURL.appendingPathComponent(kUTMBundleConfigFilename)
let configData = try Data(contentsOf: configURL)

View File

@ -155,7 +155,7 @@ extension UTMQemuConfiguration {
sound = []
}
func reset(forArchitecture architecture: QEMUArchitecture, target: QEMUTarget) {
func reset(forArchitecture architecture: QEMUArchitecture, target: any QEMUTarget) {
reset(all: false)
qemu = .init(forArchitecture: architecture, target: target)
input = .init(forArchitecture: architecture, target: target)

View File

@ -19,7 +19,7 @@ import Foundation
/// Settings for a single display.
struct UTMQemuConfigurationDisplay: Codable, Identifiable {
/// Hardware card to emulate.
var hardware: QEMUDisplayDevice = QEMUDisplayDevice_x86_64.virtio_vga
var hardware: any QEMUDisplayDevice = QEMUDisplayDevice_x86_64.virtio_vga
/// If true, attempt to use SPICE guest agent to change the display resolution automatically.
var isDynamicResolution: Bool = true
@ -68,7 +68,7 @@ struct UTMQemuConfigurationDisplay: Codable, Identifiable {
// MARK: - Default construction
extension UTMQemuConfigurationDisplay {
init?(forArchitecture architecture: QEMUArchitecture, target: QEMUTarget) {
init?(forArchitecture architecture: QEMUArchitecture, target: any QEMUTarget) {
self.init()
let rawTarget = target.rawValue
if rawTarget.hasPrefix("pc") || rawTarget.hasPrefix("q35") {

View File

@ -133,7 +133,7 @@ struct UTMQemuConfigurationDrive: UTMConfigurationDrive {
// MARK: - Default interface
extension UTMQemuConfigurationDrive {
static func defaultInterface(forArchitecture architecture: QEMUArchitecture, target: QEMUTarget, imageType: QEMUDriveImageType) -> QEMUDriveInterface {
static func defaultInterface(forArchitecture architecture: QEMUArchitecture, target: any QEMUTarget, imageType: QEMUDriveImageType) -> QEMUDriveInterface {
let rawTarget = target.rawValue
if rawTarget.hasPrefix("virt-") || rawTarget == "virt" {
if imageType == .cd {
@ -224,7 +224,7 @@ extension UTMQemuConfigurationDrive {
// MARK: - New drive
extension UTMQemuConfigurationDrive {
init(forArchitecture architecture: QEMUArchitecture, target: QEMUTarget, isExternal: Bool = false) {
init(forArchitecture architecture: QEMUArchitecture, target: any QEMUTarget, isExternal: Bool = false) {
self.isExternal = isExternal
self.imageType = isExternal ? .cd : .disk
self.isRawImage = false

View File

@ -54,7 +54,7 @@ struct UTMQemuConfigurationInput: Codable {
// MARK: - Default construction
extension UTMQemuConfigurationInput {
init(forArchitecture architecture: QEMUArchitecture, target: QEMUTarget) {
init(forArchitecture architecture: QEMUArchitecture, target: any QEMUTarget) {
self.init()
let rawTarget = target.rawValue
if rawTarget.hasPrefix("pc") || rawTarget.hasPrefix("q35") {

View File

@ -22,7 +22,7 @@ struct UTMQemuConfigurationNetwork: Codable, Identifiable {
var mode: QEMUNetworkMode = .emulated
/// Hardware model to emulate.
var hardware: QEMUNetworkDevice = QEMUNetworkDevice_x86_64.e1000
var hardware: any QEMUNetworkDevice = QEMUNetworkDevice_x86_64.e1000
/// Unique MAC address.
var macAddress: String = UTMQemuConfigurationNetwork.randomMacAddress()
@ -146,7 +146,7 @@ struct UTMQemuConfigurationNetwork: Codable, Identifiable {
// MARK: - Default construction
extension UTMQemuConfigurationNetwork {
init?(forArchitecture architecture: QEMUArchitecture, target: QEMUTarget) {
init?(forArchitecture architecture: QEMUArchitecture, target: any QEMUTarget) {
self.init()
let rawTarget = target.rawValue
if rawTarget.hasPrefix("pc") {

View File

@ -112,7 +112,7 @@ struct UTMQemuConfigurationQEMU: Codable {
// MARK: - Default construction
extension UTMQemuConfigurationQEMU {
init(forArchitecture architecture: QEMUArchitecture, target: QEMUTarget) {
init(forArchitecture architecture: QEMUArchitecture, target: any QEMUTarget) {
self.init()
let rawTarget = target.rawValue
if rawTarget.hasPrefix("pc") || rawTarget.hasPrefix("q35") {

View File

@ -29,7 +29,7 @@ struct UTMQemuConfigurationSerial: Codable, Identifiable {
var terminal: UTMConfigurationTerminal? = .init()
/// Hardware model to emulate (for manual mode).
var hardware: QEMUSerialDevice?
var hardware: (any QEMUSerialDevice)?
/// TCP server to connect to (for TCP client mode).
var tcpHostAddress: String?
@ -83,7 +83,7 @@ struct UTMQemuConfigurationSerial: Codable, Identifiable {
// MARK: - Default construction
extension UTMQemuConfigurationSerial {
init?(forArchitecture architecture: QEMUArchitecture, target: QEMUTarget) {
init?(forArchitecture architecture: QEMUArchitecture, target: any QEMUTarget) {
self.init()
}
}

View File

@ -57,7 +57,7 @@ struct UTMQemuConfigurationSharing: Codable {
// MARK: - Default construction
extension UTMQemuConfigurationSharing {
init(forArchitecture architecture: QEMUArchitecture, target: QEMUTarget) {
init(forArchitecture architecture: QEMUArchitecture, target: any QEMUTarget) {
self.init()
let rawTarget = target.rawValue
if rawTarget.hasPrefix("pc") || rawTarget.hasPrefix("q35") {

View File

@ -19,7 +19,7 @@ import Foundation
/// Settings for single audio device
struct UTMQemuConfigurationSound: Codable, Identifiable {
/// Hardware model to emulate.
var hardware: QEMUSoundDevice = QEMUSoundDevice_x86_64.AC97
var hardware: any QEMUSoundDevice = QEMUSoundDevice_x86_64.AC97
let id = UUID()
@ -44,7 +44,7 @@ struct UTMQemuConfigurationSound: Codable, Identifiable {
// MARK: - Default construction
extension UTMQemuConfigurationSound {
init?(forArchitecture architecture: QEMUArchitecture, target: QEMUTarget) {
init?(forArchitecture architecture: QEMUArchitecture, target: any QEMUTarget) {
self.init()
let rawTarget = target.rawValue
if rawTarget.hasPrefix("pc") {

View File

@ -22,16 +22,16 @@ struct UTMQemuConfigurationSystem: Codable {
var architecture: QEMUArchitecture = .x86_64
/// The QEMU machine target to emulate.
var target: QEMUTarget = QEMUTarget_x86_64.q35
var target: any QEMUTarget = QEMUTarget_x86_64.q35
/// The QEMU CPU to emulate. Note that `default` will use the default CPU for the architecture.
var cpu: QEMUCPU = QEMUCPU_x86_64.default
var cpu: any QEMUCPU = QEMUCPU_x86_64.default
/// Optional list of CPU flags to add to the target CPU.
var cpuFlagsAdd: [QEMUCPUFlag] = []
var cpuFlagsAdd: [any QEMUCPUFlag] = []
/// Optional list of CPU flags to remove from the defaults of the target CPU. Parsed after `cpuFlagsAdd`.
var cpuFlagsRemove: [QEMUCPUFlag] = []
var cpuFlagsRemove: [any QEMUCPUFlag] = []
/// Number of CPU cores to emulate. Set to 0 to match the number of available cores on the host.
var cpuCount: Int = 0

View File

@ -16,12 +16,12 @@
import SwiftUI
struct VMConfigConstantPicker<T: QEMUConstant>: View {
struct VMConfigConstantPicker: View {
@Binding private var stringSelection: String
private let titleKey: LocalizedStringKey?
private let type: QEMUConstant.Type
private let type: any QEMUConstant.Type
init(_ titleKey: LocalizedStringKey? = nil, selection: Binding<T>) {
init<T: QEMUConstant>(_ titleKey: LocalizedStringKey? = nil, selection: Binding<T>) {
self._stringSelection = Binding(get: {
selection.wrappedValue.rawValue
}, set: { newValue in
@ -31,11 +31,11 @@ struct VMConfigConstantPicker<T: QEMUConstant>: View {
self.type = T.self
}
init<S>(_ titleKey: LocalizedStringKey? = nil, selection: Binding<S>, type: QEMUConstant.Type) where T == AnyQEMUConstant {
init<S>(_ titleKey: LocalizedStringKey? = nil, selection: Binding<S>, type: any QEMUConstant.Type) {
self._stringSelection = Binding(get: {
(selection.wrappedValue as! QEMUConstant).rawValue
(selection.wrappedValue as! any QEMUConstant).rawValue
}, set: { newValue in
selection.wrappedValue = AnyQEMUConstant(rawValue: newValue) as! S
selection.wrappedValue = type.init(rawValue: newValue)! as! S
})
self.titleKey = titleKey
self.type = type
@ -57,7 +57,7 @@ struct VMConfigConstantPicker<T: QEMUConstant>: View {
struct VMConfigConstantPicker_Previews: PreviewProvider {
@State static private var fixedType: QEMUArchitecture = .aarch64
@State static private var dynamicType: QEMUCPU = QEMUCPU_aarch64.default
@State static private var dynamicType: any QEMUCPU = QEMUCPU_aarch64.default
static var previews: some View {
VStack {

View File

@ -21,7 +21,7 @@ struct VMConfigSerialView: View {
@Binding var system: UTMQemuConfigurationSystem
@State private var isUnsupportedAlertShown: Bool = false
@State private var hardware: QEMUSerialDevice = AnyQEMUConstant(rawValue: "")!
@State private var hardware: any QEMUSerialDevice = AnyQEMUConstant(rawValue: "")!
var body: some View {
VStack {

View File

@ -27,7 +27,7 @@ struct VMConfigSystemView: View {
@State private var warningMessage: WarningMessage? = nil
@State private var architecture: QEMUArchitecture = .x86_64
@State private var target: QEMUTarget = QEMUTarget_x86_64.pc
@State private var target: any QEMUTarget = QEMUTarget_x86_64.pc
var body: some View {
VStack {
@ -155,7 +155,7 @@ private enum WarningMessage: Identifiable {
private struct HardwareOptions: View {
@Binding var config: UTMQemuConfigurationSystem
@Binding var architecture: QEMUArchitecture
@Binding var target: QEMUTarget
@Binding var target: any QEMUTarget
@Binding var warningMessage: WarningMessage?
@EnvironmentObject private var data: UTMData
@ -200,7 +200,7 @@ private struct HardwareOptions: View {
struct CPUFlagsOptions: View {
let title: LocalizedStringKey
@Binding var config: UTMQemuConfigurationSystem
@Binding var flags: [QEMUCPUFlag]
@Binding var flags: [any QEMUCPUFlag]
@State private var showAllFlags: Bool = false
var body: some View {

View File

@ -92,7 +92,7 @@ enum VMWizardOS: String, Identifiable {
@Published var linuxBootArguments: String = ""
@Published var windowsBootVhdx: URL?
@Published var systemArchitecture: QEMUArchitecture = .x86_64
@Published var systemTarget: QEMUTarget = QEMUTarget_x86_64.pc
@Published var systemTarget: any QEMUTarget = QEMUTarget_x86_64.pc
#if os(macOS)
@Published var systemMemoryMib: Int = 4096
@Published var storageSizeGib: Int = 64

View File

@ -279,7 +279,7 @@ def generateEnum(name, values, prettyValues, baseName='QEMUConstant', defaultVal
def generateArchitectureAtlas(architectures, types):
output = f'extension QEMUArchitecture {{\n'
for k, v in types.items():
output += f' var {v}: {k}.Type {{\n'
output += f' var {v}: any {k}.Type {{\n'
output += f' switch self {{\n'
for a in architectures:
a = sanitizeName(a)