@@ -194,29 +194,46 @@ where SubSequence: MutableCollection
194
194
_ body: ( inout UnsafeMutableBufferPointer < Element > ) throws -> R
195
195
) rethrows -> R ?
196
196
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
201
- /// called and `nil` is returned.
197
+ /// Executes a closure on the collection's contiguous storage.
198
+ ///
199
+ /// This method calls `body(buffer)`, where `buffer` provides access to the
200
+ /// contiguous mutable storage of the entire collection. If the contiguous
201
+ /// storage doesn't exist, the collection creates it. If the collection
202
+ /// doesn't support an internal representation in the form of contiguous
203
+ /// mutable storage, this method doesn't call `body` --- it immediately
204
+ /// returns `nil`.
202
205
///
203
206
/// The optimizer can often eliminate bounds- and uniqueness-checking
204
207
/// within an algorithm. When that fails, however, invoking the same
205
- /// algorithm on `body`\ 's argument may let you trade safety for speed.
208
+ /// algorithm on the `buffer` argument may let you trade safety for speed.
209
+ ///
210
+ /// Always perform any necessary cleanup in the closure, because the
211
+ /// method makes no guarantees about the state of the collection if the
212
+ /// closure throws an error. Your changes to the collection may be absent
213
+ /// from the collection after throwing the error, because the closure could
214
+ /// receive a temporary copy rather than direct access to the collection's
215
+ /// storage.
216
+ ///
217
+ /// - Warning: Your `body` closure must not replace `buffer`. This leads
218
+ /// to a crash in all implementations of this method within the standard
219
+ /// library.
220
+ ///
221
+ /// Successive calls to this method may provide a different pointer on each
222
+ /// call. Don't store `buffer` outside of this method.
206
223
///
207
224
/// A `Collection` that provides its own implementation of this method
208
225
/// 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
226
+ /// as they appear in the collection. This guarantees that it's possible to
227
+ /// generate contiguous mutable storage to any of its subsequences by slicing
211
228
/// `buffer` with a range formed from the distances to the subsequence's
212
229
/// `startIndex` and `endIndex`, respectively.
213
230
///
214
- /// - Note: `buffer` must not be replaced by `body`.
215
- ///
216
231
/// - 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`.
232
+ /// - body: A closure that receives an in-out
233
+ /// `UnsafeMutableBufferPointer` to the collection's contiguous storage.
234
+ /// - Returns: The value returned from `body`, unless the collection doesn't
235
+ /// support contiguous storage, in which case the method ignores `body` and
236
+ /// returns `nil`.
220
237
mutating func withContiguousMutableStorageIfAvailable< R> (
221
238
_ body: ( _ buffer: inout UnsafeMutableBufferPointer < Element > ) throws -> R
222
239
) rethrows -> R ?
0 commit comments