Skip to content

Commit 069efee

Browse files
committed
stdlib: SE-0065: change UnsafeBufferPointer.Indices to CountableRange<Int>
CountableRange is cheaper than DefaultRandomAccessIndices.
1 parent 42d9c81 commit 069efee

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

stdlib/private/StdlibUnittest/StdlibUnittest.swift.gyb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,9 @@ public func expectSequenceType<
329329
X.SubSequence.SubSequence == X.SubSequence
330330
>(_ x: X) -> X { return x }
331331

332-
public func expectIndexable<X : Indexable>(_ x: X) -> X { return x }
333-
public func expectCollectionType<
334-
X : Collection
332+
% for Mutable in ['', 'Mutable']:
333+
public func expect${Mutable}CollectionType<
334+
X : ${Mutable}Collection
335335
where
336336

337337
// FIXME(ABI)(compiler limitation): there should be no constraints in
@@ -345,7 +345,8 @@ public func expectCollectionType<
345345
X.Indices.Iterator.Element == X.Index,
346346
X.Indices.Index == X.Index,
347347
X.Indices.SubSequence == X.Indices
348-
>(_ x: X) -> X { return x }
348+
>(_ x: X.Type) {}
349+
% end
349350

350351
/// A slice is a `Collection` that when sliced returns an instance of
351352
/// itself.

stdlib/public/core/UnsafeBufferPointer.swift.gyb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ public struct Unsafe${Mutable}BufferPointer<Element>
5757
return count
5858
}
5959

60-
public typealias Indices = DefaultRandomAccessIndices<Unsafe${Mutable}BufferPointer<Element>>
60+
public typealias Indices = CountableRange<Int>
61+
62+
public var indices: Indices {
63+
return startIndex..<endIndex
64+
}
6165

6266
/// Access the `i`th element in the buffer.
6367
public subscript(i: Int) -> Element {

test/1_stdlib/UnsafeBufferPointer.swift.gyb renamed to validation-test/stdlib/UnsafeBufferPointer.swift.gyb

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,28 @@
44
// REQUIRES: executable_test
55

66
import StdlibUnittest
7+
import StdlibCollectionUnittest
78

89
var UnsafeBufferPointerTestSuite = TestSuite("UnsafeBufferPointer")
910
var UnsafeMutableBufferPointerTestSuite = TestSuite("UnsafeMutableBufferPointer")
1011

11-
% for (SelfName, SelfType, PointerType) in [
12-
% ('UnsafeBufferPointer', 'UnsafeBufferPointer<Float>', 'UnsafePointer<Float>'),
13-
% ('UnsafeMutableBufferPointer', 'UnsafeMutableBufferPointer<Float>', 'UnsafeMutablePointer<Float>')]:
12+
% for (SelfName, IsMutable, SelfType, PointerType) in [
13+
% ('UnsafeBufferPointer', False, 'UnsafeBufferPointer<Float>', 'UnsafePointer<Float>'),
14+
% ('UnsafeMutableBufferPointer', True, 'UnsafeMutableBufferPointer<Float>', 'UnsafeMutablePointer<Float>')
15+
% ]:
16+
17+
${SelfName}TestSuite.test("AssociatedTypes") {
18+
typealias Subject = ${SelfName}<OpaqueValue<Int>>
19+
expectRandomAccessCollectionAssociatedTypes(
20+
collectionType: Subject.self,
21+
iteratorType: IndexingIterator<Subject>.self,
22+
subSequenceType: ${'Mutable' if IsMutable else ''}RandomAccessSlice<Subject>.self,
23+
indexType: Int.self,
24+
indexDistanceType: Int.self,
25+
indicesType: CountableRange<Int>.self)
26+
27+
expect${'Mutable' if IsMutable else ''}CollectionType(Subject.self)
28+
}
1429

1530
${SelfName}TestSuite.test("nilBaseAddress") {
1631
let emptyBuffer = ${SelfType}(start: nil, count: 0)

0 commit comments

Comments
 (0)