Skip to content

Commit 3bf6b60

Browse files
author
Dave Abrahams
authored
Merge pull request #4825 from apple/stdlib-default-RangeReplaceableCollection.SubSequence
stdlib: make RangeReplaceableCollection.SubSequence a RangeReplaceableCollection
2 parents b31e416 + 25378de commit 3bf6b60

File tree

4 files changed

+51
-3
lines changed

4 files changed

+51
-3
lines changed

stdlib/public/SDK/Foundation/Data.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
8181
public typealias Base64DecodingOptions = NSData.Base64DecodingOptions
8282

8383
public typealias Index = Int
84-
public typealias Indices = DefaultRandomAccessIndices<Data>
84+
public typealias Indices = CountableRange<Int>
8585

8686
internal var _wrapped : _SwiftNSData
8787

@@ -613,9 +613,9 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
613613
}
614614
}
615615

616-
public subscript(bounds: Range<Index>) -> MutableRandomAccessSlice<Data> {
616+
public subscript(bounds: Range<Index>) -> MutableRangeReplaceableRandomAccessSlice<Data> {
617617
get {
618-
return MutableRandomAccessSlice(base: self, bounds: bounds)
618+
return MutableRangeReplaceableRandomAccessSlice(base: self, bounds: bounds)
619619
}
620620
set {
621621
replaceSubrange(bounds, with: newValue.base)
@@ -642,6 +642,10 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
642642
return i + 1
643643
}
644644

645+
public var indices: CountableRange<Int> {
646+
return startIndex..<endIndex
647+
}
648+
645649
/// An iterator over the contents of the data.
646650
///
647651
/// The iterator will increment byte-by-byte.

stdlib/public/core/RangeReplaceableCollection.swift.gyb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,10 @@ public protocol _RangeReplaceableIndexable : _Indexable {
244244
public protocol RangeReplaceableCollection
245245
: _RangeReplaceableIndexable, Collection
246246
{
247+
// FIXME(ABI): should require `RangeReplaceableCollection`.
248+
associatedtype SubSequence : _RangeReplaceableIndexable /*: RangeReplaceableCollection*/
249+
= RangeReplaceableSlice<Self>
250+
247251
//===--- Fundamental Requirements ---------------------------------------===//
248252

249253
/// Creates a new, empty collection.
@@ -540,6 +544,9 @@ public protocol RangeReplaceableCollection
540544
//===----------------------------------------------------------------------===//
541545

542546
extension RangeReplaceableCollection {
547+
public subscript(bounds: Range<Index>) -> RangeReplaceableSlice<Self> {
548+
return RangeReplaceableSlice(base: self, bounds: bounds)
549+
}
543550

544551
/// Creates a new collection containing the specified number of a single,
545552
/// repeated value.

validation-test/stdlib/CollectionDiagnostics.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,27 @@ struct BadBidirectionalIndexable : BidirectionalIndexable {
116116
// expected-error@+1 {{'index(after:)' has different argument names from those required by protocol '_BidirectionalIndexable' ('index(before:)'}}
117117
func index(after i: Int) -> Int { return 0 }
118118
}
119+
120+
//
121+
// Check that RangeReplaceableCollection.SubSequence is defaulted.
122+
//
123+
124+
struct RangeReplaceableCollection_SubSequence_IsDefaulted : RangeReplaceableCollection {
125+
var startIndex: Int { fatalError() }
126+
var endIndex: Int { fatalError() }
127+
128+
subscript(pos: Int) -> Int { return 0 }
129+
130+
func index(after: Int) -> Int { fatalError() }
131+
func index(before: Int) -> Int { fatalError() }
132+
func index(_: Int, offsetBy: Int) -> Int { fatalError() }
133+
func distance(from: Int, to: Int) -> Int { fatalError() }
134+
135+
mutating func replaceSubrange<C>(
136+
_ subrange: Range<Int>,
137+
with newElements: C
138+
) where C : Collection, C.Iterator.Element == Int {
139+
fatalError()
140+
}
141+
}
142+

validation-test/stdlib/Data.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,17 @@ DataTestSuite.test("Data.Iterator semantics") {
2929
}
3030
checkSequence((0..<65535).lazy.map({ UInt8($0 % 23) }), data)
3131
}
32+
33+
DataTestSuite.test("associated types") {
34+
typealias Subject = Data
35+
expectRandomAccessCollectionAssociatedTypes(
36+
collectionType: Subject.self,
37+
iteratorType: Data.Iterator.self,
38+
subSequenceType: MutableRangeReplaceableRandomAccessSlice<Subject>.self,
39+
indexType: Int.self,
40+
indexDistanceType: Int.self,
41+
indicesType: CountableRange<Int>.self)
42+
}
43+
44+
3245
runAllTests()

0 commit comments

Comments
 (0)