Skip to content

SR-7266: Avoid nesting of ReversedCollection #15815

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 3 commits into from
Apr 12, 2018
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
15 changes: 12 additions & 3 deletions stdlib/public/core/Reverse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ extension MutableCollection where Self: BidirectionalCollection {
/// * `c.reversed()` does not create new storage
/// * `c.reversed().map(f)` maps eagerly and returns a new array
/// * `c.lazy.reversed().map(f)` maps lazily and returns a `LazyMapCollection`
///
/// - See also: `ReversedRandomAccessCollection`
@_fixed_layout
public struct ReversedCollection<Base: BidirectionalCollection> {
public let _base: Base
Expand Down Expand Up @@ -84,7 +82,7 @@ extension ReversedCollection {
}
}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

nitpick: Unnecessary space at the end of the line?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

so my change removes the unnecessary space from that line. Or would you rather not have those kind of cleanups in this PR?

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh. I misinterpreted the diff. I would prefer to not mix the cleanups with functional changes, but am guilty of doing it every now and then myself. So let's keep it.

extension ReversedCollection.Iterator: IteratorProtocol, Sequence {
public typealias Element = Base.Element

Expand Down Expand Up @@ -253,6 +251,17 @@ extension ReversedCollection: BidirectionalCollection {

extension ReversedCollection: RandomAccessCollection where Base: RandomAccessCollection { }

extension ReversedCollection {
/// Reversing a reversed collection returns the original collection.
///
/// - Complexity: O(1)
@inlinable
@available(swift, introduced: 4.2)
public func reversed() -> Base {
return _base
}
}

extension BidirectionalCollection {
/// Returns a view presenting the elements of the collection in reverse
/// order.
Expand Down
36 changes: 36 additions & 0 deletions test/stdlib/ReverseCompatibility.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// RUN: %empty-directory(%t)
// RUN: %target-build-swift %s -o %t/a.out3 -swift-version 3 && %target-run %t/a.out3
// RUN: %target-build-swift %s -o %t/a.out4 -swift-version 4 && %target-run %t/a.out4
// RUN: %target-build-swift %s -o %t/a.out5 -swift-version 5 && %target-run %t/a.out5
// REQUIRES: executable_test

import StdlibUnittest

#if swift(>=4.2)
let swiftVersion = ">=4.2"
#else
let swiftVersion = "<4.2"
#endif

let tests = TestSuite("ReverseCompatibility")

tests.test("Double reverse type/Collection/\(swiftVersion)") {
func reverse<C : BidirectionalCollection>(_ xs: C) {
var result = xs.reversed().reversed()
#if swift(>=4.2)
expectType(C.self, &result)
#else
expectType(ReversedCollection<ReversedCollection<C>>.self, &result)
#endif
}
reverse(Array(0..<10))

func backwardCompatible<C : BidirectionalCollection>(_ xs: C) {
typealias ExpectedType = ReversedCollection<ReversedCollection<C>>
var result: ExpectedType = xs.reversed().reversed()
expectType(ExpectedType.self, &result)
}
backwardCompatible(Array(0..<10))
}

runAllTests()