@@ -20,7 +20,7 @@ import SwiftShims
20
20
21
21
@usableFromInline
22
22
internal typealias _ArrayBridgeStorage
23
- = _BridgeStorage < __ContiguousArrayStorageBase , _NSArrayCore >
23
+ = _BridgeStorage < __ContiguousArrayStorageBase >
24
24
25
25
@usableFromInline
26
26
@_fixed_layout
@@ -33,7 +33,7 @@ internal struct _ArrayBuffer<Element> : _ArrayBufferProtocol {
33
33
}
34
34
35
35
@inlinable
36
- internal init ( nsArray: _NSArrayCore ) {
36
+ internal init ( nsArray: AnyObject ) {
37
37
_sanityCheck ( _isClassOrObjCExistential ( Element . self) )
38
38
_storage = _ArrayBridgeStorage ( objC: nsArray)
39
39
}
@@ -121,8 +121,8 @@ extension _ArrayBuffer {
121
121
///
122
122
/// O(1) if the element type is bridged verbatim, O(*n*) otherwise.
123
123
@inlinable
124
- internal func _asCocoaArray( ) -> _NSArrayCore {
125
- return _fastPath ( _isNative) ? _native. _asCocoaArray ( ) : _nonNative
124
+ internal func _asCocoaArray( ) -> AnyObject {
125
+ return _fastPath ( _isNative) ? _native. _asCocoaArray ( ) : _nonNative. buffer
126
126
}
127
127
128
128
/// If this buffer is backed by a uniquely-referenced mutable
@@ -175,8 +175,7 @@ extension _ArrayBuffer {
175
175
)
176
176
}
177
177
else {
178
- let ns = _nonNative
179
- let element = ns. objectAt ( index)
178
+ let element = _nonNative [ index]
180
179
precondition (
181
180
element is Element ,
182
181
"""
@@ -216,26 +215,12 @@ extension _ArrayBuffer {
216
215
if _fastPath ( _isNative) {
217
216
return _native. _copyContents ( subRange: bounds, initializing: target)
218
217
}
219
-
220
- let nonNative = _nonNative
221
-
222
- let nsSubRange = SwiftShims . _SwiftNSRange (
223
- location: bounds. lowerBound,
224
- length: bounds. upperBound - bounds. lowerBound)
225
-
226
- let buffer = UnsafeMutableRawPointer ( target) . assumingMemoryBound (
227
- to: AnyObject . self)
228
-
229
- // Copies the references out of the NSArray without retaining them
230
- nonNative. getObjects ( buffer, range: nsSubRange)
231
-
232
- // Make another pass to retain the copied objects
233
- var result = target
234
- for _ in bounds {
235
- result. initialize ( to: result. pointee)
236
- result += 1
237
- }
238
- return result
218
+ let buffer = UnsafeMutableRawPointer ( target)
219
+ . assumingMemoryBound ( to: AnyObject . self)
220
+ let result = _nonNative. _copyContents (
221
+ subRange: bounds,
222
+ initializing: buffer)
223
+ return UnsafeMutableRawPointer ( result) . assumingMemoryBound ( to: Element . self)
239
224
}
240
225
241
226
/// Returns a `_SliceBuffer` containing the given sub-range of elements in
@@ -244,45 +229,10 @@ extension _ArrayBuffer {
244
229
internal subscript( bounds: Range < Int > ) -> _SliceBuffer < Element > {
245
230
get {
246
231
_typeCheck ( bounds)
247
-
248
232
if _fastPath ( _isNative) {
249
233
return _native [ bounds]
250
234
}
251
-
252
- let boundsCount = bounds. count
253
- if boundsCount == 0 {
254
- return _SliceBuffer (
255
- _buffer: _ContiguousArrayBuffer < Element > ( ) ,
256
- shiftedToStartIndex: bounds. lowerBound)
257
- }
258
-
259
- // Look for contiguous storage in the NSArray
260
- let nonNative = self . _nonNative
261
- let cocoa = _CocoaArrayWrapper ( nonNative)
262
- let cocoaStorageBaseAddress = cocoa. contiguousStorage ( self . indices)
263
-
264
- if let cocoaStorageBaseAddress = cocoaStorageBaseAddress {
265
- let basePtr = UnsafeMutableRawPointer ( cocoaStorageBaseAddress)
266
- . assumingMemoryBound ( to: Element . self)
267
- return _SliceBuffer (
268
- owner: nonNative,
269
- subscriptBaseAddress: basePtr,
270
- indices: bounds,
271
- hasNativeBuffer: false )
272
- }
273
-
274
- // No contiguous storage found; we must allocate
275
- let result = _ContiguousArrayBuffer < Element > (
276
- _uninitializedCount: boundsCount, minimumCapacity: 0 )
277
-
278
- // Tell Cocoa to copy the objects into our storage
279
- cocoa. buffer. getObjects (
280
- UnsafeMutableRawPointer ( result. firstElementAddress)
281
- . assumingMemoryBound ( to: AnyObject . self) ,
282
- range: _SwiftNSRange ( location: bounds. lowerBound, length: boundsCount) )
283
-
284
- return _SliceBuffer (
285
- _buffer: result, shiftedToStartIndex: bounds. lowerBound)
235
+ return _nonNative [ bounds] . unsafeCastElements ( to: Element . self)
286
236
}
287
237
set {
288
238
fatalError ( " not implemented " )
@@ -315,7 +265,7 @@ extension _ArrayBuffer {
315
265
_native. count = newValue
316
266
}
317
267
}
318
-
268
+
319
269
/// Traps if an inout violation is detected or if the buffer is
320
270
/// native and the subscript is out of range.
321
271
///
@@ -392,7 +342,7 @@ extension _ArrayBuffer {
392
342
)
393
343
} else {
394
344
// ObjC arrays do their own subscript checking.
395
- element = _nonNative. objectAt ( i )
345
+ element = _nonNative [ i ]
396
346
precondition (
397
347
element is Element ,
398
348
"""
@@ -460,7 +410,7 @@ extension _ArrayBuffer {
460
410
/// An object that keeps the elements stored in this buffer alive.
461
411
@inlinable
462
412
internal var owner : AnyObject {
463
- return _fastPath ( _isNative) ? _native. _storage : _nonNative
413
+ return _fastPath ( _isNative) ? _native. _storage : _nonNative. buffer
464
414
}
465
415
466
416
/// An object that keeps the elements stored in this buffer alive.
@@ -481,7 +431,8 @@ extension _ArrayBuffer {
481
431
return _native. identity
482
432
}
483
433
else {
484
- return UnsafeRawPointer ( Unmanaged . passUnretained ( _nonNative) . toOpaque ( ) )
434
+ return UnsafeRawPointer (
435
+ Unmanaged . passUnretained ( _nonNative. buffer) . toOpaque ( ) )
485
436
}
486
437
}
487
438
@@ -550,11 +501,11 @@ extension _ArrayBuffer {
550
501
}
551
502
552
503
@inlinable
553
- internal var _nonNative : _NSArrayCore {
504
+ internal var _nonNative : _CocoaArrayWrapper {
554
505
@inline ( __always)
555
506
get {
556
507
_sanityCheck ( _isClassOrObjCExistential ( Element . self) )
557
- return _storage. objCInstance
508
+ return _CocoaArrayWrapper ( _storage. objCInstance)
558
509
}
559
510
}
560
511
}
0 commit comments