Skip to content

stdlib: make RangeReplaceableCollection.SubSequence a RangeReplaceableCollection #4825

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions stdlib/public/SDK/Foundation/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
public typealias Base64DecodingOptions = NSData.Base64DecodingOptions

public typealias Index = Int
public typealias Indices = DefaultRandomAccessIndices<Data>
public typealias Indices = CountableRange<Int>

internal var _wrapped : _SwiftNSData

Expand Down Expand Up @@ -613,9 +613,9 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
}
}

public subscript(bounds: Range<Index>) -> MutableRandomAccessSlice<Data> {
public subscript(bounds: Range<Index>) -> MutableRangeReplaceableRandomAccessSlice<Data> {
get {
return MutableRandomAccessSlice(base: self, bounds: bounds)
return MutableRangeReplaceableRandomAccessSlice(base: self, bounds: bounds)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have an additional test case so we can verify this is the right type in the future?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The newly-added protocol constraint ensures that the slice type conforms to RangeReplaceableCollection. I'll try to add a direct test, but I don't have access to a macOS machine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, you can use Data as Data's slice type (I was just making a minimal change). It would make sense if constructing a Data pointing to other Data's storage is O(1) (I don't know if it is).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the tests.

}
set {
replaceSubrange(bounds, with: newValue.base)
Expand All @@ -642,6 +642,10 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
return i + 1
}

public var indices: CountableRange<Int> {
return startIndex..<endIndex
}

/// An iterator over the contents of the data.
///
/// The iterator will increment byte-by-byte.
Expand Down
7 changes: 7 additions & 0 deletions stdlib/public/core/RangeReplaceableCollection.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ public protocol _RangeReplaceableIndexable : _Indexable {
public protocol RangeReplaceableCollection
: _RangeReplaceableIndexable, Collection
{
// FIXME(ABI): should require `RangeReplaceableCollection`.
associatedtype SubSequence : _RangeReplaceableIndexable /*: RangeReplaceableCollection*/
= RangeReplaceableSlice<Self>

//===--- Fundamental Requirements ---------------------------------------===//

/// Creates a new, empty collection.
Expand Down Expand Up @@ -540,6 +544,9 @@ public protocol RangeReplaceableCollection
//===----------------------------------------------------------------------===//

extension RangeReplaceableCollection {
public subscript(bounds: Range<Index>) -> RangeReplaceableSlice<Self> {
return RangeReplaceableSlice(base: self, bounds: bounds)
}

/// Creates a new collection containing the specified number of a single,
/// repeated value.
Expand Down
24 changes: 24 additions & 0 deletions validation-test/stdlib/CollectionDiagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,27 @@ struct BadBidirectionalIndexable : BidirectionalIndexable {
// expected-error@+1 {{'index(after:)' has different argument names from those required by protocol '_BidirectionalIndexable' ('index(before:)'}}
func index(after i: Int) -> Int { return 0 }
}

//
// Check that RangeReplaceableCollection.SubSequence is defaulted.
//

struct RangeReplaceableCollection_SubSequence_IsDefaulted : RangeReplaceableCollection {
var startIndex: Int { fatalError() }
var endIndex: Int { fatalError() }

subscript(pos: Int) -> Int { return 0 }

func index(after: Int) -> Int { fatalError() }
func index(before: Int) -> Int { fatalError() }
func index(_: Int, offsetBy: Int) -> Int { fatalError() }
func distance(from: Int, to: Int) -> Int { fatalError() }

mutating func replaceSubrange<C>(
_ subrange: Range<Int>,
with newElements: C
) where C : Collection, C.Iterator.Element == Int {
fatalError()
}
}

13 changes: 13 additions & 0 deletions validation-test/stdlib/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,17 @@ DataTestSuite.test("Data.Iterator semantics") {
}
checkSequence((0..<65535).lazy.map({ UInt8($0 % 23) }), data)
}

DataTestSuite.test("associated types") {
typealias Subject = Data
expectRandomAccessCollectionAssociatedTypes(
collectionType: Subject.self,
iteratorType: Data.Iterator.self,
subSequenceType: MutableRangeReplaceableRandomAccessSlice<Subject>.self,
indexType: Int.self,
indexDistanceType: Int.self,
indicesType: CountableRange<Int>.self)
}


runAllTests()