From 359977d87b67ea92799aad0448e0c985e4483143 Mon Sep 17 00:00:00 2001 From: Steffan Andrews Date: Thu, 5 Aug 2021 11:11:49 -0700 Subject: [PATCH 1/2] Changed capture semantics --- Sources/MIDIKit/IO/Managed/Input.swift | 20 ++++++++--------- .../MIDIKit/IO/Managed/InputConnection.swift | 18 +++++++-------- Sources/MIDIKit/IO/Managed/Output.swift | 16 +++++++------- .../MIDIKit/IO/Managed/OutputConnection.swift | 22 +++++++++---------- .../MIDIKit/IO/Managed/ThruConnection.swift | 4 ++-- .../MIDIKit/IO/Manager/Manager State.swift | 4 ++-- Sources/MIDIKit/Parser/MIDI1Parser.swift | 16 +++++++------- 7 files changed, 50 insertions(+), 50 deletions(-) diff --git a/Sources/MIDIKit/IO/Managed/Input.swift b/Sources/MIDIKit/IO/Managed/Input.swift index d9e13ac715..44336e2553 100644 --- a/Sources/MIDIKit/IO/Managed/Input.swift +++ b/Sources/MIDIKit/IO/Managed/Input.swift @@ -50,11 +50,11 @@ extension MIDI.IO.Input { /// Queries the system and returns true if the endpoint exists (by matching port name and unique ID) internal var uniqueIDExistsInSystem: MIDIEndpointRef? { - guard let uniqueID = self.uniqueID else { + guard let unwrappedUniqueID = self.uniqueID else { return nil } - if let endpoint = MIDI.IO.getSystemDestinationEndpoint(matching: uniqueID.coreMIDIUniqueID) { + if let endpoint = MIDI.IO.getSystemDestinationEndpoint(matching: unwrappedUniqueID.coreMIDIUniqueID) { return endpoint } @@ -84,9 +84,9 @@ extension MIDI.IO.Input { ._1_0, &newPortRef, { [weak self] eventListPtr, srcConnRefCon in - guard let self = self else { return } - self.midiManager?.queue.async { - self.receiveHandler.midiReceiveBlock(eventListPtr, srcConnRefCon) + guard let strongSelf = self else { return } + strongSelf.midiManager?.queue.async { + strongSelf.receiveHandler.midiReceiveBlock(eventListPtr, srcConnRefCon) } } @@ -100,9 +100,9 @@ extension MIDI.IO.Input { endpointName as CFString, &newPortRef, { [weak self] packetListPtr, srcConnRefCon in - guard let self = self else { return } - self.midiManager?.queue.async { - self.receiveHandler.midiReadBlock(packetListPtr, srcConnRefCon) + guard let strongSelf = self else { return } + strongSelf.midiManager?.queue.async { + strongSelf.receiveHandler.midiReadBlock(packetListPtr, srcConnRefCon) } } ) @@ -132,11 +132,11 @@ extension MIDI.IO.Input { /// Errors thrown can be safely ignored and are typically only useful for debugging purposes. internal func dispose() throws { - guard let portRef = self.portRef else { return } + guard let unwrappedPortRef = self.portRef else { return } defer { self.portRef = nil } - try MIDIEndpointDispose(portRef) + try MIDIEndpointDispose(unwrappedPortRef) .throwIfOSStatusErr() } diff --git a/Sources/MIDIKit/IO/Managed/InputConnection.swift b/Sources/MIDIKit/IO/Managed/InputConnection.swift index 5596d67f6d..ceea11ec12 100644 --- a/Sources/MIDIKit/IO/Managed/InputConnection.swift +++ b/Sources/MIDIKit/IO/Managed/InputConnection.swift @@ -82,9 +82,9 @@ extension MIDI.IO.InputConnection { ._1_0, &newConnection, { [weak self] eventListPtr, srcConnRefCon in - guard let self = self else { return } - self.midiManager?.queue.async { - self.receiveHandler.midiReceiveBlock(eventListPtr, srcConnRefCon) + guard let strongSelf = self else { return } + strongSelf.midiManager?.queue.async { + strongSelf.receiveHandler.midiReceiveBlock(eventListPtr, srcConnRefCon) } } ) @@ -97,9 +97,9 @@ extension MIDI.IO.InputConnection { UUID().uuidString as CFString, &newConnection, { [weak self] packetListPtr, srcConnRefCon in - guard let self = self else { return } - self.midiManager?.queue.async { - self.receiveHandler.midiReadBlock(packetListPtr, srcConnRefCon) + guard let strongSelf = self else { return } + strongSelf.midiManager?.queue.async { + strongSelf.receiveHandler.midiReadBlock(packetListPtr, srcConnRefCon) } } ) @@ -126,12 +126,12 @@ extension MIDI.IO.InputConnection { isConnected = false - guard let inputPortRef = self.inputPortRef, - let outputEndpointRef = self.outputEndpointRef else { return } + guard let upwrappedInputPortRef = self.inputPortRef, + let upwrappedOutputEndpointRef = self.outputEndpointRef else { return } defer { self.inputPortRef = nil } - try MIDIPortDisconnectSource(inputPortRef, outputEndpointRef) + try MIDIPortDisconnectSource(upwrappedInputPortRef, upwrappedOutputEndpointRef) .throwIfOSStatusErr() } diff --git a/Sources/MIDIKit/IO/Managed/Output.swift b/Sources/MIDIKit/IO/Managed/Output.swift index 05c092cdd9..f77065f79a 100644 --- a/Sources/MIDIKit/IO/Managed/Output.swift +++ b/Sources/MIDIKit/IO/Managed/Output.swift @@ -42,11 +42,11 @@ extension MIDI.IO.Output { /// Queries the system and returns true if the endpoint exists (by matching port name and unique ID) public var uniqueIDExistsInSystem: MIDIEndpointRef? { - guard let uniqueID = self.uniqueID else { + guard let upwrappedUniqueID = self.uniqueID else { return nil } - if let endpoint = MIDI.IO.getSystemSourceEndpoint(matching: uniqueID.coreMIDIUniqueID) { + if let endpoint = MIDI.IO.getSystemSourceEndpoint(matching: upwrappedUniqueID.coreMIDIUniqueID) { return endpoint } @@ -111,11 +111,11 @@ extension MIDI.IO.Output { /// Errors thrown can be safely ignored and are typically only useful for debugging purposes. internal func dispose() throws { - guard let portRef = self.portRef else { return } + guard let upwrappedPortRef = self.portRef else { return } defer { self.portRef = nil } - try MIDIEndpointDispose(portRef) + try MIDIEndpointDispose(upwrappedPortRef) .throwIfOSStatusErr() } @@ -141,13 +141,13 @@ extension MIDI.IO.Output: MIDIIOSendsMIDIMessagesProtocol { public func send(packetList: UnsafeMutablePointer) throws { - guard let portRef = self.portRef else { + guard let upwrappedPortRef = self.portRef else { throw MIDI.IO.MIDIError.internalInconsistency( "Port reference is nil." ) } - try MIDIReceived(portRef, packetList) + try MIDIReceived(upwrappedPortRef, packetList) .throwIfOSStatusErr() } @@ -155,13 +155,13 @@ extension MIDI.IO.Output: MIDIIOSendsMIDIMessagesProtocol { @available(macOS 11, iOS 14, macCatalyst 14, tvOS 14, watchOS 7, *) public func send(eventList: UnsafeMutablePointer) throws { - guard let portRef = self.portRef else { + guard let upwrappedPortRef = self.portRef else { throw MIDI.IO.MIDIError.internalInconsistency( "Port reference is nil." ) } - try MIDIReceivedEventList(portRef, eventList) + try MIDIReceivedEventList(upwrappedPortRef, eventList) .throwIfOSStatusErr() } diff --git a/Sources/MIDIKit/IO/Managed/OutputConnection.swift b/Sources/MIDIKit/IO/Managed/OutputConnection.swift index 5e778f60bd..4efea23b5d 100644 --- a/Sources/MIDIKit/IO/Managed/OutputConnection.swift +++ b/Sources/MIDIKit/IO/Managed/OutputConnection.swift @@ -86,15 +86,15 @@ extension MIDI.IO.OutputConnection { isConnected = false - guard let outputPortRef = self.portRef, - let inputEndpointRef = self.inputEndpointRef else { return } + guard let upwrappedOutputPortRef = self.portRef, + let upwrappedInputEndpointRef = self.inputEndpointRef else { return } defer { self.portRef = nil self.inputEndpointRef = nil } - try MIDIPortDisconnectSource(outputPortRef, inputEndpointRef) + try MIDIPortDisconnectSource(upwrappedOutputPortRef, upwrappedInputEndpointRef) .throwIfOSStatusErr() } @@ -150,20 +150,20 @@ extension MIDI.IO.OutputConnection: MIDIIOSendsMIDIMessagesProtocol { public func send(packetList: UnsafeMutablePointer) throws { - guard let outputPortRef = self.portRef else { + guard let upwrappedOutputPortRef = self.portRef else { throw MIDI.IO.MIDIError.internalInconsistency( "Output port reference is nil." ) } - guard let inputEndpointRef = self.inputEndpointRef else { + guard let upwrappedInputEndpointRef = self.inputEndpointRef else { throw MIDI.IO.MIDIError.internalInconsistency( "Input port reference is nil." ) } - try MIDISend(outputPortRef, - inputEndpointRef, + try MIDISend(upwrappedOutputPortRef, + upwrappedInputEndpointRef, packetList) .throwIfOSStatusErr() @@ -172,20 +172,20 @@ extension MIDI.IO.OutputConnection: MIDIIOSendsMIDIMessagesProtocol { @available(macOS 11, iOS 14, macCatalyst 14, tvOS 14, watchOS 7, *) public func send(eventList: UnsafeMutablePointer) throws { - guard let outputPortRef = self.portRef else { + guard let upwrappedOutputPortRef = self.portRef else { throw MIDI.IO.MIDIError.internalInconsistency( "Output port reference is nil." ) } - guard let inputEndpointRef = self.inputEndpointRef else { + guard let upwrappedInputEndpointRef = self.inputEndpointRef else { throw MIDI.IO.MIDIError.internalInconsistency( "Input port reference is nil." ) } - try MIDISendEventList(outputPortRef, - inputEndpointRef, + try MIDISendEventList(upwrappedOutputPortRef, + upwrappedInputEndpointRef, eventList) .throwIfOSStatusErr() diff --git a/Sources/MIDIKit/IO/Managed/ThruConnection.swift b/Sources/MIDIKit/IO/Managed/ThruConnection.swift index 4c1855fbfc..24f119a270 100644 --- a/Sources/MIDIKit/IO/Managed/ThruConnection.swift +++ b/Sources/MIDIKit/IO/Managed/ThruConnection.swift @@ -186,13 +186,13 @@ extension MIDI.IO.ThruConnection { /// Errors thrown can be safely ignored and are typically only useful for debugging purposes. internal func dispose() throws { - guard let thruConnectionRef = self.thruConnectionRef else { return } + guard let upwrappedThruConnectionRef = self.thruConnectionRef else { return } defer { self.thruConnectionRef = nil } - try MIDIThruConnectionDispose(thruConnectionRef) + try MIDIThruConnectionDispose(upwrappedThruConnectionRef) .throwIfOSStatusErr() } diff --git a/Sources/MIDIKit/IO/Manager/Manager State.swift b/Sources/MIDIKit/IO/Manager/Manager State.swift index c525aca944..819e9241da 100644 --- a/Sources/MIDIKit/IO/Manager/Manager State.swift +++ b/Sources/MIDIKit/IO/Manager/Manager State.swift @@ -21,8 +21,8 @@ extension MIDI.IO.Manager { try MIDIClientCreateWithBlock(clientName as CFString, &clientRef) { [weak self] notificationPtr in - guard let self = self else { return } - self.internalNotificationHandler(notificationPtr) + guard let strongSelf = self else { return } + strongSelf.internalNotificationHandler(notificationPtr) } .throwIfOSStatusErr() diff --git a/Sources/MIDIKit/Parser/MIDI1Parser.swift b/Sources/MIDIKit/Parser/MIDI1Parser.swift index 79af9bb84e..39d86eebea 100644 --- a/Sources/MIDIKit/Parser/MIDI1Parser.swift +++ b/Sources/MIDIKit/Parser/MIDI1Parser.swift @@ -400,11 +400,11 @@ extension MIDI { case 0xE: // pitch bend let channel = statusByte.nibbles.low - guard let dataByte1 = dataByte1, - let dataByte2 = dataByte2 + guard let unwrappedDataByte1 = dataByte1, + let unwrappedDataByte2 = dataByte2 else { return events } - let uint14 = MIDI.UInt14(bytePair: .init(msb: dataByte2, lsb: dataByte1)) + let uint14 = MIDI.UInt14(bytePair: .init(msb: unwrappedDataByte2, lsb: unwrappedDataByte1)) events.append(.pitchBend(value: uint14, channel: channel)) case 0xF: // system message @@ -416,17 +416,17 @@ extension MIDI { events.append(parsedSysEx) case 0x1: // System Common - timecode quarter-frame - guard let dataByte = dataByte1 + guard let unwrappedDataByte1 = dataByte1 else { return events } - events.append(.timecodeQuarterFrame(byte: dataByte)) + events.append(.timecodeQuarterFrame(byte: unwrappedDataByte1)) case 0x2: // System Common - Song Position Pointer - guard let dataByte1 = dataByte1, - let dataByte2 = dataByte2 + guard let unwrappedDataByte1 = dataByte1, + let unwrappedDataByte2 = dataByte2 else { return events } - let uint14 = MIDI.UInt14(bytePair: .init(msb: dataByte2, lsb: dataByte1)) + let uint14 = MIDI.UInt14(bytePair: .init(msb: unwrappedDataByte2, lsb: unwrappedDataByte1)) events.append(.songPositionPointer(midiBeat: uint14)) case 0x3: // System Common - Song Select From 676320e856a3f0346374b1e5320f8c569dac12f5 Mon Sep 17 00:00:00 2001 From: Steffan Andrews Date: Thu, 5 Aug 2021 11:17:03 -0700 Subject: [PATCH 2/2] Changed capture semantics --- .../MIDISystemInfo/MIDISystemInfo/DetailsView.swift | 4 ++-- .../MIDISystemInfo/MIDISystemInfo/WebKitView.swift | 4 ++-- Sources/MIDIKit/IO/Managed/Input.swift | 8 ++++---- Sources/MIDIKit/IO/Managed/InputConnection.swift | 12 ++++++------ Sources/MIDIKit/IO/Managed/Output.swift | 8 ++++---- Sources/MIDIKit/IO/Managed/OutputConnection.swift | 12 ++++++------ Sources/MIDIKit/IO/Managed/ThruConnection.swift | 4 ++-- .../Inputs and Outputs/InputConnection Tests.swift | 4 ++-- .../Inputs and Outputs/OutputConnection Tests.swift | 4 ++-- 9 files changed, 30 insertions(+), 30 deletions(-) diff --git a/Examples/MIDISystemInfo/MIDISystemInfo/DetailsView.swift b/Examples/MIDISystemInfo/MIDISystemInfo/DetailsView.swift index bac9ec9002..a3e949fbfd 100644 --- a/Examples/MIDISystemInfo/MIDISystemInfo/DetailsView.swift +++ b/Examples/MIDISystemInfo/MIDISystemInfo/DetailsView.swift @@ -121,11 +121,11 @@ struct DetailsView: View where T : MIDIIOObjectProtocol { var body: some View { - if let endpoint = endpoint { + if let unwrappedEndpoint = endpoint { WebKitView(dynamicHeight: $webViewHeight, webview: $webview, - html: generateHTML(endpoint)) + html: generateHTML(unwrappedEndpoint)) Group { if showAll { diff --git a/Examples/MIDISystemInfo/MIDISystemInfo/WebKitView.swift b/Examples/MIDISystemInfo/MIDISystemInfo/WebKitView.swift index 47a2b46537..f612de1959 100644 --- a/Examples/MIDISystemInfo/MIDISystemInfo/WebKitView.swift +++ b/Examples/MIDISystemInfo/MIDISystemInfo/WebKitView.swift @@ -31,8 +31,8 @@ struct WebKitView: NSViewRepresentable { "document.documentElement.scrollHeight", completionHandler: { (height, error) in DispatchQueue.main.async { - if let height = height as? CGFloat { - self.parent.dynamicHeight = height + if let unwrappedHeight = height as? CGFloat { + self.parent.dynamicHeight = unwrappedHeight } } }) diff --git a/Sources/MIDIKit/IO/Managed/Input.swift b/Sources/MIDIKit/IO/Managed/Input.swift index 44336e2553..8cd80c6394 100644 --- a/Sources/MIDIKit/IO/Managed/Input.swift +++ b/Sources/MIDIKit/IO/Managed/Input.swift @@ -115,10 +115,10 @@ extension MIDI.IO.Input { _ = try? MIDI.IO.setModel(of: newPortRef, to: manager.model) _ = try? MIDI.IO.setManufacturer(of: newPortRef, to: manager.manufacturer) - if let uniqueID = self.uniqueID { + if let unwrappedUniqueID = self.uniqueID { // inject previously-stored unique ID into port try MIDI.IO.setUniqueID(of: newPortRef, - to: uniqueID.coreMIDIUniqueID) + to: unwrappedUniqueID.coreMIDIUniqueID) } else { // if managed ID is nil, either it was not supplied or it was already in use // so fetch the new ID from the port we just created @@ -148,8 +148,8 @@ extension MIDI.IO.Input: CustomStringConvertible { public var description: String { var uniqueIDString: String = "nil" - if let uniqueID = uniqueID { - uniqueIDString = "\(uniqueID)" + if let unwrappedUniqueID = uniqueID { + uniqueIDString = "\(unwrappedUniqueID)" } return "Input(name: \(endpointName.quoted), uniqueID: \(uniqueIDString))" diff --git a/Sources/MIDIKit/IO/Managed/InputConnection.swift b/Sources/MIDIKit/IO/Managed/InputConnection.swift index ceea11ec12..7b30a9b5a5 100644 --- a/Sources/MIDIKit/IO/Managed/InputConnection.swift +++ b/Sources/MIDIKit/IO/Managed/InputConnection.swift @@ -162,19 +162,19 @@ extension MIDI.IO.InputConnection: CustomStringConvertible { public var description: String { var outputEndpointName: String = "?" - if let outputEndpointRef = outputEndpointRef, - let getName = try? MIDI.IO.getName(of: outputEndpointRef) { + if let unwrappedOutputEndpointRef = outputEndpointRef, + let getName = try? MIDI.IO.getName(of: unwrappedOutputEndpointRef) { outputEndpointName = "\(getName)".quoted } var outputEndpointRefString: String = "nil" - if let outputEndpointRef = outputEndpointRef { - outputEndpointRefString = "\(outputEndpointRef)" + if let unwrappedOutputEndpointRef = outputEndpointRef { + outputEndpointRefString = "\(unwrappedOutputEndpointRef)" } var inputPortRefString: String = "nil" - if let inputPortRef = inputPortRef { - inputPortRefString = "\(inputPortRef)" + if let unwrappedInputPortRef = inputPortRef { + inputPortRefString = "\(unwrappedInputPortRef)" } return "InputConnection(criteria: \(outputCriteria), outputEndpointRef: \(outputEndpointRefString) \(outputEndpointName), inputPortRef: \(inputPortRefString), isConnected: \(isConnected))" diff --git a/Sources/MIDIKit/IO/Managed/Output.swift b/Sources/MIDIKit/IO/Managed/Output.swift index f77065f79a..dfc4c85bf6 100644 --- a/Sources/MIDIKit/IO/Managed/Output.swift +++ b/Sources/MIDIKit/IO/Managed/Output.swift @@ -94,10 +94,10 @@ extension MIDI.IO.Output { _ = try? MIDI.IO.setModel(of: newPortRef, to: manager.model) _ = try? MIDI.IO.setManufacturer(of: newPortRef, to: manager.manufacturer) - if let uniqueID = self.uniqueID { + if let unwrappedUniqueID = self.uniqueID { // inject previously-stored unique ID into port try MIDI.IO.setUniqueID(of: newPortRef, - to: uniqueID.coreMIDIUniqueID) + to: unwrappedUniqueID.coreMIDIUniqueID) } else { // if managed ID is nil, either it was not supplied or it was already in use // so fetch the new ID from the port we just created @@ -127,8 +127,8 @@ extension MIDI.IO.Output: CustomStringConvertible { public var description: String { var uniqueIDString: String = "nil" - if let uniqueID = uniqueID { - uniqueIDString = "\(uniqueID)" + if let unwrappedUniqueID = uniqueID { + uniqueIDString = "\(unwrappedUniqueID)" } return "Output(name: \(endpointName.quoted), uniqueID: \(uniqueIDString))" diff --git a/Sources/MIDIKit/IO/Managed/OutputConnection.swift b/Sources/MIDIKit/IO/Managed/OutputConnection.swift index 4efea23b5d..0707c49671 100644 --- a/Sources/MIDIKit/IO/Managed/OutputConnection.swift +++ b/Sources/MIDIKit/IO/Managed/OutputConnection.swift @@ -125,19 +125,19 @@ extension MIDI.IO.OutputConnection: CustomStringConvertible { public var description: String { var inputEndpointName: String = "?" - if let inputEndpointRef = inputEndpointRef, - let getName = try? MIDI.IO.getName(of: inputEndpointRef) { + if let unwrappedInputEndpointRef = inputEndpointRef, + let getName = try? MIDI.IO.getName(of: unwrappedInputEndpointRef) { inputEndpointName = "\(getName)".quoted } var inputEndpointRefString: String = "nil" - if let inputEndpointRef = inputEndpointRef { - inputEndpointRefString = "\(inputEndpointRef)" + if let unwrappediInputEndpointRef = inputEndpointRef { + inputEndpointRefString = "\(unwrappediInputEndpointRef)" } var outputPortRefString: String = "nil" - if let portRef = portRef { - outputPortRefString = "\(portRef)" + if let unwrappedPortRef = portRef { + outputPortRefString = "\(unwrappedPortRef)" } return "OutputConnection(criteria: \(inputCriteria), inputEndpointRef: \(inputEndpointRefString) \(inputEndpointName), outputPortRef: \(outputPortRefString), isConnected: \(isConnected))" diff --git a/Sources/MIDIKit/IO/Managed/ThruConnection.swift b/Sources/MIDIKit/IO/Managed/ThruConnection.swift index 24f119a270..40501be914 100644 --- a/Sources/MIDIKit/IO/Managed/ThruConnection.swift +++ b/Sources/MIDIKit/IO/Managed/ThruConnection.swift @@ -204,8 +204,8 @@ extension MIDI.IO.ThruConnection: CustomStringConvertible { public var description: String { var thruConnectionRefString: String = "nil" - if let thruConnectionRef = thruConnectionRef { - thruConnectionRefString = "\(thruConnectionRef)" + if let unwrappedThruConnectionRef = thruConnectionRef { + thruConnectionRefString = "\(unwrappedThruConnectionRef)" } return "ThruConnection(ref: \(thruConnectionRefString), outputs: \(outputs), inputs: \(inputs), \(lifecycle)" diff --git a/Tests/MIDIKitTests/IO/Inputs and Outputs/InputConnection Tests.swift b/Tests/MIDIKitTests/IO/Inputs and Outputs/InputConnection Tests.swift index 9610f49d8a..e5ba37b645 100644 --- a/Tests/MIDIKitTests/IO/Inputs and Outputs/InputConnection Tests.swift +++ b/Tests/MIDIKitTests/IO/Inputs and Outputs/InputConnection Tests.swift @@ -51,8 +51,8 @@ final class InputsAndOutputs_InputConnection_Tests: XCTestCase { XCTFail(error.localizedDescription) ; return } - if let caughtErr = caughtErr as? MIDI.IO.MIDIError, - case .connectionError = caughtErr { + if let castCaughtErr = caughtErr as? MIDI.IO.MIDIError, + case .connectionError = castCaughtErr { // correct - expect error to be present } else { diff --git a/Tests/MIDIKitTests/IO/Inputs and Outputs/OutputConnection Tests.swift b/Tests/MIDIKitTests/IO/Inputs and Outputs/OutputConnection Tests.swift index 7e4c17d9b7..b79409153c 100644 --- a/Tests/MIDIKitTests/IO/Inputs and Outputs/OutputConnection Tests.swift +++ b/Tests/MIDIKitTests/IO/Inputs and Outputs/OutputConnection Tests.swift @@ -50,8 +50,8 @@ final class InputsAndOutputs_OutputConnection_Tests: XCTestCase { XCTFail(error.localizedDescription) ; return } - if let caughtErr = caughtErr as? MIDI.IO.MIDIError, - case .connectionError = caughtErr { + if let castCaughtErr = caughtErr as? MIDI.IO.MIDIError, + case .connectionError = castCaughtErr { // correct - expect error to be present } else {