Skip to content

Commit bbe8fbc

Browse files
committed
[stdlib] improve documentation of withContiguous[Mutable]StorageIfAvailable
- clarify the requirement that the entire collection must be accessible - clarify the requirement surrounding subsequences / slices - add parameter descriptions - specify that buffer cannot be replaced
1 parent f16eda4 commit bbe8fbc

File tree

2 files changed

+58
-28
lines changed

2 files changed

+58
-28
lines changed

stdlib/public/core/MutableCollection.swift

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -172,34 +172,53 @@ where SubSequence: MutableCollection
172172
///
173173
/// - Complexity: O(1)
174174
mutating func swapAt(_ i: Index, _ j: Index)
175-
176-
/// Call `body(p)`, where `p` is a pointer to the collection's
177-
/// mutable contiguous storage. If no such storage exists, it is
178-
/// first created. If the collection does not support an internal
179-
/// representation in a form of mutable contiguous storage, `body` is not
175+
176+
/// Call `body(buffer)`, where `buffer` provides access to the contiguous
177+
/// mutable storage of the entire collection. If no such storage exists, it is
178+
/// first created. If the collection does not support an internal
179+
/// representation in the form of contiguous mutable storage, `body` is not
180180
/// called and `nil` is returned.
181181
///
182-
/// Often, the optimizer can eliminate bounds- and uniqueness-checks
183-
/// within an algorithm, but when that fails, invoking the
184-
/// same algorithm on `body`\ 's argument lets you trade safety for
185-
/// speed.
182+
/// The optimizer can often eliminate bounds- and uniqueness-checking
183+
/// within an algorithm. When that fails, however, invoking the same
184+
/// algorithm on `body`\ 's argument may let you trade safety for speed.
185+
///
186+
/// A `Collection` that provides its own implementation of this method
187+
/// must provide contiguous storage to its elements in the same order
188+
/// as they appear in the collection. This guarantees that contiguous
189+
/// mutable storage to any of its subsequences can be generated by slicing
190+
/// `buffer` with a range formed from the distances to the subsequence's
191+
/// `startIndex` and `endIndex`, respectively.
186192
@available(*, deprecated, renamed: "withContiguousMutableStorageIfAvailable")
187193
mutating func _withUnsafeMutableBufferPointerIfSupported<R>(
188194
_ body: (inout UnsafeMutableBufferPointer<Element>) throws -> R
189195
) rethrows -> R?
190196

191-
/// Call `body(p)`, where `p` is a pointer to the collection's
192-
/// mutable contiguous storage. If no such storage exists, it is
193-
/// first created. If the collection does not support an internal
194-
/// representation in a form of mutable contiguous storage, `body` is not
197+
/// Call `body(buffer)`, where `buffer` provides access to the contiguous
198+
/// mutable storage of the entire collection. If no such storage exists, it is
199+
/// first created. If the collection does not support an internal
200+
/// representation in the form of contiguous mutable storage, `body` is not
195201
/// called and `nil` is returned.
196202
///
197-
/// Often, the optimizer can eliminate bounds- and uniqueness-checks
198-
/// within an algorithm, but when that fails, invoking the
199-
/// same algorithm on `body`\ 's argument lets you trade safety for
200-
/// speed.
203+
/// The optimizer can often eliminate bounds- and uniqueness-checking
204+
/// within an algorithm. When that fails, however, invoking the same
205+
/// algorithm on `body`\ 's argument may let you trade safety for speed.
206+
///
207+
/// A `Collection` that provides its own implementation of this method
208+
/// must provide contiguous storage to its elements in the same order
209+
/// as they appear in the collection. This guarantees that contiguous
210+
/// mutable storage to any of its subsequences can be generated by slicing
211+
/// `buffer` with a range formed from the distances to the subsequence's
212+
/// `startIndex` and `endIndex`, respectively.
213+
///
214+
/// - Note: `buffer` must not be replaced by `body`.
215+
///
216+
/// - Parameters:
217+
/// - body: a closure to be executed using the elements of this collection.
218+
/// - buffer: a buffer to the mutable contiguous storage of this collection.
219+
/// - Returns: the value returned by `body`, or `nil`.
201220
mutating func withContiguousMutableStorageIfAvailable<R>(
202-
_ body: (inout UnsafeMutableBufferPointer<Element>) throws -> R
221+
_ body: (_ buffer: inout UnsafeMutableBufferPointer<Element>) throws -> R
203222
) rethrows -> R?
204223
}
205224

stdlib/public/core/Sequence.swift

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -383,20 +383,31 @@ public protocol Sequence {
383383
__consuming func _copyContents(
384384
initializing ptr: UnsafeMutableBufferPointer<Element>
385385
) -> (Iterator,UnsafeMutableBufferPointer<Element>.Index)
386-
387-
/// Call `body(p)`, where `p` is a pointer to the collection's
388-
/// contiguous storage. If no such storage exists, it is
389-
/// first created. If the collection does not support an internal
390-
/// representation in a form of contiguous storage, `body` is not
386+
387+
/// Call `body(buffer)`, where `buffer` provides access to the contiguous
388+
/// storage of the entire collection. If no such storage exists, it is
389+
/// first created. If the collection does not support an internal
390+
/// representation in the form of contiguous storage, `body` is not
391391
/// called and `nil` is returned.
392392
///
393+
/// The optimizer can often eliminate bounds- and uniqueness-checking
394+
/// within an algorithm. When that fails, however, invoking the same
395+
/// algorithm on `body`\ 's argument may let you trade safety for speed.
396+
///
393397
/// A `Collection` that provides its own implementation of this method
394-
/// must also guarantee that an equivalent buffer of its `SubSequence`
395-
/// can be generated by advancing the pointer by the distance to the
396-
/// slice's `startIndex`.
398+
/// must provide contiguous storage to its elements in the same order
399+
/// as they appear in the collection. This guarantees that contiguous
400+
/// storage to any of its subsequences can be generated by slicing
401+
/// `buffer` with a range formed from the distances to the subsequence's
402+
/// `startIndex` and `endIndex`, respectively.
403+
///
404+
/// - Parameters:
405+
/// - body: a closure to be executed using the elements of this collection.
406+
/// - buffer: a buffer to the contiguous storage of this collection.
407+
/// - Returns: the value returned by `body`, or `nil`.
397408
func withContiguousStorageIfAvailable<R>(
398-
_ body: (UnsafeBufferPointer<Element>) throws -> R
399-
) rethrows -> R?
409+
_ body: (_ buffer: UnsafeBufferPointer<Element>) throws -> R
410+
) rethrows -> R?
400411
}
401412

402413
// Provides a default associated type witness for Iterator when the

0 commit comments

Comments
 (0)