Repository: apple/swift-nio
File: Sources/NIOHTTP1/HTTPServerPipelineHandler.swift (Swift)

Code before:

        guard let firstHead = maybeFirstHead else {
            return
        }

        self.eventBuffer.removeSubrange(firstHead...)
    }

    /// A utility function that runs the body code only in debug builds, without
    /// emitting compiler warnings.
    ///
    /// This is currently the only way to do this in Swift: see
    /// https://forums.swift.org/t/support-debug-only-code/11037 for a discussion.
    private func debugOnly(_ body: () -> Void) {
        checkPrecondition({ body(); return true }())
    }

    /// Calls assertionFailure if and only if `self.failOnPreconditions` is true. This allows us to avoid terminating the program in tests
    private func preconditionFailed(_ message: @autoclosure () -> String, file: StaticString = #file, line: UInt = #line) {
        if self.failOnPreconditions {
            assertionFailure(message(), file: file, line: line)
        }
    }

    /// Calls assert if and only if `self.failOnPreconditions` is true. This allows us to avoid terminating the program in tests
    private func checkPrecondition(
            _ closure: @autoclosure () -> Bool,
            _ message: @autoclosure () -> String = String(),
            file: StaticString = #file,
            line: UInt = #line
    ) {
        if self.failOnPreconditions {
            assert(closure(), message(), file: file, line: line)
        }
    }
}

@available(*, unavailable)
extension HTTPServerPipelineHandler: Sendable {}


Review comment (bug):
I think a minor nit here, but let's replace the word `precondition` in these two functions with `assertion`. They only fail in debug mode so they behave like assertions, not preconditions, and it'll be a little less confusing at the call site I think.

We can keep the word `precondition` in the error, that works well.

Code after:

        guard let firstHead = maybeFirstHead else {
            return
        }

        self.eventBuffer.removeSubrange(firstHead...)
    }

    /// A utility function that runs the body code only in debug builds, without
    /// emitting compiler warnings.
    ///
    /// This is currently the only way to do this in Swift: see
    /// https://forums.swift.org/t/support-debug-only-code/11037 for a discussion.
    private func debugOnly(_ body: () -> Void) {
        self.checkAssertion({ body(); return true }())
    }

    /// Calls assertionFailure if and only if `self.failOnPreconditions` is true. This allows us to avoid terminating the program in tests
    private func assertionFailed(_ message: @autoclosure () -> String, file: StaticString = #file, line: UInt = #line) {
        if self.failOnPreconditions {
            assertionFailure(message(), file: file, line: line)
        }
    }

    /// Calls assert if and only if `self.failOnPreconditions` is true. This allows us to avoid terminating the program in tests
    private func checkAssertion(
            _ closure: @autoclosure () -> Bool,
            _ message: @autoclosure () -> String = String(),
            file: StaticString = #file,
            line: UInt = #line
    ) {
        if self.failOnPreconditions {
            assert(closure(), message(), file: file, line: line)
        }
    }
}

@available(*, unavailable)
extension HTTPServerPipelineHandler: Sendable {}