@@ -1366,21 +1366,35 @@ extension Array {
1366
1366
}
1367
1367
1368
1368
extension Array {
1369
+ @available ( swift, deprecated: 5.1 , renamed: " init(unsafeUninitializedCapacity:initializingWith:) " )
1370
+ @inlinable
1371
+ public init (
1372
+ _unsafeUninitializedCapacity: Int ,
1373
+ initializingWith initializer: (
1374
+ _ buffer: inout UnsafeMutableBufferPointer < Element > ,
1375
+ _ initializedCount: inout Int ) throws -> Void
1376
+ ) rethrows {
1377
+ try self . init (
1378
+ unsafeUninitializedCapacity: _unsafeUninitializedCapacity,
1379
+ initializingWith: initializer)
1380
+ }
1381
+
1369
1382
/// Creates an array with the specified capacity, then calls the given
1370
1383
/// closure with a buffer covering the array's uninitialized memory.
1371
1384
///
1372
1385
/// Inside the closure, set the `initializedCount` parameter to the number of
1373
1386
/// elements that are initialized by the closure. The memory in the range
1374
1387
/// `buffer[0..<initializedCount]` must be initialized at the end of the
1375
1388
/// closure's execution, and the memory in the range
1376
- /// `buffer[initializedCount...]` must be uninitialized.
1389
+ /// `buffer[initializedCount...]` must be uninitialized. This postcondition
1390
+ /// must hold even if the `initializer` closure throws an error.
1377
1391
///
1378
1392
/// - Note: While the resulting array may have a capacity larger than the
1379
1393
/// requested amount, the buffer passed to the closure will cover exactly
1380
1394
/// the requested number of elements.
1381
1395
///
1382
1396
/// - Parameters:
1383
- /// - _unsafeUninitializedCapacity : The number of elements to allocate
1397
+ /// - unsafeUninitializedCapacity : The number of elements to allocate
1384
1398
/// space for in the new array.
1385
1399
/// - initializer: A closure that initializes elements and sets the count
1386
1400
/// of the new array.
@@ -1392,29 +1406,29 @@ extension Array {
1392
1406
/// elements you initialize.
1393
1407
@inlinable
1394
1408
public init (
1395
- _unsafeUninitializedCapacity : Int ,
1409
+ unsafeUninitializedCapacity : Int ,
1396
1410
initializingWith initializer: (
1397
1411
_ buffer: inout UnsafeMutableBufferPointer < Element > ,
1398
1412
_ initializedCount: inout Int ) throws -> Void
1399
1413
) rethrows {
1400
1414
var firstElementAddress : UnsafeMutablePointer < Element >
1401
1415
( self , firstElementAddress) =
1402
- Array . _allocateUninitialized ( _unsafeUninitializedCapacity )
1416
+ Array . _allocateUninitialized ( unsafeUninitializedCapacity )
1403
1417
1404
1418
var initializedCount = 0
1405
1419
defer {
1406
1420
// Update self.count even if initializer throws an error.
1407
1421
_precondition (
1408
- initializedCount <= _unsafeUninitializedCapacity ,
1422
+ initializedCount <= unsafeUninitializedCapacity ,
1409
1423
" Initialized count set to greater than specified capacity. "
1410
1424
)
1411
1425
self . _buffer. count = initializedCount
1412
1426
}
1413
1427
var buffer = UnsafeMutableBufferPointer < Element > (
1414
- start: firstElementAddress, count: _unsafeUninitializedCapacity )
1428
+ start: firstElementAddress, count: unsafeUninitializedCapacity )
1415
1429
try initializer ( & buffer, & initializedCount)
1416
1430
}
1417
-
1431
+
1418
1432
/// Calls a closure with a pointer to the array's contiguous storage.
1419
1433
///
1420
1434
/// Often, the optimizer can eliminate bounds checks within an array
0 commit comments