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

Code before:
        }

        return self.withUnsafeReadableBytes { ptr in
            // this is not technically correct because we shouldn't just bind
            // the memory to `UInt8` but it's not a real issue either and we
            // need to work around https://bugs.swift.org/browse/SR-9604
            [UInt8](UnsafeRawBufferPointer(rebasing: ptr[range]).bindMemory(to: UInt8.self))
        }
    }

    /// Read `length` bytes off this `ByteBuffer`, move the reader index forward by `length` bytes and return the result
    /// as `[UInt8]`.
    ///
    /// - Parameters:
    ///   - length: The number of bytes to be read from this `ByteBuffer`.
    /// - Returns: A `[UInt8]` value containing `length` bytes or `nil` if there aren't at least `length` bytes readable.
    @inlinable
    public mutating func readBytes(length: Int) -> [UInt8]? {
        guard let result = self.getBytes(at: self.readerIndex, length: length) else {
            return nil
        }
        self._moveReaderIndex(forwardBy: length)
        return result
    }

    #if compiler(>=6.2) && !canImport(Darwin)
    @inlinable
    public mutating func readInlineArray<
        let count: Int,
        IntegerType: FixedWidthInteger
    >(
        endianness: Endianness = .big,
        as: InlineArray<count, IntegerType>.Type = InlineArray<count, IntegerType>.self
    ) -> InlineArray<count, IntegerType>? {
        let length = MemoryLayout<IntegerType>.size
        let bytesRequired = length &* count

        guard self.readableBytes >= bytesRequired else {
            return nil
        }

        return self.readWithUnsafeReadableBytes {
            ptr -> (Int, InlineArray<count, IntegerType>) in
            assert(ptr.count >= bytesRequired)
            let values: InlineArray<count, IntegerType> = InlineArray { index in
                switch endianness {
                case .big:
                    return IntegerType(
                        bigEndian: ptr.load(
                            fromByteOffset: index &* length,
                            as: IntegerType.self

Review comment (suggestion):
I don't think we need a `canImport` guard here: the `compiler>=6.2` is sufficient. It does, however, need an availability guard for the presumed next OS releases.

Code after:
        }

        return self.withUnsafeReadableBytes { ptr in
            // this is not technically correct because we shouldn't just bind
            // the memory to `UInt8` but it's not a real issue either and we
            // need to work around https://bugs.swift.org/browse/SR-9604
            [UInt8](UnsafeRawBufferPointer(rebasing: ptr[range]).bindMemory(to: UInt8.self))
        }
    }

    /// Read `length` bytes off this `ByteBuffer`, move the reader index forward by `length` bytes and return the result
    /// as `[UInt8]`.
    ///
    /// - Parameters:
    ///   - length: The number of bytes to be read from this `ByteBuffer`.
    /// - Returns: A `[UInt8]` value containing `length` bytes or `nil` if there aren't at least `length` bytes readable.
    @inlinable
    public mutating func readBytes(length: Int) -> [UInt8]? {
        guard let result = self.getBytes(at: self.readerIndex, length: length) else {
            return nil
        }
        self._moveReaderIndex(forwardBy: length)
        return result
    }

    #if compiler(>=6.2)
    @inlinable
    @available(macOS 26, iOS 26, tvOS 26, watchOS 26, visionOS 26, *)
    public mutating func readInlineArray<
        let count: Int,
        IntegerType: FixedWidthInteger
    >(
        endianness: Endianness = .big,
        as: InlineArray<count, IntegerType>.Type = InlineArray<count, IntegerType>.self
    ) -> InlineArray<count, IntegerType>? {
        // use stride to account for padding bytes
        let stride = MemoryLayout<IntegerType>.stride
        let bytesRequired = stride * count

        guard self.readableBytes >= bytesRequired else {
            return nil
        }

        let inlineArray = InlineArray<count, IntegerType> { (outputSpan: inout OutputSpan<IntegerType>) in
            for index in 0..<count {
                // already made sure of 'self.readableBytes >= bytesRequired' above,
                // so this is safe to force-unwrap as it's guaranteed to exist
                let integer = self.getInteger(
                    // this is less than 'bytesRequired' so is safe to multiply
                    at: stride &* index,
                    endianness: endianness,