@@ -1565,6 +1565,16 @@ extension Array {
1565
1565
initializingWith: initializer)
1566
1566
}
1567
1567
1568
+ // Superseded by the typed-throws version of this function, but retained
1569
+ // for ABI reasons.
1570
+ @usableFromInline
1571
+ @_disfavoredOverload
1572
+ func withUnsafeBufferPointer< R> (
1573
+ _ body: ( UnsafeBufferPointer < Element > ) throws -> R
1574
+ ) rethrows -> R {
1575
+ return try _buffer. withUnsafeBufferPointer ( body)
1576
+ }
1577
+
1568
1578
/// Calls a closure with a pointer to the array's contiguous storage.
1569
1579
///
1570
1580
/// Often, the optimizer can eliminate bounds checks within an array
@@ -1594,13 +1604,44 @@ extension Array {
1594
1604
/// for the `withUnsafeBufferPointer(_:)` method. The pointer argument is
1595
1605
/// valid only for the duration of the method's execution.
1596
1606
/// - Returns: The return value, if any, of the `body` closure parameter.
1597
- @inlinable
1598
- public func withUnsafeBufferPointer< R> (
1599
- _ body: ( UnsafeBufferPointer < Element > ) throws -> R
1600
- ) rethrows -> R {
1607
+ @_alwaysEmitIntoClient
1608
+ public func withUnsafeBufferPointer< R, E > (
1609
+ _ body: ( UnsafeBufferPointer < Element > ) throws ( E ) -> R
1610
+ ) throws ( E ) -> R {
1601
1611
return try _buffer. withUnsafeBufferPointer ( body)
1602
1612
}
1603
1613
1614
+ // Superseded by the typed-throws version of this function, but retained
1615
+ // for ABI reasons.
1616
+ @_semantics ( " array.withUnsafeMutableBufferPointer " )
1617
+ @_effects ( notEscaping self. value**)
1618
+ @usableFromInline
1619
+ @inline ( __always)
1620
+ @_silgen_name ( " $sSa30withUnsafeMutableBufferPointeryqd__qd__SryxGzKXEKlF " )
1621
+ mutating func __abi_withUnsafeMutableBufferPointer< R> (
1622
+ _ body: ( inout UnsafeMutableBufferPointer < Element > ) throws -> R
1623
+ ) rethrows -> R {
1624
+ _makeMutableAndUnique ( )
1625
+ let count = _buffer. mutableCount
1626
+
1627
+ // Create an UnsafeBufferPointer that we can pass to body
1628
+ let pointer = _buffer. mutableFirstElementAddress
1629
+ var inoutBufferPointer = UnsafeMutableBufferPointer (
1630
+ start: pointer, count: count)
1631
+
1632
+ defer {
1633
+ _precondition (
1634
+ inoutBufferPointer. baseAddress == pointer &&
1635
+ inoutBufferPointer. count == count,
1636
+ " Array withUnsafeMutableBufferPointer: replacing the buffer is not allowed " )
1637
+ _endMutation ( )
1638
+ _fixLifetime ( self )
1639
+ }
1640
+
1641
+ // Invoke the body.
1642
+ return try body ( & inoutBufferPointer)
1643
+ }
1644
+
1604
1645
/// Calls the given closure with a pointer to the array's mutable contiguous
1605
1646
/// storage.
1606
1647
///
@@ -1639,14 +1680,14 @@ extension Array {
1639
1680
/// - Returns: The return value, if any, of the `body` closure parameter.
1640
1681
@_semantics ( " array.withUnsafeMutableBufferPointer " )
1641
1682
@_effects ( notEscaping self. value**)
1642
- @inlinable // FIXME(inline-always)
1683
+ @_alwaysEmitIntoClient
1643
1684
@inline ( __always) // Performance: This method should get inlined into the
1644
1685
// caller such that we can combine the partial apply with the apply in this
1645
1686
// function saving on allocating a closure context. This becomes unnecessary
1646
1687
// once we allocate noescape closures on the stack.
1647
- public mutating func withUnsafeMutableBufferPointer< R> (
1648
- _ body: ( inout UnsafeMutableBufferPointer < Element > ) throws -> R
1649
- ) rethrows -> R {
1688
+ public mutating func withUnsafeMutableBufferPointer< R, E > (
1689
+ _ body: ( inout UnsafeMutableBufferPointer < Element > ) throws ( E ) -> R
1690
+ ) throws ( E ) -> R {
1650
1691
_makeMutableAndUnique ( )
1651
1692
let count = _buffer. mutableCount
1652
1693
0 commit comments