Skip to content

Commit 7e6a105

Browse files
committed
stdlib: underscore AnyCollectionProtocol
1 parent 34b9b98 commit 7e6a105

File tree

2 files changed

+27
-35
lines changed

2 files changed

+27
-35
lines changed

stdlib/public/core/ExistentialCollection.swift.gyb

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -715,28 +715,11 @@ public func < (lhs: AnyIndex, rhs: AnyIndex) -> Bool {
715715
//===--- Collections ------------------------------------------------------===//
716716
//===----------------------------------------------------------------------===//
717717

718-
/// A protocol for `AnyCollection<Element>`,
719-
/// `AnyBidirectionalCollection<Element>`, and
720-
/// `AnyRandomAccessCollection<Element>`.
721-
public protocol AnyCollectionProtocol : Collection {
718+
public // @testable
719+
protocol _AnyCollectionProtocol : Collection {
722720
/// Identifies the underlying collection stored by `self`. Instances
723-
/// copied from one another have the same `_underlyingCollectionID`.
724-
var _underlyingCollectionID: ObjectIdentifier { get }
725-
726-
/// Returns `true` iff `self` and `other` use the same `_box` to store their
727-
/// underlying collections.
728-
func _storesSameUnderlyingCollection<
729-
C: AnyCollectionProtocol
730-
>(_ other: C) -> Bool
731-
}
732-
733-
extension AnyCollectionProtocol {
734-
public // @testable
735-
func _storesSameUnderlyingCollection<
736-
C: AnyCollectionProtocol
737-
>(_ other: C) -> Bool {
738-
return _underlyingCollectionID == other._underlyingCollectionID
739-
}
721+
/// copied or upgraded/downgraded from one another have the same `_boxID`.
722+
var _boxID: ObjectIdentifier { get }
740723
}
741724

742725
% for (ti, Traversal) in enumerate(TRAVERSALS):
@@ -751,7 +734,7 @@ extension AnyCollectionProtocol {
751734
///
752735
/// - SeeAlso: ${', '.join('`Any%sCollection`' % t for t in (2 * TRAVERSALS)[ti + 1 : ti + 3]) }
753736
public struct ${Self}<Element>
754-
: AnyCollectionProtocol, ${SelfProtocol} {
737+
: _AnyCollectionProtocol, ${SelfProtocol} {
755738

756739
internal init(_box: _${Self}Box<Element>) {
757740
self._box = _box
@@ -948,7 +931,7 @@ public struct ${Self}<Element>
948931

949932
/// Uniquely identifies the stored underlying collection.
950933
public // Due to language limitations only
951-
var _underlyingCollectionID: ObjectIdentifier {
934+
var _boxID: ObjectIdentifier {
952935
return ObjectIdentifier(_box)
953936
}
954937

@@ -976,10 +959,13 @@ extension Any${Kind} {
976959
}
977960
%end
978961

979-
@available(*, unavailable, renamed: "AnyCollectionProtocol")
980-
public typealias AnyCollectionType = AnyCollectionProtocol
962+
@available(*, unavailable, renamed: "_AnyCollectionProtocol")
963+
public typealias AnyCollectionType = _AnyCollectionProtocol
964+
965+
@available(*, unavailable, renamed: "_AnyCollectionProtocol")
966+
public typealias AnyCollectionProtocol = _AnyCollectionProtocol
981967

982-
extension AnyCollectionProtocol {
968+
extension _AnyCollectionProtocol {
983969
@available(*, unavailable, renamed: "makeIterator()")
984970
public func generate() -> AnyIterator<Iterator.Element> {
985971
Builtin.unreachable()
@@ -1004,14 +990,14 @@ public func anyGenerator<Element>(_ body: () -> Element?) -> AnyIterator<Element
1004990

1005991
@available(*, unavailable)
1006992
public func === <
1007-
L : AnyCollectionProtocol, R : AnyCollectionProtocol
993+
L : _AnyCollectionProtocol, R : _AnyCollectionProtocol
1008994
>(lhs: L, rhs: R) -> Bool {
1009995
Builtin.unreachable()
1010996
}
1011997

1012998
@available(*, unavailable)
1013999
public func !== <
1014-
L : AnyCollectionProtocol, R : AnyCollectionProtocol
1000+
L : _AnyCollectionProtocol, R : _AnyCollectionProtocol
10151001
>(lhs: L, rhs: R) -> Bool {
10161002
Builtin.unreachable()
10171003
}

validation-test/stdlib/ExistentialCollection.swift.gyb

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ extension AnyRandomAccessCollection where Element : TestProtocol1 {
5757
}
5858
}
5959

60+
func storesSameUnderlyingCollection<
61+
L: _AnyCollectionProtocol, R: _AnyCollectionProtocol
62+
>(_ lhs: L, _ rhs: R) -> Bool {
63+
return lhs._boxID == rhs._boxID
64+
}
65+
6066
var tests = TestSuite("ExistentialCollection")
6167

6268
tests.test("AnyIterator") {
@@ -357,13 +363,13 @@ tests.test("BidirectionalCollection") {
357363
let bc0_ = AnyBidirectionalCollection(fc0) // upgrade!
358364
expectNotEmpty(bc0_)
359365
let bc0 = bc0_!
360-
expectTrue(fc0._storesSameUnderlyingCollection(bc0))
366+
expectTrue(storesSameUnderlyingCollection(fc0, bc0))
361367

362368
let fc1 = AnyCollection(a0.lazy.reversed()) // new collection
363-
expectFalse(fc1._storesSameUnderlyingCollection(fc0))
369+
expectFalse(storesSameUnderlyingCollection(fc1, fc0))
364370

365371
let fc2 = AnyCollection(bc0) // downgrade
366-
expectTrue(fc2._storesSameUnderlyingCollection(bc0))
372+
expectTrue(storesSameUnderlyingCollection(fc2, bc0))
367373

368374
let a1 = ContiguousArray(bc0.lazy.reversed())
369375
expectEqual(a0, a1)
@@ -381,7 +387,7 @@ tests.test("BidirectionalCollection") {
381387
let s0 = "Hello, Woyld".characters
382388
let bc1 = AnyBidirectionalCollection(s0)
383389
let fc3 = AnyCollection(bc1)
384-
expectTrue(fc3._storesSameUnderlyingCollection(bc1))
390+
expectTrue(storesSameUnderlyingCollection(fc3, bc1))
385391
expectEmpty(AnyRandomAccessCollection(bc1))
386392
expectEmpty(AnyRandomAccessCollection(fc3))
387393
}
@@ -392,13 +398,13 @@ tests.test("RandomAccessCollection") {
392398
let rc0_ = AnyRandomAccessCollection(fc0) // upgrade!
393399
expectNotEmpty(rc0_)
394400
let rc0 = rc0_!
395-
expectTrue(rc0._storesSameUnderlyingCollection(fc0))
401+
expectTrue(storesSameUnderlyingCollection(rc0, fc0))
396402

397403
let bc1 = AnyBidirectionalCollection(rc0) // downgrade
398-
expectTrue(bc1._storesSameUnderlyingCollection(rc0))
404+
expectTrue(storesSameUnderlyingCollection(bc1, rc0))
399405

400406
let fc1 = AnyBidirectionalCollection(rc0) // downgrade
401-
expectTrue(fc1._storesSameUnderlyingCollection(rc0))
407+
expectTrue(storesSameUnderlyingCollection(fc1, rc0))
402408

403409
let a1 = ContiguousArray(rc0.lazy.reversed())
404410
expectEqual(a0, a1)

0 commit comments

Comments
 (0)