From bdcb82de520f3927eec9fa1d2d2f483f8f2c39e1 Mon Sep 17 00:00:00 2001 From: Nate Cook Date: Mon, 23 Oct 2017 11:44:35 -0500 Subject: [PATCH] [stdlib] Limit ranged subscript default implementations This change limits the default implementations for subscript(Range) to collections where the SubSequence type matches the slice type that the implementation provides. This cuts down on the number of extraneous subscript overloads. --- stdlib/public/core/MutableCollection.swift | 44 +++++++++++-------- .../core/RangeReplaceableCollection.swift.gyb | 7 --- 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/stdlib/public/core/MutableCollection.swift b/stdlib/public/core/MutableCollection.swift index 15860eccc24fc..a9a33494bdc4b 100644 --- a/stdlib/public/core/MutableCollection.swift +++ b/stdlib/public/core/MutableCollection.swift @@ -193,7 +193,26 @@ extension MutableCollection { ) rethrows -> R? { return nil } + + /// Exchanges the values at the specified indices of the collection. + /// + /// Both parameters must be valid indices of the collection that are not + /// equal to `endIndex`. Calling `swapAt(_:_:)` with the same index as both + /// `i` and `j` has no effect. + /// + /// - Parameters: + /// - i: The index of the first value to swap. + /// - j: The index of the second value to swap. + @_inlineable + public mutating func swapAt(_ i: Index, _ j: Index) { + guard i != j else { return } + let tmp = self[i] + self[i] = self[j] + self[j] = tmp + } +} +extension MutableCollection where SubSequence == MutableSlice { /// Accesses a contiguous subrange of the collection's elements. /// /// The accessed slice uses the same indices for the same elements as the @@ -226,26 +245,11 @@ extension MutableCollection { _writeBackMutableSlice(&self, bounds: bounds, slice: newValue) } } - - /// Exchanges the values at the specified indices of the collection. - /// - /// Both parameters must be valid indices of the collection that are not - /// equal to `endIndex`. Calling `swapAt(_:_:)` with the same index as both - /// `i` and `j` has no effect. - /// - /// - Parameters: - /// - i: The index of the first value to swap. - /// - j: The index of the second value to swap. - @_inlineable - public mutating func swapAt(_ i: Index, _ j: Index) { - guard i != j else { return } - let tmp = self[i] - self[i] = self[j] - self[j] = tmp - } } -extension MutableCollection where Self: BidirectionalCollection { +extension MutableCollection + where SubSequence == MutableBidirectionalSlice +{ @_inlineable // FIXME(sil-serialize-all) public subscript(bounds: Range) -> MutableBidirectionalSlice { get { @@ -258,7 +262,9 @@ extension MutableCollection where Self: BidirectionalCollection { } } -extension MutableCollection where Self: RandomAccessCollection { +extension MutableCollection + where SubSequence == MutableRandomAccessSlice +{ @_inlineable // FIXME(sil-serialize-all) public subscript(bounds: Range) -> MutableRandomAccessSlice { get { diff --git a/stdlib/public/core/RangeReplaceableCollection.swift.gyb b/stdlib/public/core/RangeReplaceableCollection.swift.gyb index eda9d20187bdd..f37740037eb6a 100644 --- a/stdlib/public/core/RangeReplaceableCollection.swift.gyb +++ b/stdlib/public/core/RangeReplaceableCollection.swift.gyb @@ -362,11 +362,6 @@ public protocol RangeReplaceableCollection : Collection //===----------------------------------------------------------------------===// extension RangeReplaceableCollection { - @_inlineable - public subscript(bounds: Range) -> RangeReplaceableSlice { - return RangeReplaceableSlice(base: self, bounds: bounds) - } - /// Creates a new collection containing the specified number of a single, /// repeated value. /// @@ -657,7 +652,6 @@ extension RangeReplaceableCollection { // collections. % for capability in ['', 'Bidirectional', 'RandomAccess']: -% if capability: extension RangeReplaceableCollection where Self.SubSequence == RangeReplaceable${capability}Slice { @_inlineable // FIXME(sil-serialize-all) @@ -666,7 +660,6 @@ extension RangeReplaceableCollection where return RangeReplaceable${capability}Slice(base: self, bounds: bounds) } } -% end extension RangeReplaceableCollection where Self.SubSequence == MutableRangeReplaceable${capability}Slice