Deprecate and replace `ByteBuffer.clear(minimumCapacity: _Capacity)` (#1610)

* Add new method

* Fix precondition message

* George PR comments
This commit is contained in:
David Evans 2020-08-04 12:04:06 +01:00 committed by GitHub
parent 3141b02d2f
commit 08706eb13e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 1 deletions

View File

@ -708,7 +708,25 @@ public struct ByteBuffer {
///
/// - parameters:
/// - minimumCapacity: The minimum capacity that will be (re)allocated for this buffer
public mutating func clear(minimumCapacity: _Capacity) {
@available(*, deprecated, message: "Use an `Int` as the argument")
public mutating func clear(minimumCapacity: UInt32) {
self.clear(minimumCapacity: Int(minimumCapacity))
}
/// 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.
///
/// - parameters:
/// - minimumCapacity: The minimum capacity that will be (re)allocated for this buffer
public mutating func clear(minimumCapacity: Int) {
precondition(minimumCapacity >= 0, "Cannot have a minimum capacity < 0")
precondition(minimumCapacity <= _Capacity.max, "Minimum capacity must be <= \(_Capacity.max)")
let minimumCapacity = _Capacity(minimumCapacity)
if !isKnownUniquelyReferenced(&self._storage) {
self._storage = self._storage.allocateStorage(capacity: minimumCapacity)
} else if minimumCapacity > self._storage.capacity {