Skip to content

Commit 6e7b36a

Browse files
author
Max Moiseev
committed
Conditional conformances for LazyCollection
1 parent a1b1778 commit 6e7b36a

File tree

4 files changed

+23
-39
lines changed

4 files changed

+23
-39
lines changed

stdlib/public/core/LazyCollection.swift.gyb

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,13 @@ extension LazyCollectionProtocol where Elements == Self {
3838
public var elements: Self { return self }
3939
}
4040

41-
% for Traversal in TRAVERSALS:
42-
% TraversalCollection = collectionForTraversal(Traversal)
43-
% Self = 'Lazy' + TraversalCollection
44-
% Slice = TraversalCollection.replace('Collection', 'Slice')
45-
4641
/// A collection containing the same elements as a `Base` collection,
4742
/// but on which some operations such as `map` and `filter` are
4843
/// implemented lazily.
4944
///
5045
/// - See also: `LazySequenceProtocol`, `LazyCollection`
5146
@_fixed_layout
52-
public struct ${Self}<Base : ${TraversalCollection}> : LazyCollectionProtocol {
47+
public struct LazyCollection<Base : Collection> : LazyCollectionProtocol {
5348

5449
/// The type of the underlying collection.
5550
public typealias Elements = Base
@@ -78,7 +73,7 @@ public struct ${Self}<Base : ${TraversalCollection}> : LazyCollectionProtocol {
7873

7974
/// Forward implementations to the base collection, to pick up any
8075
/// optimizations it might implement.
81-
extension ${Self} : Sequence {
76+
extension LazyCollection : Sequence {
8277

8378
public typealias Iterator = Base.Iterator
8479

@@ -118,7 +113,7 @@ extension ${Self} : Sequence {
118113
}
119114
}
120115

121-
extension ${Self} : ${TraversalCollection} {
116+
extension LazyCollection : Collection {
122117
/// The position of the first element in a non-empty collection.
123118
///
124119
/// In an empty collection, `startIndex == endIndex`.
@@ -162,7 +157,7 @@ extension ${Self} : ${TraversalCollection} {
162157
///
163158
/// - Complexity: O(1)
164159
@_inlineable
165-
public subscript(bounds: Range<Index>) -> Slice<${Self}<Base>> {
160+
public subscript(bounds: Range<Index>) -> Slice<LazyCollection<Base>> {
166161
return Slice(base: self, bounds: bounds)
167162
}
168163

@@ -226,8 +221,10 @@ extension ${Self} : ${TraversalCollection} {
226221
return _base.distance(from:start, to: end)
227222
}
228223

229-
% if Traversal != 'Forward':
224+
}
230225

226+
extension LazyCollection : BidirectionalCollection
227+
where Base : BidirectionalCollection {
231228
@_inlineable
232229
public func index(before i: Base.Index) -> Base.Index {
233230
return _base.index(before: i)
@@ -237,23 +234,27 @@ extension ${Self} : ${TraversalCollection} {
237234
public var last: Base.Element? {
238235
return _base.last
239236
}
240-
% end
241237
}
242238

239+
extension LazyCollection : RandomAccessCollection
240+
where Base : RandomAccessCollection {}
241+
243242
/// Augment `self` with lazy methods such as `map`, `filter`, etc.
244-
extension ${TraversalCollection} {
243+
extension Collection {
245244
/// A view onto this collection that provides lazy implementations of
246245
/// normally eager operations, such as `map` and `filter`.
247246
///
248247
/// Use the `lazy` property when chaining operations to prevent
249248
/// intermediate operations from allocating storage, or when you only
250249
/// need a part of the final collection to avoid unnecessary computation.
251250
@_inlineable
252-
public var lazy: ${Self}<Self> {
253-
return ${Self}(_base: self)
251+
public var lazy: LazyCollection<Self> {
252+
return LazyCollection(_base: self)
254253
}
255254
}
256255

256+
% for Traversal in TRAVERSALS:
257+
% TraversalCollection = collectionForTraversal(Traversal)
257258
// Without this specific overload the non-re-wrapping extension on
258259
// LazyCollectionProtocol (below) is not selected for some reason.
259260
extension ${TraversalCollection} where Self : LazyCollectionProtocol {
@@ -263,12 +264,15 @@ extension ${TraversalCollection} where Self : LazyCollectionProtocol {
263264
return self
264265
}
265266
}
266-
267267
% end
268268

269269
extension Slice: LazySequenceProtocol where Base: LazySequenceProtocol { }
270270
extension Slice: LazyCollectionProtocol where Base: LazyCollectionProtocol { }
271271

272+
@available(*, deprecated, renamed: "LazyCollection")
273+
public typealias LazyBidirectionalCollection<T> = LazyCollection<T> where T : BidirectionalCollection
274+
@available(*, deprecated, renamed: "LazyCollection")
275+
public typealias LazyRandomAccessCollection<T> = LazyCollection<T> where T : RandomAccessCollection
272276
// ${'Local Variables'}:
273277
// eval: (read-only-mode 1)
274278
// End:

stdlib/public/core/Reverse.swift

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -312,25 +312,7 @@ extension LazyCollectionProtocol
312312
///
313313
/// - Complexity: O(1)
314314
@_inlineable
315-
public func reversed() -> LazyBidirectionalCollection<
316-
ReversedCollection<Elements>
317-
> {
318-
return ReversedCollection(_base: elements).lazy
319-
}
320-
}
321-
322-
extension LazyCollectionProtocol
323-
where
324-
Self : RandomAccessCollection,
325-
Elements : RandomAccessCollection {
326-
327-
/// Returns the elements of the collection in reverse order.
328-
///
329-
/// - Complexity: O(1)
330-
@_inlineable
331-
public func reversed() -> LazyRandomAccessCollection<
332-
ReversedCollection<Elements>
333-
> {
315+
public func reversed() -> LazyCollection<ReversedCollection<Elements>> {
334316
return ReversedCollection(_base: elements).lazy
335317
}
336318
}

validation-test/stdlib/Lazy.swift.gyb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -988,8 +988,7 @@ tests.test("ReversedCollection/Lazy") {
988988
typealias Base = LazyMapCollection<[Int], Int>
989989
ExpectType<Base>.test(base)
990990

991-
typealias LazyReversedBase = LazyRandomAccessCollection<
992-
ReversedCollection<Base>>
991+
typealias LazyReversedBase = LazyCollection<ReversedCollection<Base>>
993992

994993
let reversed = base.reversed()
995994
ExpectType<LazyReversedBase>.test(reversed)
@@ -1006,8 +1005,7 @@ tests.test("ReversedCollection/Lazy") {
10061005
typealias Base = LazyMapCollection<String, Character>
10071006
ExpectType<Base>.test(base)
10081007

1009-
typealias LazyReversedBase = LazyBidirectionalCollection<
1010-
ReversedCollection<Base>>
1008+
typealias LazyReversedBase = LazyCollection<ReversedCollection<Base>>
10111009

10121010
let reversed = base.reversed()
10131011
ExpectType<LazyReversedBase>.test(reversed)

validation-test/stdlib/Range.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ MiscTestSuite.test("map()") {
784784

785785
MiscTestSuite.test("reversed()") {
786786
var result = (0..<10).lazy.reversed()
787-
typealias Expected = LazyRandomAccessCollection<
787+
typealias Expected = LazyCollection<
788788
ReversedRandomAccessCollection<CountableRange<Int>>>
789789
expectType(Expected.self, &result)
790790
expectEqualSequence(

0 commit comments

Comments
 (0)