@@ -1366,17 +1366,35 @@ extension Array {
1366
1366
}
1367
1367
1368
1368
extension Array {
1369
- @available ( swift, deprecated: 5.1 , renamed: " init(unsafeUninitializedCapacity:initializingWith:) " )
1369
+ /// Implementation for Array(unsafeUninitializedCapacity:initializingWith:)
1370
+ /// and ContiguousArray(unsafeUninitializedCapacity:initializingWith:)
1370
1371
@inlinable
1371
- public init (
1372
+ internal init (
1372
1373
_unsafeUninitializedCapacity: Int ,
1373
1374
initializingWith initializer: (
1374
1375
_ buffer: inout UnsafeMutableBufferPointer < Element > ,
1375
1376
_ initializedCount: inout Int ) throws -> Void
1376
1377
) rethrows {
1377
- try self . init (
1378
- unsafeUninitializedCapacity: _unsafeUninitializedCapacity,
1379
- initializingWith: initializer)
1378
+ var firstElementAddress : UnsafeMutablePointer < Element >
1379
+ ( self , firstElementAddress) =
1380
+ Array . _allocateUninitialized ( _unsafeUninitializedCapacity)
1381
+
1382
+ var initializedCount = 0
1383
+ var buffer = UnsafeMutableBufferPointer < Element > (
1384
+ start: firstElementAddress, count: _unsafeUninitializedCapacity)
1385
+ defer {
1386
+ // Update self.count even if initializer throws an error.
1387
+ _precondition (
1388
+ initializedCount <= _unsafeUninitializedCapacity,
1389
+ " Initialized count set to greater than specified capacity. "
1390
+ )
1391
+ _precondition (
1392
+ buffer. baseAddress == firstElementAddress,
1393
+ " Can't reassign buffer in Array(unsafeUninitializedCapacity:initializingWith:) "
1394
+ )
1395
+ self . _buffer. count = initializedCount
1396
+ }
1397
+ try initializer ( & buffer, & initializedCount)
1380
1398
}
1381
1399
1382
1400
/// Creates an array with the specified capacity, then calls the given
@@ -1404,29 +1422,16 @@ extension Array {
1404
1422
/// - initializedCount: The count of initialized elements in the array,
1405
1423
/// which begins as zero. Set `initializedCount` to the number of
1406
1424
/// elements you initialize.
1407
- @inlinable
1425
+ @_alwaysEmitIntoClient @ inlinable
1408
1426
public init (
1409
1427
unsafeUninitializedCapacity: Int ,
1410
1428
initializingWith initializer: (
1411
1429
_ buffer: inout UnsafeMutableBufferPointer < Element > ,
1412
1430
_ initializedCount: inout Int ) throws -> Void
1413
1431
) rethrows {
1414
- var firstElementAddress : UnsafeMutablePointer < Element >
1415
- ( self , firstElementAddress) =
1416
- Array . _allocateUninitialized ( unsafeUninitializedCapacity)
1417
-
1418
- var initializedCount = 0
1419
- defer {
1420
- // Update self.count even if initializer throws an error.
1421
- _precondition (
1422
- initializedCount <= unsafeUninitializedCapacity,
1423
- " Initialized count set to greater than specified capacity. "
1424
- )
1425
- self . _buffer. count = initializedCount
1426
- }
1427
- var buffer = UnsafeMutableBufferPointer < Element > (
1428
- start: firstElementAddress, count: unsafeUninitializedCapacity)
1429
- try initializer ( & buffer, & initializedCount)
1432
+ self = try Array (
1433
+ _unsafeUninitializedCapacity: unsafeUninitializedCapacity,
1434
+ initializingWith: initializer)
1430
1435
}
1431
1436
1432
1437
/// Calls a closure with a pointer to the array's contiguous storage.
0 commit comments