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

Code before:
        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? {
        guard let result = self.getInteger(at: self.readerIndex, endianness: endianness, as: T.self) else {
            return nil
        }
        return result
    }
}

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)
    }

    /// Returns the previous power of 2, or self if it already is.
    @inlinable
    func previousPowerOf2() -> Self {
        guard self != 0 else {
            return 0
        }

        return 1 << ((Self.bitWidth - 1) - self.leadingZeroBitCount)
    }

    /// Initialize an integer from a byte buffer of exactly the right size.

Review comment (suggestion):
We don't need the `guard` here. The formatter was complaining because of the explicit return where it wasn't necessary:

```suggestion
        self.getInteger(at: self.readerIndex, endianness: endianness, as: T.self)
```

Code after:
        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)
    }

    /// Returns the previous power of 2, or self if it already is.
    @inlinable
    func previousPowerOf2() -> Self {
        guard self != 0 else {
            return 0
        }

        return 1 << ((Self.bitWidth - 1) - self.leadingZeroBitCount)
    }

    /// Initialize an integer from a byte buffer of exactly the right size.
    /// The bytes will be read using the host system's endianness.
    ///
    /// - Parameters: