[stdlib] Make protocol _NSArrayCore internal

These shadow protocols don’t add any real type safety, and exposing them in the ABI seems unnecessary.
This commit is contained in:
Karoy Lorentey
2018-11-12 15:44:09 +00:00
parent c71abaaf51
commit ee17e975ca
9 changed files with 131 additions and 106 deletions

View File

@@ -20,7 +20,7 @@ import SwiftShims
@usableFromInline
internal typealias _ArrayBridgeStorage
= _BridgeStorage<__ContiguousArrayStorageBase, _NSArrayCore>
= _BridgeStorage<__ContiguousArrayStorageBase, AnyObject>
@usableFromInline
@_fixed_layout
@@ -33,7 +33,7 @@ internal struct _ArrayBuffer<Element> : _ArrayBufferProtocol {
}
@inlinable
internal init(nsArray: _NSArrayCore) {
internal init(nsArray: AnyObject) {
_sanityCheck(_isClassOrObjCExistential(Element.self))
_storage = _ArrayBridgeStorage(objC: nsArray)
}
@@ -121,8 +121,8 @@ extension _ArrayBuffer {
///
/// O(1) if the element type is bridged verbatim, O(*n*) otherwise.
@inlinable
internal func _asCocoaArray() -> _NSArrayCore {
return _fastPath(_isNative) ? _native._asCocoaArray() : _nonNative
internal func _asCocoaArray() -> AnyObject {
return _fastPath(_isNative) ? _native._asCocoaArray() : _nonNative.buffer
}
/// If this buffer is backed by a uniquely-referenced mutable
@@ -175,8 +175,7 @@ extension _ArrayBuffer {
)
}
else {
let ns = _nonNative
let element = ns.objectAt(index)
let element = _nonNative[index]
precondition(
element is Element,
"""
@@ -216,26 +215,12 @@ extension _ArrayBuffer {
if _fastPath(_isNative) {
return _native._copyContents(subRange: bounds, initializing: target)
}
let nonNative = _nonNative
let nsSubRange = SwiftShims._SwiftNSRange(
location: bounds.lowerBound,
length: bounds.upperBound - bounds.lowerBound)
let buffer = UnsafeMutableRawPointer(target).assumingMemoryBound(
to: AnyObject.self)
// Copies the references out of the NSArray without retaining them
nonNative.getObjects(buffer, range: nsSubRange)
// Make another pass to retain the copied objects
var result = target
for _ in bounds {
result.initialize(to: result.pointee)
result += 1
}
return result
let buffer = UnsafeMutableRawPointer(target)
.assumingMemoryBound(to: AnyObject.self)
let result = _nonNative._copyContents(
subRange: bounds,
initializing: buffer)
return UnsafeMutableRawPointer(result).assumingMemoryBound(to: Element.self)
}
/// Returns a `_SliceBuffer` containing the given sub-range of elements in
@@ -244,45 +229,10 @@ extension _ArrayBuffer {
internal subscript(bounds: Range<Int>) -> _SliceBuffer<Element> {
get {
_typeCheck(bounds)
if _fastPath(_isNative) {
return _native[bounds]
}
let boundsCount = bounds.count
if boundsCount == 0 {
return _SliceBuffer(
_buffer: _ContiguousArrayBuffer<Element>(),
shiftedToStartIndex: bounds.lowerBound)
}
// Look for contiguous storage in the NSArray
let nonNative = self._nonNative
let cocoa = _CocoaArrayWrapper(nonNative)
let cocoaStorageBaseAddress = cocoa.contiguousStorage(self.indices)
if let cocoaStorageBaseAddress = cocoaStorageBaseAddress {
let basePtr = UnsafeMutableRawPointer(cocoaStorageBaseAddress)
.assumingMemoryBound(to: Element.self)
return _SliceBuffer(
owner: nonNative,
subscriptBaseAddress: basePtr,
indices: bounds,
hasNativeBuffer: false)
}
// No contiguous storage found; we must allocate
let result = _ContiguousArrayBuffer<Element>(
_uninitializedCount: boundsCount, minimumCapacity: 0)
// Tell Cocoa to copy the objects into our storage
cocoa.buffer.getObjects(
UnsafeMutableRawPointer(result.firstElementAddress)
.assumingMemoryBound(to: AnyObject.self),
range: _SwiftNSRange(location: bounds.lowerBound, length: boundsCount))
return _SliceBuffer(
_buffer: result, shiftedToStartIndex: bounds.lowerBound)
return _nonNative[bounds].unsafeCastElements(to: Element.self)
}
set {
fatalError("not implemented")
@@ -315,7 +265,7 @@ extension _ArrayBuffer {
_native.count = newValue
}
}
/// Traps if an inout violation is detected or if the buffer is
/// native and the subscript is out of range.
///
@@ -392,7 +342,7 @@ extension _ArrayBuffer {
)
} else {
// ObjC arrays do their own subscript checking.
element = _nonNative.objectAt(i)
element = _nonNative[i]
precondition(
element is Element,
"""
@@ -460,7 +410,7 @@ extension _ArrayBuffer {
/// An object that keeps the elements stored in this buffer alive.
@inlinable
internal var owner: AnyObject {
return _fastPath(_isNative) ? _native._storage : _nonNative
return _fastPath(_isNative) ? _native._storage : _nonNative.buffer
}
/// An object that keeps the elements stored in this buffer alive.
@@ -481,7 +431,8 @@ extension _ArrayBuffer {
return _native.identity
}
else {
return UnsafeRawPointer(Unmanaged.passUnretained(_nonNative).toOpaque())
return UnsafeRawPointer(
Unmanaged.passUnretained(_nonNative.buffer).toOpaque())
}
}
@@ -550,11 +501,11 @@ extension _ArrayBuffer {
}
@inlinable
internal var _nonNative: _NSArrayCore {
internal var _nonNative: _CocoaArrayWrapper {
@inline(__always)
get {
_sanityCheck(_isClassOrObjCExistential(Element.self))
return _storage.objCInstance
return _CocoaArrayWrapper(_storage.objCInstance)
}
}
}