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

Code before:
        return Int(bytesWritten)
    }

    /// Write `integer` into this `ByteBuffer` starting at `index`. This does not alter the writer index.
    ///
    /// - Parameters:
    ///   - integer: The integer to serialize.
    ///   - index: The index of the first byte to write.
    ///   - endianness: The endianness to use, defaults to big endian.
    ///   - as: the desired `FixedWidthInteger` type (optional parameter)
    /// - Returns: The number of bytes written.
    @discardableResult
    @inlinable
    public mutating func setInteger<T: FixedWidthInteger>(
        _ integer: T,
        at index: Int,
        endianness: Endianness = .big,
        as: T.Type = T.self
    ) -> Int {
        var value = _toEndianness(value: integer, endianness: endianness)
        return Swift.withUnsafeBytes(of: &value) { ptr in
            self.setBytes(ptr, at: index)
        }
    }

    /// Peeks at the integer at the current reader index without advancing it.
    ///
    /// This method is equivalent to calling `getInteger(at: readerIndex, ...)
    ///
    /// - Parameters:
    ///   - endianness: The endianness of the integer (defaults to big endian).
    ///   - as: The desired `FixedWidthInteger` type (optional parameter).
    /// - Returns: An integer value deserialized from this `ByteBuffer` or `nil` if the bytes are not readable.
    @inlinable
    public func peekInteger<T: FixedWidthInteger>(
        endianness: Endianness = .big,
        as: T.Type = T.self
    ) -> T? {
        return self.getInteger(at: self.readerIndex, endianness: endianness, as: `as`)
    }
}

extension FixedWidthInteger {

    /// Returns the next power of two.
    @inlinable
    func nextPowerOf2() -> Self {
        guard self != 0 else {
            return 1
        }
        return 1 << (Self.bitWidth - (self - 1).leadingZeroBitCount)

Review comment (suggestion):
```suggestion
    /// Returns the integer at the current reader index without advancing it.
```

Code after:
        return Int(bytesWritten)
    }

    /// Write `integer` into this `ByteBuffer` starting at `index`. This does not alter the writer index.
    ///
    /// - Parameters:
    ///   - integer: The integer to serialize.
    ///   - index: The index of the first byte to write.
    ///   - endianness: The endianness to use, defaults to big endian.
    ///   - as: the desired `FixedWidthInteger` type (optional parameter)
    /// - Returns: The number of bytes written.
    @discardableResult
    @inlinable
    public mutating func setInteger<T: FixedWidthInteger>(
        _ integer: T,
        at index: Int,
        endianness: Endianness = .big,
        as: T.Type = T.self
    ) -> Int {
        var value = _toEndianness(value: integer, endianness: endianness)
        return Swift.withUnsafeBytes(of: &value) { ptr in
            self.setBytes(ptr, at: index)
        }
    }

    /// Returns the integer at the current reader index without advancing it.
    ///
    /// This method is equivalent to calling `getInteger(at: readerIndex, ...)`
    ///
    /// - Parameters:
    ///   - endianness: The endianness of the integer (defaults to big endian).
    ///   - as: The desired `FixedWidthInteger` type (optional parameter).
    /// - Returns: An integer value deserialized from this `ByteBuffer` or `nil` if the bytes are not readable.
    @inlinable
    public func peekInteger<T: FixedWidthInteger>(
        endianness: Endianness = .big,
        as: T.Type = T.self
    ) -> T? {
        self.getInteger(at: self.readerIndex, endianness: endianness, as: `as`)
    }
}

extension FixedWidthInteger {

    /// Returns the next power of two.
    @inlinable
    func nextPowerOf2() -> Self {
        guard self != 0 else {
            return 1
        }
        return 1 << (Self.bitWidth - (self - 1).leadingZeroBitCount)