Skip to content

Commit 09c0b60

Browse files
committed
Merge pull request #2929 from ultramiraculous/slice-base
SE-0093 - Add public base property to slices
2 parents 072af72 + d08cbd6 commit 09c0b60

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

stdlib/public/core/Slice.swift.gyb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,11 @@ public struct ${Self}<Base : ${BaseRequirements}>
435435
% else:
436436
internal let _base: Base
437437
% end
438+
439+
/// The underlying collection of the slice.
440+
public var base: Base {
441+
return _base
442+
}
438443
}
439444

440445
% end

validation-test/stdlib/Slice.swift.gyb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,32 @@ import StdlibCollectionUnittest
1818
import SwiftPrivate
1919

2020

21+
/// Reference-type collection for testing the `base` property.
22+
class ReferenceCollection : RandomAccessCollection {
23+
typealias Index = Int
24+
25+
var startIndex: Int {
26+
return 0
27+
}
28+
29+
var endIndex: Int {
30+
return 1
31+
}
32+
33+
subscript(index: Int) -> String {
34+
return ""
35+
}
36+
37+
func index(after i: Int) -> Int {
38+
return 1
39+
}
40+
41+
func index(before i: Int) -> Int {
42+
return 0
43+
}
44+
}
45+
46+
2147
var SliceTests = TestSuite("Collection")
2248

2349
//===----------------------------------------------------------------------===//
@@ -69,6 +95,14 @@ SliceTests.test("${Slice}/init(base:bounds:)") {
6995
}
7096
}
7197

98+
% if RangeReplaceable == False and Mutable == False:
99+
SliceTests.test("${Slice}/baseProperty") {
100+
let referenceCollection = ReferenceCollection()
101+
let testSlice = ${Slice}(base: referenceCollection, bounds: 0..<1)
102+
expectTrue(testSlice.base === referenceCollection)
103+
}
104+
% end
105+
72106
SliceTests.test("${Slice}.{startIndex,endIndex}") {
73107
for test in subscriptRangeTests {
74108
let c = ${Collection}(elements: test.collection)

0 commit comments

Comments
 (0)