@@ -1642,6 +1642,42 @@ extension Array {
1642
1642
return try body ( & inoutBufferPointer)
1643
1643
}
1644
1644
1645
+ /// Calls the given closure with a pointer to the array's mutable contiguous
1646
+ /// storage.
1647
+ ///
1648
+ /// Often, the optimizer can eliminate bounds checks within an array
1649
+ /// algorithm, but when that fails, invoking the same algorithm on the
1650
+ /// buffer pointer passed into your closure lets you trade safety for speed.
1651
+ ///
1652
+ /// The following example shows how modifying the contents of the
1653
+ /// `UnsafeMutableBufferPointer` argument to `body` alters the contents of
1654
+ /// the array:
1655
+ ///
1656
+ /// var numbers = [1, 2, 3, 4, 5]
1657
+ /// numbers.withUnsafeMutableBufferPointer { buffer in
1658
+ /// for i in stride(from: buffer.startIndex, to: buffer.endIndex - 1, by: 2) {
1659
+ /// buffer.swapAt(i, i + 1)
1660
+ /// }
1661
+ /// }
1662
+ /// print(numbers)
1663
+ /// // Prints "[2, 1, 4, 3, 5]"
1664
+ ///
1665
+ /// The pointer passed as an argument to `body` is valid only during the
1666
+ /// execution of `withUnsafeMutableBufferPointer(_:)`. Do not store or
1667
+ /// return the pointer for later use.
1668
+ ///
1669
+ /// - Warning: Do not rely on anything about the array that is the target of
1670
+ /// this method during execution of the `body` closure; it might not
1671
+ /// appear to have its correct value. Instead, use only the
1672
+ /// `UnsafeMutableBufferPointer` argument to `body`.
1673
+ ///
1674
+ /// - Parameter body: A closure with an `UnsafeMutableBufferPointer`
1675
+ /// parameter that points to the contiguous storage for the array.
1676
+ /// If no such storage exists, it is created. If `body` has a return value, that value is also
1677
+ /// used as the return value for the `withUnsafeMutableBufferPointer(_:)`
1678
+ /// method. The pointer argument is valid only for the duration of the
1679
+ /// method's execution.
1680
+ /// - Returns: The return value, if any, of the `body` closure parameter.
1645
1681
@_semantics ( " array.withUnsafeMutableBufferPointer " )
1646
1682
@_effects ( notEscaping self. value**)
1647
1683
@_alwaysEmitIntoClient
0 commit comments