Repository: apple/swift-nio
File: Sources/NIOCore/AsyncSequences/NIOThrowingAsyncSequenceProducer.swift (Swift)

Code before:
                        switch action {
                        case .callProduceMore:
                            let delegate = _delegate
                            self._lock.unlock()

                            delegate?.produceMore()

                        case .none:
                            self._lock.unlock()
                        }
                    }
                }
            } onCancel: {
                let delegate: Delegate? = self._lock.withLock {
                    let action = self._stateMachine.cancelled()

                    switch action {
                    case .callDidTerminate:
                        let delegate = self._delegate
                        self._delegate = nil

                        return delegate

                    case .resumeContinuationWithCancellationErrorAndCallDidTerminate(let continuation):
                        // We have deprecated the generic Failure type in the public API and Failure should
                        // now be `Swift.Error`. However, if users have not migrated to the new API the could
                        // still use a custom generic Error type and this cast might fail.
                        // In addition, we use `NIOThrowingAsyncSequenceProducer` in the implementation of the
                        // non-throwing variant `NIOAsyncSequenceProducer` where `Failure` will be `Never` and
                        // this cast will fail as well.
                        // Everything is marked @inlinable and the Failure type is know at compile time,
                        // therefore this cast should be optimised away in release build.
                        if let failure = CancellationError() as? Failure {
                            continuation.resume(throwing: failure)
                        } else {
                            continuation.resume(returning: nil)
                        }
                        
                        let delegate = self._delegate
                        self._delegate = nil

                        return delegate

                    case .none:
                        return nil
                    }
                }

                delegate?.didTerminate()
            }
        }

Review comment (suggestion):
```suggestion
                        // now be `Swift.Error`. However, if users have not migrated to the new API they could
```

Code after:
                        switch action {
                        case .callProduceMore:
                            let delegate = _delegate
                            self._lock.unlock()

                            delegate?.produceMore()

                        case .none:
                            self._lock.unlock()
                        }
                    }
                }
            } onCancel: {
                let delegate: Delegate? = self._lock.withLock {
                    let action = self._stateMachine.cancelled()

                    switch action {
                    case .callDidTerminate:
                        let delegate = self._delegate
                        self._delegate = nil

                        return delegate

                    case .resumeContinuationWithCancellationErrorAndCallDidTerminate(let continuation):
                        // We have deprecated the generic Failure type in the public API and Failure should
                        // now be `Swift.Error`. However, if users have not migrated to the new API they could
                        // still use a custom generic Error type and this cast might fail.
                        // In addition, we use `NIOThrowingAsyncSequenceProducer` in the implementation of the
                        // non-throwing variant `NIOAsyncSequenceProducer` where `Failure` will be `Never` and
                        // this cast will fail as well.
                        // Everything is marked @inlinable and the Failure type is known at compile time,
                        // therefore this cast should be optimised away in release build.
                        if let failure = CancellationError() as? Failure {
                            continuation.resume(throwing: failure)
                        } else {
                            continuation.resume(returning: nil)
                        }
                        
                        let delegate = self._delegate
                        self._delegate = nil

                        return delegate

                    case .none:
                        return nil
                    }
                }

                delegate?.didTerminate()
            }
        }