Repository: apple/swift-nio
File: Sources/NIOEmbedded/Embedded.swift (Swift)

Code before:
    }

    public func errorCaught0(error: Error) {
        self.eventLoop.preconditionInEventLoop()
        if self.error == nil {
            self.error = error
        }
    }

    private func addToBuffer(
        buffer: inout CircularBuffer<NIOAny>,
        consumer: inout Deque<(Result<NIOAny, Error>) -> Void>,
        data: NIOAny
    ) {
        self.eventLoop.preconditionInEventLoop()
        if let consume = consumer.popFirst() {
            consume(.success(data))
        } else {
            buffer.append(data)
        }
    }

    /// Enqueue a consumer closure that will be invoked upon the next pending inbound write.
    /// - Parameter newElement: The consumer closure to enqueue. Returns a `.failure` result if the channel has already
    ///   closed.
    func enqueueInboundBufferConsumer(_ newElement: @escaping (Result<NIOAny, Error>) -> Void) {
        self.eventLoop.preconditionInEventLoop()

        // The channel has already closed: there cannot be any further writes. Return a `.failure` result with an error.
        guard self.isOpen else {
            newElement(.failure(ChannelError.ioOnClosedChannel))
            return
        }

        self.inboundBufferConsumer.append(newElement)
    }

    /// Enqueue a consumer closure that will be invoked upon the next pending outbound write.
    /// - Parameter newElement: The consumer closure to enqueue. Returns a `.failure` result if the channel has already
    ///   closed.
    func enqueueOutboundBufferConsumer(_ newElement: @escaping (Result<NIOAny, Error>) -> Void) {
        self.eventLoop.preconditionInEventLoop()

        // The channel has already closed: there cannot be any further writes. Return a `.failure` result with an error.
        guard self.isOpen else {
            newElement(.failure(ChannelError.ioOnClosedChannel))
            return
        }

        self.outboundBufferConsumer.append(newElement)
    }

Review comment (suggestion):
Nit: we typically annotate methods only supposed to be called on the EL with a leading underscore, and I recommend making this `private` if it can be.

Code after:
    }

    public func errorCaught0(error: Error) {
        self.eventLoop.preconditionInEventLoop()
        if self.error == nil {
            self.error = error
        }
    }

    private func addToBuffer(
        buffer: inout CircularBuffer<NIOAny>,
        consumer: inout Deque<(Result<NIOAny, Error>) -> Void>,
        data: NIOAny
    ) {
        self.eventLoop.preconditionInEventLoop()
        if let consume = consumer.popFirst() {
            consume(.success(data))
        } else {
            buffer.append(data)
        }
    }

    /// Enqueue a consumer closure that will be invoked upon the next pending inbound write.
    /// - Parameter newElement: The consumer closure to enqueue. Returns a `.failure` result if the channel has already
    ///   closed.
    func _enqueueInboundBufferConsumer(_ newElement: @escaping (Result<NIOAny, Error>) -> Void) {
        self.eventLoop.preconditionInEventLoop()

        // The channel has already closed: there cannot be any further writes. Return a `.failure` result with an error.
        guard self.isOpen else {
            newElement(.failure(ChannelError.ioOnClosedChannel))
            return
        }

        self.inboundBufferConsumer.append(newElement)
    }

    /// Enqueue a consumer closure that will be invoked upon the next pending outbound write.
    /// - Parameter newElement: The consumer closure to enqueue. Returns a `.failure` result if the channel has already
    ///   closed.
    func _enqueueOutboundBufferConsumer(_ newElement: @escaping (Result<NIOAny, Error>) -> Void) {
        self.eventLoop.preconditionInEventLoop()

        // The channel has already closed: there cannot be any further writes. Return a `.failure` result with an error.
        guard self.isOpen else {
            newElement(.failure(ChannelError.ioOnClosedChannel))
            return
        }

        self.outboundBufferConsumer.append(newElement)
    }