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

Code before:
    @preconcurrency
    public func addHandlers(
        _ handlers: [ChannelHandler & Sendable],
        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, Hashable {
    /// 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
    }
}

@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

Review comment (suggestion):
Can you add a custom `Equatable` and `Hashable` implementation that ignores `maxBytes`? `==` can just return true and `hash(into:)` can just hash a constant e.g. `0`. Users are usually not interested in the specific `maxBytes` value but rather just that it is a `NIOTooManyBytesError` error.

Code after:
    @preconcurrency
    public func addHandlers(
        _ handlers: [ChannelHandler & Sendable],
        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 {