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

Code before:
    case unknownLocalAddress

    /// The address family of the multicast group was not valid for this `Channel`.
    case badMulticastGroupAddressFamily

    /// The address family of the provided multicast group join is not valid for this `Channel`.
    case badInterfaceAddressFamily

    /// An attempt was made to join a multicast group that does not correspond to a multicast
    /// address.
    case illegalMulticastAddress(SocketAddress)

    /// Multicast is not supported on Interface
    @available(*, deprecated, renamed: "NIOMulticastNotSupportedError")
    case multicastNotSupported(NIONetworkInterface)

    /// An operation that was inappropriate given the current `Channel` state was attempted.
    case inappropriateOperationForState

    /// An attempt was made to remove a ChannelHandler that is not removable.
    case unremovableHandler
}

extension ChannelError {
    // 'any Error' is unconditionally boxed, avoid allocating per use by statically boxing them.
    static let _alreadyClosed: any Error = ChannelError.alreadyClosed
    static let _inputClosed: any Error = ChannelError.inputClosed
    static let _ioOnClosedChannel: any Error = ChannelError.ioOnClosedChannel
    static let _operationUnsupported: any Error = ChannelError.operationUnsupported
    static let _outputClosed: any Error = ChannelError.outputClosed
    static let _unremovableHandler: any Error = ChannelError.unremovableHandler
}

extension ChannelError: Equatable { }

/// The removal of a `ChannelHandler` using `ChannelPipeline.removeHandler` has been attempted more than once.
public struct NIOAttemptedToRemoveHandlerMultipleTimesError: Error {}

public enum DatagramChannelError {
    public struct WriteOnUnconnectedSocketWithoutAddress: Error {
        public init() {}
    }

    public struct WriteOnConnectedSocketWithInvalidAddress: Error {
        let envelopeRemoteAddress: SocketAddress
        let connectedRemoteAddress: SocketAddress

        public init(
            envelopeRemoteAddress: SocketAddress,
            connectedRemoteAddress: SocketAddress
        ) {

Review comment (bug):
I am wondering if should file a compiler bug here and this is something that the compiler ought to be able to optimise. The error enum is in the same module and it has zero associated values. So it might as well store the instance in a global memory region

Code after:
    case unknownLocalAddress

    /// The address family of the multicast group was not valid for this `Channel`.
    case badMulticastGroupAddressFamily

    /// The address family of the provided multicast group join is not valid for this `Channel`.
    case badInterfaceAddressFamily

    /// An attempt was made to join a multicast group that does not correspond to a multicast
    /// address.
    case illegalMulticastAddress(SocketAddress)

    /// Multicast is not supported on Interface
    @available(*, deprecated, renamed: "NIOMulticastNotSupportedError")
    case multicastNotSupported(NIONetworkInterface)

    /// An operation that was inappropriate given the current `Channel` state was attempted.
    case inappropriateOperationForState

    /// An attempt was made to remove a ChannelHandler that is not removable.
    case unremovableHandler
}

extension ChannelError {
    // 'any Error' is unconditionally boxed, avoid allocating per use by statically boxing them.
    static let _alreadyClosed: any Error = ChannelError.alreadyClosed
    static let _inputClosed: any Error = ChannelError.inputClosed
    @usableFromInline
    static let _ioOnClosedChannel: any Error = ChannelError.ioOnClosedChannel
    static let _operationUnsupported: any Error = ChannelError.operationUnsupported
    static let _outputClosed: any Error = ChannelError.outputClosed
    static let _unremovableHandler: any Error = ChannelError.unremovableHandler
}

extension ChannelError: Equatable { }

/// The removal of a `ChannelHandler` using `ChannelPipeline.removeHandler` has been attempted more than once.
public struct NIOAttemptedToRemoveHandlerMultipleTimesError: Error {}

public enum DatagramChannelError {
    public struct WriteOnUnconnectedSocketWithoutAddress: Error {
        public init() {}
    }

    public struct WriteOnConnectedSocketWithInvalidAddress: Error {
        let envelopeRemoteAddress: SocketAddress
        let connectedRemoteAddress: SocketAddress

        public init(
            envelopeRemoteAddress: SocketAddress,
            connectedRemoteAddress: SocketAddress