Skip to content

Commit 1e1049a

Browse files
authored
Merge pull request #4085 from apple/stdlib-make-array-implementation-internal-3
stdlib: make Array implementation internal, part 2
2 parents 62c814a + 19c29f6 commit 1e1049a

File tree

8 files changed

+154
-160
lines changed

8 files changed

+154
-160
lines changed

stdlib/public/core/ArrayBuffer.swift

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ internal typealias _ArrayBridgeStorage
2222
= _BridgeStorage<_ContiguousArrayStorageBase, _NSArrayCore>
2323

2424
@_fixed_layout
25-
public struct _ArrayBuffer<Element> : _ArrayBufferProtocol {
25+
internal struct _ArrayBuffer<Element> : _ArrayBufferProtocol {
2626

2727
/// Create an empty buffer.
28-
public init() {
28+
internal init() {
2929
_storage = _ArrayBridgeStorage(native: _emptyArrayStorage)
3030
}
3131

32-
public init(nsArray: _NSArrayCore) {
32+
internal init(nsArray: _NSArrayCore) {
3333
_sanityCheck(_isClassOrObjCExistential(Element.self))
3434
_storage = _ArrayBridgeStorage(objC: nsArray)
3535
}
@@ -46,7 +46,7 @@ public struct _ArrayBuffer<Element> : _ArrayBufferProtocol {
4646

4747
/// The spare bits that are set when a native array needs deferred
4848
/// element type checking.
49-
var deferredTypeCheckMask: Int { return 1 }
49+
internal var deferredTypeCheckMask: Int { return 1 }
5050

5151
/// Returns an `_ArrayBuffer<U>` containing the same elements,
5252
/// deferring checking each element's `U`-ness until it is accessed.
@@ -68,7 +68,7 @@ public struct _ArrayBuffer<Element> : _ArrayBufferProtocol {
6868
native: _native._storage, bits: deferredTypeCheckMask))
6969
}
7070

71-
var needsElementTypeCheck: Bool {
71+
internal var needsElementTypeCheck: Bool {
7272
// NSArray's need an element typecheck when the element type isn't AnyObject
7373
return !_isNativeTypeChecked && !(AnyObject.self is Element.Type)
7474
}
@@ -83,18 +83,18 @@ public struct _ArrayBuffer<Element> : _ArrayBufferProtocol {
8383

8484
extension _ArrayBuffer {
8585
/// Adopt the storage of `source`.
86-
public init(_buffer source: NativeBuffer, shiftedToStartIndex: Int) {
86+
internal init(_buffer source: NativeBuffer, shiftedToStartIndex: Int) {
8787
_sanityCheck(shiftedToStartIndex == 0, "shiftedToStartIndex must be 0")
8888
_storage = _ArrayBridgeStorage(native: source._storage)
8989
}
9090

9191
/// `true`, if the array is native and does not need a deferred type check.
92-
var arrayPropertyIsNativeTypeChecked: Bool {
92+
internal var arrayPropertyIsNativeTypeChecked: Bool {
9393
return _isNativeTypeChecked
9494
}
9595

9696
/// Returns `true` iff this buffer's storage is uniquely-referenced.
97-
mutating func isUniquelyReferenced() -> Bool {
97+
internal mutating func isUniquelyReferenced() -> Bool {
9898
if !_isClassOrObjCExistential(Element.self) {
9999
return _storage.isUniquelyReferenced_native_noSpareBits()
100100
}
@@ -103,7 +103,7 @@ extension _ArrayBuffer {
103103

104104
/// Returns `true` iff this buffer's storage is either
105105
/// uniquely-referenced or pinned.
106-
mutating func isUniquelyReferencedOrPinned() -> Bool {
106+
internal mutating func isUniquelyReferencedOrPinned() -> Bool {
107107
if !_isClassOrObjCExistential(Element.self) {
108108
return _storage.isUniquelyReferencedOrPinned_native_noSpareBits()
109109
}
@@ -113,15 +113,15 @@ extension _ArrayBuffer {
113113
/// Convert to an NSArray.
114114
///
115115
/// O(1) if the element type is bridged verbatim, O(N) otherwise.
116-
public func _asCocoaArray() -> _NSArrayCore {
116+
internal func _asCocoaArray() -> _NSArrayCore {
117117
return _fastPath(_isNative) ? _native._asCocoaArray() : _nonNative
118118
}
119119

120120
/// If this buffer is backed by a uniquely-referenced mutable
121121
/// `_ContiguousArrayBuffer` that can be grown in-place to allow the self
122122
/// buffer store minimumCapacity elements, returns that buffer.
123123
/// Otherwise, returns `nil`.
124-
public mutating func requestUniqueMutableBackingBuffer(minimumCapacity: Int)
124+
internal mutating func requestUniqueMutableBackingBuffer(minimumCapacity: Int)
125125
-> NativeBuffer? {
126126
if _fastPath(isUniquelyReferenced()) {
127127
let b = _native
@@ -132,18 +132,18 @@ extension _ArrayBuffer {
132132
return nil
133133
}
134134

135-
public mutating func isMutableAndUniquelyReferenced() -> Bool {
135+
internal mutating func isMutableAndUniquelyReferenced() -> Bool {
136136
return isUniquelyReferenced()
137137
}
138138

139-
public mutating func isMutableAndUniquelyReferencedOrPinned() -> Bool {
139+
internal mutating func isMutableAndUniquelyReferencedOrPinned() -> Bool {
140140
return isUniquelyReferencedOrPinned()
141141
}
142142

143143
/// If this buffer is backed by a `_ContiguousArrayBuffer`
144144
/// containing the same number of elements as `self`, return it.
145145
/// Otherwise, return `nil`.
146-
public func requestNativeBuffer() -> NativeBuffer? {
146+
internal func requestNativeBuffer() -> NativeBuffer? {
147147
if !_isClassOrObjCExistential(Element.self) {
148148
return _native
149149
}
@@ -170,7 +170,7 @@ extension _ArrayBuffer {
170170
}
171171
}
172172

173-
func _typeCheck(_ subRange: Range<Int>) {
173+
internal func _typeCheck(_ subRange: Range<Int>) {
174174
if !_isClassOrObjCExistential(Element.self) {
175175
return
176176
}
@@ -189,7 +189,7 @@ extension _ArrayBuffer {
189189
/// memory starting at `target`. Return a pointer "past the end" of the
190190
/// just-initialized memory.
191191
@discardableResult
192-
public func _copyContents(
192+
internal func _copyContents(
193193
subRange bounds: Range<Int>,
194194
initializing target: UnsafeMutablePointer<Element>
195195
) -> UnsafeMutablePointer<Element> {
@@ -221,7 +221,7 @@ extension _ArrayBuffer {
221221

222222
/// Returns a `_SliceBuffer` containing the given sub-range of elements in
223223
/// `bounds` from this buffer.
224-
public subscript(bounds: Range<Int>) -> _SliceBuffer<Element> {
224+
internal subscript(bounds: Range<Int>) -> _SliceBuffer<Element> {
225225
get {
226226
_typeCheck(bounds)
227227

@@ -254,7 +254,7 @@ extension _ArrayBuffer {
254254

255255
// No contiguous storage found; we must allocate
256256
let result = _ContiguousArrayBuffer<Element>(
257-
uninitializedCount: boundsCount, minimumCapacity: 0)
257+
_uninitializedCount: boundsCount, minimumCapacity: 0)
258258

259259
// Tell Cocoa to copy the objects into our storage
260260
cocoa.buffer.getObjects(
@@ -273,17 +273,17 @@ extension _ArrayBuffer {
273273
/// A pointer to the first element.
274274
///
275275
/// - Precondition: The elements are known to be stored contiguously.
276-
public var firstElementAddress: UnsafeMutablePointer<Element> {
276+
internal var firstElementAddress: UnsafeMutablePointer<Element> {
277277
_sanityCheck(_isNative, "must be a native buffer")
278278
return _native.firstElementAddress
279279
}
280280

281-
public var firstElementAddressIfContiguous: UnsafeMutablePointer<Element>? {
281+
internal var firstElementAddressIfContiguous: UnsafeMutablePointer<Element>? {
282282
return _fastPath(_isNative) ? firstElementAddress : nil
283283
}
284284

285285
/// The number of elements the buffer stores.
286-
public var count: Int {
286+
internal var count: Int {
287287
@inline(__always)
288288
get {
289289
return _fastPath(_isNative) ? _native.count : _nonNative.count
@@ -331,13 +331,13 @@ extension _ArrayBuffer {
331331
}
332332

333333
/// The number of elements the buffer can store without reallocation.
334-
public var capacity: Int {
334+
internal var capacity: Int {
335335
return _fastPath(_isNative) ? _native.capacity : _nonNative.count
336336
}
337337

338338
@_versioned
339339
@inline(__always)
340-
func getElement(_ i: Int, wasNativeTypeChecked: Bool) -> Element {
340+
internal func getElement(_ i: Int, wasNativeTypeChecked: Bool) -> Element {
341341
if _fastPath(wasNativeTypeChecked) {
342342
return _nativeTypeChecked[i]
343343
}
@@ -346,7 +346,7 @@ extension _ArrayBuffer {
346346

347347
@_versioned
348348
@inline(never)
349-
func _getElementSlowPath(_ i: Int) -> AnyObject {
349+
internal func _getElementSlowPath(_ i: Int) -> AnyObject {
350350
_sanityCheck(
351351
_isClassOrObjCExistential(Element.self),
352352
"Only single reference elements can be indexed here.")
@@ -372,7 +372,7 @@ extension _ArrayBuffer {
372372
}
373373

374374
/// Get or set the value of the ith element.
375-
public subscript(i: Int) -> Element {
375+
internal subscript(i: Int) -> Element {
376376
get {
377377
return getElement(i, wasNativeTypeChecked: _isNativeTypeChecked)
378378
}
@@ -394,7 +394,7 @@ extension _ArrayBuffer {
394394
/// Call `body(p)`, where `p` is an `UnsafeBufferPointer` over the
395395
/// underlying contiguous storage. If no such storage exists, it is
396396
/// created on-demand.
397-
public func withUnsafeBufferPointer<R>(
397+
internal func withUnsafeBufferPointer<R>(
398398
_ body: (UnsafeBufferPointer<Element>) throws -> R
399399
) rethrows -> R {
400400
if _fastPath(_isNative) {
@@ -409,7 +409,7 @@ extension _ArrayBuffer {
409409
/// over the underlying contiguous storage.
410410
///
411411
/// - Precondition: Such contiguous storage exists or the buffer is empty.
412-
public mutating func withUnsafeMutableBufferPointer<R>(
412+
internal mutating func withUnsafeMutableBufferPointer<R>(
413413
_ body: (UnsafeMutableBufferPointer<Element>) throws -> R
414414
) rethrows -> R {
415415
_sanityCheck(
@@ -422,22 +422,22 @@ extension _ArrayBuffer {
422422
}
423423

424424
/// An object that keeps the elements stored in this buffer alive.
425-
public var owner: AnyObject {
425+
internal var owner: AnyObject {
426426
return _fastPath(_isNative) ? _native._storage : _nonNative
427427
}
428428

429429
/// An object that keeps the elements stored in this buffer alive.
430430
///
431431
/// - Precondition: This buffer is backed by a `_ContiguousArrayBuffer`.
432-
public var nativeOwner: AnyObject {
432+
internal var nativeOwner: AnyObject {
433433
_sanityCheck(_isNative, "Expect a native array")
434434
return _native._storage
435435
}
436436

437437
/// A value that identifies the storage used by the buffer. Two
438438
/// buffers address the same elements when they have the same
439439
/// identity and count.
440-
public var identity: UnsafeRawPointer {
440+
internal var identity: UnsafeRawPointer {
441441
if _isNative {
442442
return _native.identity
443443
}
@@ -450,7 +450,7 @@ extension _ArrayBuffer {
450450
/// The position of the first element in a non-empty collection.
451451
///
452452
/// In an empty collection, `startIndex == endIndex`.
453-
public var startIndex: Int {
453+
internal var startIndex: Int {
454454
return 0
455455
}
456456

@@ -459,18 +459,18 @@ extension _ArrayBuffer {
459459
/// `endIndex` is not a valid argument to `subscript`, and is always
460460
/// reachable from `startIndex` by zero or more applications of
461461
/// `index(after:)`.
462-
public var endIndex: Int {
462+
internal var endIndex: Int {
463463
return count
464464
}
465465

466-
public typealias Indices = CountableRange<Int>
466+
internal typealias Indices = CountableRange<Int>
467467

468468
//===--- private --------------------------------------------------------===//
469-
typealias Storage = _ContiguousArrayStorage<Element>
470-
public typealias NativeBuffer = _ContiguousArrayBuffer<Element>
469+
internal typealias Storage = _ContiguousArrayStorage<Element>
470+
internal typealias NativeBuffer = _ContiguousArrayBuffer<Element>
471471

472472
@_versioned
473-
var _isNative: Bool {
473+
internal var _isNative: Bool {
474474
if !_isClassOrObjCExistential(Element.self) {
475475
return true
476476
} else {
@@ -479,7 +479,7 @@ extension _ArrayBuffer {
479479
}
480480

481481
/// `true`, if the array is native and does not need a deferred type check.
482-
var _isNativeTypeChecked: Bool {
482+
internal var _isNativeTypeChecked: Bool {
483483
if !_isClassOrObjCExistential(Element.self) {
484484
return true
485485
} else {
@@ -491,7 +491,7 @@ extension _ArrayBuffer {
491491
///
492492
/// - Precondition: `_isNative`.
493493
@_versioned
494-
var _native: NativeBuffer {
494+
internal var _native: NativeBuffer {
495495
return NativeBuffer(
496496
_isClassOrObjCExistential(Element.self)
497497
? _storage.nativeInstance : _storage.nativeInstance_noSpareBits)
@@ -501,12 +501,12 @@ extension _ArrayBuffer {
501501
///
502502
/// - Precondition: `_isNativeTypeChecked`.
503503
@_versioned
504-
var _nativeTypeChecked: NativeBuffer {
504+
internal var _nativeTypeChecked: NativeBuffer {
505505
return NativeBuffer(_storage.nativeInstance_noSpareBits)
506506
}
507507

508508
@_versioned
509-
var _nonNative: _NSArrayCore {
509+
internal var _nonNative: _NSArrayCore {
510510
@inline(__always)
511511
get {
512512
_sanityCheck(_isClassOrObjCExistential(Element.self))

stdlib/public/core/ArrayBufferProtocol.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
/// The underlying buffer for an ArrayType conforms to
1414
/// `_ArrayBufferProtocol`. This buffer does not provide value semantics.
15-
public protocol _ArrayBufferProtocol
15+
internal protocol _ArrayBufferProtocol
1616
: MutableCollection, RandomAccessCollection {
1717

1818
associatedtype Indices : RandomAccessCollection = CountableRange<Int>
@@ -127,11 +127,11 @@ public protocol _ArrayBufferProtocol
127127

128128
extension _ArrayBufferProtocol where Index == Int {
129129

130-
public var subscriptBaseAddress: UnsafeMutablePointer<Element> {
130+
internal var subscriptBaseAddress: UnsafeMutablePointer<Element> {
131131
return firstElementAddress
132132
}
133133

134-
public mutating func replace<C>(
134+
internal mutating func replace<C>(
135135
subRange: Range<Int>,
136136
with newCount: Int,
137137
elementsOf newValues: C

stdlib/public/core/ArrayType.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
public // @testable
14-
protocol _ArrayProtocol
13+
internal protocol _ArrayProtocol
1514
: RangeReplaceableCollection,
1615
ExpressibleByArrayLiteral
1716
{

0 commit comments

Comments
 (0)