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

Code before:
        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
        }

        do {
            let inlineArray = try InlineArray<count, IntegerType> { (outputSpan: inout OutputSpan<IntegerType>) in
                for index in 0..<count {
                    guard
                        let integer = self.getInteger(
                            // this is less than 'bytesRequired' so is safe to multiply
                            at: stride &* index,
                            endianness: endianness,
                            as: IntegerType.self
                        )
                    else {
                        throw InlineArrayFailedToGetElementError()
                    }
                    outputSpan.append(integer)
                }
            }
            // already made sure of 'self.readableBytes >= bytesRequired' above
            self._moveReaderIndex(forwardBy: bytesRequired)
            return inlineArray
        } catch {
            // only 'InlineArrayFailedToGetElementError' could have been thrown
            return nil
        }
    }
    #endif

    /// Returns the Bytes at the current reader index without advancing it.
    ///
    /// This method is equivalent to calling `getBytes(at: readerIndex, ...)`
    ///
    /// - Parameters:
    ///   - length: The number of bytes of interest.
    /// - Returns: A `[UInt8]` value containing the bytes of interest or `nil` if the bytes `ByteBuffer` are not readable.
    @inlinable
    public func peekBytes(length: Int) -> [UInt8]? {
        self.getBytes(at: self.readerIndex, length: length)
    }

Review comment (suggestion):
This error is unreachable: if `readableBytes >= bytesRequired`, then there can be no way for this to throw, so we can force unwrap the return value of `getInteger`. Please do leave a comment in the code that reflects this fact.

Code after:
        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,
                    as: IntegerType.self
                )!
                outputSpan.append(integer)
            }
        }
        // already made sure of 'self.readableBytes >= bytesRequired' above
        self._moveReaderIndex(forwardBy: bytesRequired)
        return inlineArray
    }
    #endif

    /// Returns the Bytes at the current reader index without advancing it.
    ///
    /// This method is equivalent to calling `getBytes(at: readerIndex, ...)`
    ///
    /// - Parameters:
    ///   - length: The number of bytes of interest.
    /// - Returns: A `[UInt8]` value containing the bytes of interest or `nil` if the bytes `ByteBuffer` are not readable.
    @inlinable
    public func peekBytes(length: Int) -> [UInt8]? {
        self.getBytes(at: self.readerIndex, length: length)
    }

    // MARK: StaticString APIs

    /// Write the static `string` into this `ByteBuffer` using UTF-8 encoding, moving the writer index forward appropriately.
    ///
    /// - Parameters:
    ///   - string: The string to write.