Rigid, Unique types: Rework OutputSpan-based append/prepend/insert/replaceSubrange primitives

This commit is contained in:
Karoy Lorentey committed 2026-02-04 17:29:43 -08:00
1 parent 9503f8fe57
commit 06f2ec073d
19 files changed
+887 -260

No files matched your search

@@ -36,6 +36,37 @@ extension UniqueArray where Element: ~Copyable {
@available(SwiftStdlib 5.0, *)
extension UniqueArray where Element: ~Copyable {
/// Append a given number of items to the end of this array by populating
/// an output span.
///
/// If the array does not have sufficient capacity to hold the requested
/// number of new elements, then this reallocates the array's storage to
/// grow its capacity, using a geometric growth rate.
///
/// If the callback fails to fully populate its output span or if
/// it throws an error, then the array keeps all items that were
/// successfully initialized before the callback terminated the insertion.
///
/// - Parameters:
/// - newItemCount: The number of items to append to the array.
/// - initializer: A callback that gets called at most once to directly
/// populate newly reserved storage within the array. The function
/// is allowed to initialize fewer than `uninitializedCount` items.
/// The array is appended however many items the callback adds to the
/// output span before it returns (or before it throws an error).
///
/// - Complexity: O(`uninitializedCount`)
@_alwaysEmitIntoClient
public mutating func append<E: Error>(
addingCount newItemCount: Int,
initializingWith initializer: (inout OutputSpan<Element>) throws(E) -> Void
) throws(E) {
_ensureFreeCapacity(newItemCount)
return try _storage.append(
addingCount: newItemCount,
initializingWith: initializer)
}
/// Append a given number of items to the end of this array by populating
/// an output span.
///
@@ -45,20 +76,25 @@ extension UniqueArray where Element: ~Copyable {
///
/// - Parameters
/// - count: The number of items to append to the array.
/// - body: A callback that gets called at most once to directly
/// - initializer: A callback that gets called exactly once to directly
/// populate newly reserved storage within the array. The function
/// is allowed to initialize fewer than `count` items. The array is
/// appended however many items the callback adds to the output span
/// before it returns (or before it throws an error).
///
/// - Complexity: O(`count`)
@available(*, deprecated, renamed: "append(addingCount:initializingWith:)")
@_alwaysEmitIntoClient
@inline(__always)
public mutating func append<E: Error, Result: ~Copyable>(
count: Int,
initializingWith body: (inout OutputSpan<Element>) throws(E) -> Result
initializingWith initializer: (inout OutputSpan<Element>) throws(E) -> Result
) throws(E) -> Result {
_ensureFreeCapacity(count)
return try _storage.append(count: count, initializingWith: body)
var result: Result? = nil
try append(addingCount: count) { target throws(E) in
result = try initializer(&target)
}
return result.take()!
}
}