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

Code before:
            self._copyStorageAndRebase(extraCapacity: 0, resetIndices: true)
        }
        return true
    }

    /// The `ByteBuffer` will successfully be shrunk if the requested capacity is less than the current capacity,
    /// and the requested capacity is more than the number of readable bytes in the buffer.
    /// If either condition is not true, the buffer will not be shrunk.
    ///
    /// - Parameter desiredCapacity: The desired capacity for the buffers capacity to be shrunken to
    /// - Returns: Bool indicating whether the buffer capacity has been shrunk to the desiredCapacity.
    @inlinable
    @discardableResult public mutating func shrinkBufferCapacity(to desiredCapacity: Int) -> Bool {
        let desiredCapacity = ByteBuffer.addPaddingTo(desiredCapacity)
        guard desiredCapacity < self.capacity, desiredCapacity > self.readableBytes else {
            return false
        }
        self._copyStorageAndRebase(capacity: _toCapacity(desiredCapacity), resetIndices: true)
        return true
    }

    /// Returns size of capacity with optimal padding
    /// - Parameter initialCapacity: Capacity that needs expansion with padding
    /// - Returns: Capacity with calculated padding
    @inlinable
    static func addPaddingTo(_ initialCapacity: Int) -> Int {
        initialCapacity == 0 ? 0 : initialCapacity.nextPowerOf2()
    }

    /// The reader index or the number of bytes previously read from this `ByteBuffer`. `readerIndex` is `0` for a
    /// newly allocated `ByteBuffer`.
    @inlinable
    public var readerIndex: Int {
        Int(self._readerIndex)
    }

    /// The write index or the number of bytes previously written to this `ByteBuffer`. `writerIndex` is `0` for a
    /// newly allocated `ByteBuffer`.
    @inlinable
    public var writerIndex: Int {
        Int(self._writerIndex)
    }

    /// Set both reader index and writer index to `0`. This will reset the state of this `ByteBuffer` to the state
    /// of a freshly allocated one, if possible without allocations. This is the cheapest way to recycle a `ByteBuffer`
    /// for a new use-case.
    ///
    /// - note: This method will allocate if the underlying storage is referenced by another `ByteBuffer`. Even if an
    ///         allocation is necessary this will be cheaper as the copy of the storage is elided.
    @inlinable
    public mutating func clear() {

Review comment (suggestion):
```suggestion
    static func roundUpToUsableCapacity(_ initialCapacity: Int) -> Int {
```

I think we should be more specific here. You can also use this function elsewhere.

Code after:
            self._moveWriterIndex(to: self._writerIndex - indexShift)
        } else {
            self._copyStorageAndRebase(extraCapacity: 0, resetIndices: true)
        }
        return true
    }

    /// The `ByteBuffer` will successfully be shrunk if the requested capacity is less than the current capacity,
    /// and the requested capacity is greater than or equal to the number of readable bytes in the buffer.
    /// If either condition is not true, the buffer will not be shrunk.
    ///
    /// - Parameter desiredCapacity: The desired capacity for the buffers capacity to be shrunken to
    /// - Returns: Bool indicating whether the buffer capacity has been shrunk to the desiredCapacity.
    @inlinable
    @discardableResult public mutating func shrinkBufferCapacity(to desiredCapacity: Int) -> Bool {
        let desiredCapacity = ByteBuffer.roundUpToUsableCapacity(_toCapacity(desiredCapacity))
        guard desiredCapacity < self.capacity, desiredCapacity >= self.readableBytes else {
            return false
        }
        self._copyStorageAndRebase(capacity: desiredCapacity, resetIndices: true)
        return true
    }

    /// Returns size of capacity with optimal padding
    /// - Parameter initialCapacity: Capacity that needs expansion with padding
    /// - Returns: Capacity with calculated padding
    @inlinable
    static func roundUpToUsableCapacity(_ initialCapacity: UInt32) -> UInt32 {
        initialCapacity == 0 ? 0 : initialCapacity.nextPowerOf2ClampedToMax()
    }

    /// The reader index or the number of bytes previously read from this `ByteBuffer`. `readerIndex` is `0` for a
    /// newly allocated `ByteBuffer`.
    @inlinable
    public var readerIndex: Int {
        Int(self._readerIndex)
    }

    /// The write index or the number of bytes previously written to this `ByteBuffer`. `writerIndex` is `0` for a
    /// newly allocated `ByteBuffer`.
    @inlinable
    public var writerIndex: Int {
        Int(self._writerIndex)
    }

    /// Set both reader index and writer index to `0`. This will reset the state of this `ByteBuffer` to the state
    /// of a freshly allocated one, if possible without allocations. This is the cheapest way to recycle a `ByteBuffer`
    /// for a new use-case.
    ///
    /// - note: This method will allocate if the underlying storage is referenced by another `ByteBuffer`. Even if an
    ///         allocation is necessary this will be cheaper as the copy of the storage is elided.