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

Code before:
        position: ChannelPipeline.Position = .last
    ) async throws {
        try await self.addHandlers(handlers, position: position).map { UnsafeTransfer($0) }.get().wrappedValue
    }

    @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
    @preconcurrency
    public func addHandlers(
        _ handlers: (ChannelHandler & Sendable)...,
        position: ChannelPipeline.Position = .last
    ) async throws {
        try await self.addHandlers(handlers, position: position)
    }
}

/// An error that is thrown when the number of bytes in an AsyncSequence exceeds the limit.
///
/// When collecting the bytes from an AsyncSequence, there is a limit up to where the content
/// exceeds a certain threshold beyond which the content isn't matching the expected maximum size.
/// that will be processed. This error is generally thrown when it is discovered that there are
/// more bytes in a sequence than what was specified as the maximum. It could be that this upTo
/// limit should be increased, or that the sequence has unexpected content in it.
///
/// - maxBytes: (optional) current limit on the maximum number of bytes in the sequence
public struct NIOTooManyBytesError: Error, Hashable {
    let maxBytes: Int?

    public init() {
        self.maxBytes = nil
    }

    public init(maxBytes: Int) {
        self.maxBytes = maxBytes
    }
}

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
extension AsyncSequence where Element: RandomAccessCollection, Element.Element == UInt8 {
    /// Accumulates an `AsyncSequence` of `RandomAccessCollection`s into a single `accumulationBuffer`.
    /// - Parameters:
    ///   - accumulationBuffer: buffer to write all the elements of `self` into
    ///   - maxBytes: The maximum number of bytes this method is allowed to write into `accumulationBuffer`
    /// - Throws: `NIOTooManyBytesError` if the the sequence contains more than `maxBytes`.
    /// Note that previous elements of `self` might already be write to `accumulationBuffer`.
    @inlinable
    public func collect(
        upTo maxBytes: Int,
        into accumulationBuffer: inout ByteBuffer
    ) async throws {
        precondition(maxBytes >= 0, "`maxBytes` must be greater than or equal to zero")
        var bytesRead = 0

Review comment (suggestion):
This should be a var. no need to restrict it to a let as there are no invariants or similar that need to be verified. 

Code after:
        position: ChannelPipeline.Position = .last
    ) async throws {
        try await self.addHandlers(handlers, position: position).map { UnsafeTransfer($0) }.get().wrappedValue
    }

    @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
    @preconcurrency
    public func addHandlers(
        _ handlers: (ChannelHandler & Sendable)...,
        position: ChannelPipeline.Position = .last
    ) async throws {
        try await self.addHandlers(handlers, position: position)
    }
}

/// An error that is thrown when the number of bytes in an AsyncSequence exceeds the limit.
///
/// When collecting the bytes from an AsyncSequence, there is a limit up to where the content
/// exceeds a certain threshold beyond which the content isn't matching an expected reasonable
/// size to be processed. This error is generally thrown when it is discovered that there are more
/// more bytes in a sequence than what was specified as the maximum. It could be that this upTo
/// limit should be increased, or that the sequence has unexpected content in it.
public struct NIOTooManyBytesError: Error {
    /// Current limit on the maximum number of bytes in the sequence
    public var maxBytes: Int?

    @available(
        *,
        deprecated,
        message: "Construct the NIOTooManyBytesError with the maxBytes limit that triggered this error"
    )
    public init() {
        self.maxBytes = nil
    }

    public init(maxBytes: Int) {
        self.maxBytes = maxBytes
    }
}

extension NIOTooManyBytesError: Equatable {
    public static func == (lhs: NIOTooManyBytesError, rhs: NIOTooManyBytesError) -> Bool {
        // Equality of the maxBytes isn't of consequence
        true
    }
}

extension NIOTooManyBytesError: Hashable {
    public func hash(into hasher: inout Hasher) {
        // All errors of this type hash to the same value since maxBytes isn't of consequence
        hasher.combine(7)