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

Code before:
///
/// Most of these are closures that pull a value from one future, call a user callback, push the
/// result into another, then return a list of callbacks from the target future that are now ready to be invoked.
///
/// In particular, note that `_run()` here continues to obtain and execute lists of callbacks until it completes.
/// This eliminates recursion when processing `flatMap()` chains.
@usableFromInline
internal struct CallbackList {
    @usableFromInline
    internal typealias Element = Wrapper

    // The compiler is able to better optimize a struct holding a closure than just a raw closure
    // when used as a generic parameter.
    @usableFromInline
    struct Wrapper {
        @usableFromInline
        var callback: () -> CallbackList

        @inlinable
        init(_ callback: @escaping () -> CallbackList) {
            self.callback = callback
        }
    }

    @usableFromInline
    internal var firstCallback: Optional<Wrapper>
    @usableFromInline
    internal var furtherCallbacks: Optional<[Wrapper]>

    @inlinable
    internal init() {
        self.firstCallback = nil
        self.furtherCallbacks = nil
    }

    @inlinable
    internal mutating func append(_ callback: @escaping () -> CallbackList) {
        if self.firstCallback == nil {
            self.firstCallback = Wrapper(callback)
        } else {
            if self.furtherCallbacks != nil {
                self.furtherCallbacks!.append(Wrapper(callback))
            } else {
                self.furtherCallbacks = [Wrapper(callback)]
            }
        }
    }

    @inlinable
    internal func _allCallbacks() -> CircularBuffer<Element> {
        switch (self.firstCallback, self.furtherCallbacks) {

Review comment (suggestion):
Did we need to rename this? It's called `Element`.

Code after:
///
/// Most of these are closures that pull a value from one future, call a user callback, push the
/// result into another, then return a list of callbacks from the target future that are now ready to be invoked.
///
/// In particular, note that `_run()` here continues to obtain and execute lists of callbacks until it completes.
/// This eliminates recursion when processing `flatMap()` chains.
@usableFromInline
internal struct CallbackList {
    @usableFromInline
    internal typealias Element = Wrapper

    // The compiler is able to better optimize a struct holding a closure than just a raw closure
    // when used as a generic parameter.
    @usableFromInline
    struct Wrapper {
        @usableFromInline
        var callback: () -> CallbackList

        @inlinable
        init(_ callback: @escaping () -> CallbackList) {
            self.callback = callback
        }
    }

    @usableFromInline
    internal var firstCallback: Optional<Element>
    @usableFromInline
    internal var furtherCallbacks: Optional<[Element]>

    @inlinable
    internal init() {
        self.firstCallback = nil
        self.furtherCallbacks = nil
    }

    @inlinable
    internal mutating func append(_ callback: @escaping () -> CallbackList) {
        if self.firstCallback == nil {
            self.firstCallback = Wrapper(callback)
        } else {
            if self.furtherCallbacks != nil {
                self.furtherCallbacks!.append(Wrapper(callback))
            } else {
                self.furtherCallbacks = [Wrapper(callback)]
            }
        }
    }

    @inlinable
    internal func _allCallbacks() -> CircularBuffer<Element> {
        switch (self.firstCallback, self.furtherCallbacks) {