From b768d2239348e68fd05406d1e8bbf8048681ae76 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Wed, 4 Feb 2026 10:09:00 -0800 Subject: [PATCH] =?UTF-8?q?[BasicContainers]=20RigidArray,=20UniqueArray:?= =?UTF-8?q?=20=E2=80=9Cprecisely=20once=E2=80=9D=20=E2=86=92=20=E2=80=9Cat?= =?UTF-8?q?=20most=20once=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Loosen guarantees about how many times closure arguments are getting called, so that e.g. insertions of zero items do not necessarily need to call their callback with an empty OutputSpan. (For these array types we can reinstate the guarantee later, if it turns out to be important/useful. However, the generalized primitives will need to allow their callbacks to get called an arbitrary number of times, and it seems useful to establish that (at least partially) with these simple array types too.) --- .../BasicContainers/RigidArray/RigidArray+Append.swift | 8 ++++++-- .../RigidArray/RigidArray+Initializers.swift | 2 +- .../RigidArray/RigidArray+Insertions.swift | 2 +- .../RigidArray/RigidArray+Replacements.swift | 2 +- Sources/BasicContainers/RigidArray/RigidArray.swift | 4 ++-- .../BasicContainers/UniqueArray/UniqueArray+Append.swift | 2 +- .../UniqueArray/UniqueArray+Initializers.swift | 2 +- .../UniqueArray/UniqueArray+Insertions.swift | 2 +- .../UniqueArray/UniqueArray+Replacements.swift | 2 +- Sources/BasicContainers/UniqueArray/UniqueArray.swift | 4 ++-- .../DequeModule/RigidDeque/RigidDeque+Insertions.swift | 2 +- 11 files changed, 18 insertions(+), 14 deletions(-) diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Append.swift b/Sources/BasicContainers/RigidArray/RigidArray+Append.swift index 57e64641..96608c54 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+Append.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+Append.swift @@ -62,7 +62,7 @@ extension RigidArray where Element: ~Copyable { /// /// - Parameters /// - uninitializedCount: The number of items to append to the array. - /// - body: A callback that gets called precisely once to directly + /// - body: 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 @@ -91,9 +91,13 @@ extension RigidArray where Element: ~Copyable { /// If the array does not have sufficient capacity to store the new items in /// the buffer, then this triggers a runtime error. /// + /// If the callback fails to fully populate its output span or if + /// it throws an error, then the items that were successfully initialized + /// still get appended to the end of the array. + /// /// - Parameters /// - count: The number of items to append to the array. - /// - body: A callback that gets called precisely once to directly + /// - body: 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 `count` items. The array is /// appended however many items the callback adds to the output span diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Initializers.swift b/Sources/BasicContainers/RigidArray/RigidArray+Initializers.swift index 1cb76faa..4f001442 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+Initializers.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+Initializers.swift @@ -47,7 +47,7 @@ extension RigidArray where Element: ~Copyable { /// /// - Parameters: /// - capacity: The storage capacity of the new array. - /// - body: A callback that gets called precisely once to directly + /// - body: A callback that gets called at most once to directly /// populate newly reserved storage within the array. The function /// is allowed to add fewer than `capacity` items. The array is /// initialized with however many items the callback adds to the diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Insertions.swift b/Sources/BasicContainers/RigidArray/RigidArray+Insertions.swift index aa83b767..47018162 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+Insertions.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+Insertions.swift @@ -66,7 +66,7 @@ extension RigidArray where Element: ~Copyable { /// - count: The number of items to insert into the array. /// - index: The position at which to insert the new items. /// `index` must be a valid index in the array. - /// - body: A callback that gets called precisely once to directly + /// - body: A callback that gets called at most once to directly /// populate newly reserved storage within the array. The function /// is called with an empty output span of capacity matching the /// supplied count, and it must fully populate it before returning. diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Replacements.swift b/Sources/BasicContainers/RigidArray/RigidArray+Replacements.swift index 9c32a1b6..19a0744a 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+Replacements.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+Replacements.swift @@ -42,7 +42,7 @@ extension RigidArray where Element: ~Copyable { /// - subrange: The subrange of the array to replace. The bounds of /// the range must be valid indices in the array. /// - newCount: the number of items to replace the old subrange. - /// - body: A callback that gets called precisely once to directly + /// - body: A callback that gets called at most once to directly /// populate newly reserved storage within the array. The function /// is called with an empty output span of capacity `newCount`, /// and it must fully populate it before returning. diff --git a/Sources/BasicContainers/RigidArray/RigidArray.swift b/Sources/BasicContainers/RigidArray/RigidArray.swift index 2c5d2b67..4f13a871 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray.swift @@ -196,7 +196,7 @@ extension RigidArray where Element: ~Copyable { extension RigidArray where Element: ~Copyable { /// Arbitrarily edit the storage underlying this array by invoking a /// user-supplied closure with a mutable `OutputSpan` view over it. - /// This method calls its function argument precisely once, allowing it to + /// This method calls its function argument at most once, allowing it to /// arbitrarily modify the contents of the output span it is given. /// The argument is free to add, remove or reorder any items; however, /// it is not allowed to replace the span or change its capacity. @@ -207,7 +207,7 @@ extension RigidArray where Element: ~Copyable { /// /// - Parameter body: A function that edits the contents of this array through /// an `OutputSpan` argument. This method invokes this function - /// precisely once. + /// at most once. /// - Returns: This method returns the result of its function argument. /// - Complexity: Adds O(1) overhead to the complexity of the function /// argument. diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray+Append.swift b/Sources/BasicContainers/UniqueArray/UniqueArray+Append.swift index 6843b9ab..999c7384 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray+Append.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray+Append.swift @@ -45,7 +45,7 @@ extension UniqueArray where Element: ~Copyable { /// /// - Parameters /// - count: The number of items to append to the array. - /// - body: A callback that gets called precisely once to directly + /// - body: 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 `count` items. The array is /// appended however many items the callback adds to the output span diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray+Initializers.swift b/Sources/BasicContainers/UniqueArray/UniqueArray+Initializers.swift index 106790fc..fd18c8d1 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray+Initializers.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray+Initializers.swift @@ -38,7 +38,7 @@ extension UniqueArray where Element: ~Copyable { /// /// - Parameters: /// - capacity: The storage capacity of the new array. - /// - body: A callback that gets called precisely once to directly + /// - body: A callback that gets called at most once to directly /// populate newly reserved storage within the array. The function /// is allowed to add fewer than `capacity` items. The array is /// initialized with however many items the callback adds to the diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray+Insertions.swift b/Sources/BasicContainers/UniqueArray/UniqueArray+Insertions.swift index 8180b00c..c5cd3475 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray+Insertions.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray+Insertions.swift @@ -62,7 +62,7 @@ extension UniqueArray where Element: ~Copyable { /// - count: The number of items to insert into the array. /// - index: The position at which to insert the new items. /// `index` must be a valid index in the array. - /// - body: A callback that gets called precisely once to directly + /// - body: A callback that gets called at most once to directly /// populate newly reserved storage within the array. The function /// is called with an empty output span of capacity matching the /// supplied count, and it must fully populate it before returning. diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray+Replacements.swift b/Sources/BasicContainers/UniqueArray/UniqueArray+Replacements.swift index f2b85f0e..3eac8b2b 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray+Replacements.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray+Replacements.swift @@ -43,7 +43,7 @@ extension UniqueArray where Element: ~Copyable { /// - subrange: The subrange of the array to replace. The bounds of /// the range must be valid indices in the array. /// - newCount: the number of items to replace the old subrange. - /// - body: A callback that gets called precisely once to directly + /// - body: A callback that gets called at most once to directly /// populate newly reserved storage within the array. The function /// is called with an empty output span of capacity `newCount`, /// and it must fully populate it before returning. diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray.swift b/Sources/BasicContainers/UniqueArray/UniqueArray.swift index 081e07c3..3f99cc7b 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray.swift @@ -128,7 +128,7 @@ extension UniqueArray where Element: ~Copyable { extension UniqueArray where Element: ~Copyable { /// Arbitrarily edit the storage underlying this array by invoking a /// user-supplied closure with a mutable `OutputSpan` view over it. - /// This method calls its function argument precisely once, allowing it to + /// This method calls its function argument at most once, allowing it to /// arbitrarily modify the contents of the output span it is given. /// The argument is free to add, remove or reorder any items; however, /// it is not allowed to replace the span or change its capacity. @@ -139,7 +139,7 @@ extension UniqueArray where Element: ~Copyable { /// /// - Parameter body: A function that edits the contents of this array through /// an `OutputSpan` argument. This method invokes this function - /// precisely once. + /// at most once. /// - Returns: This method returns the result of its function argument. /// - Complexity: Adds O(1) overhead to the complexity of the function /// argument. diff --git a/Sources/DequeModule/RigidDeque/RigidDeque+Insertions.swift b/Sources/DequeModule/RigidDeque/RigidDeque+Insertions.swift index 3d37b48e..e569b630 100644 --- a/Sources/DequeModule/RigidDeque/RigidDeque+Insertions.swift +++ b/Sources/DequeModule/RigidDeque/RigidDeque+Insertions.swift @@ -74,7 +74,7 @@ extension RigidDeque where Element: ~Copyable { /// times to initialize each successive chunk of storage. However, invocations /// cease if the callback fails to fully populate its output span or if /// it throws an error. In such cases, the deque keeps all items that were - /// successfully initialized before the callback terminated the prepend. + /// successfully initialized before the callback terminated the insertion. /// /// Partial insertions create a gap in ring buffer storage that needs to be /// closed by moving already inserted items to their correct positions given