Skip to content

Commit d954e46

Browse files
authored
Merge pull request #40027 from glessard/wCSIA-clarification
2 parents f9aed0c + 0d1d49d commit d954e46

File tree

5 files changed

+62
-30
lines changed

5 files changed

+62
-30
lines changed

stdlib/private/StdlibCollectionUnittest/LoggingWrappers.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ extension LoggingMutableCollection: MutableCollection {
387387
return try base.partition(by: belongsInSecondPartition)
388388
}
389389

390+
@available(*, deprecated, renamed: "withContiguousMutableStorageIfAvailable")
390391
public mutating func _withUnsafeMutableBufferPointerIfSupported<R>(
391392
_ body: (inout UnsafeMutableBufferPointer<Element>) throws -> R
392393
) rethrows -> R? {
@@ -601,6 +602,7 @@ extension BufferAccessLoggingMutableCollection: MutableCollection {
601602
return base.distance(from: start, to: end)
602603
}
603604

605+
@available(*, deprecated, renamed: "withContiguousMutableStorageIfAvailable")
604606
public mutating func _withUnsafeMutableBufferPointerIfSupported<R>(
605607
_ body: (inout UnsafeMutableBufferPointer<Element>) throws -> R
606608
) rethrows -> R? {

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

stdlib/public/core/Slice.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ extension Slice: MutableCollection where Base: MutableCollection {
309309
_precondition(
310310
slice.baseAddress == copy.baseAddress &&
311311
slice.count == copy.count,
312-
"Slice.withUnsafeMutableBufferPointer: replacing the buffer is not allowed")
312+
"Slice.withContiguousMutableStorageIfAvailable: replacing the buffer is not allowed")
313313
}
314314
return try body(&slice)
315315
}

stdlib/public/core/UnsafeBufferPointer.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ extension Unsafe${Mutable}BufferPointer {
472472
let (oldBase, oldCount) = (self.baseAddress, self.count)
473473
defer {
474474
_debugPrecondition((oldBase, oldCount) == (self.baseAddress, self.count),
475-
"UnsafeMutableBufferPointer.withUnsafeMutableBufferPointer: replacing the buffer is not allowed")
475+
"UnsafeMutableBufferPointer.withContiguousMutableStorageIfAvailable: replacing the buffer is not allowed")
476476
}
477477
return try body(&self)
478478
}

0 commit comments

Comments
 (0)