Skip to content

Commit 8f582ad

Browse files
committed
No really fix existentials I mean it
1 parent acb5399 commit 8f582ad

File tree

3 files changed

+58
-59
lines changed

3 files changed

+58
-59
lines changed

stdlib/public/core/ExistentialCollection.swift.gyb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,10 +492,20 @@ internal final class _${Kind}Box<S : ${Kind}> : _Any${Kind}Box<S.Element>
492492
return _SequenceBox<DropFirstSequence<S>>(_base: _base.dropFirst(n))
493493
}
494494
@inlinable
495+
internal override func _drop(
496+
while predicate: (Element) throws -> Bool
497+
) rethrows -> _Any${Kind}Box<Element> {
498+
return try _SequenceBox<DropWhileSequence<S>>(_base: _base.drop(while: predicate))
499+
}
500+
@inlinable
495501
internal override func _dropLast(_ n: Int) -> [Element] {
496502
return _base.dropLast(n)
497503
}
498504
@inlinable
505+
internal override func _prefix(_ n: Int) -> _AnySequenceBox<Element> {
506+
return _SequenceBox<PrefixSequence<S>>(_base: _base.prefix(n))
507+
}
508+
@inlinable
499509
internal override func _prefix(
500510
while predicate: (Element) throws -> Bool
501511
) rethrows -> [Element] {
@@ -809,7 +819,7 @@ extension Any${Kind} {
809819
}
810820

811821
@inlinable
812-
public __consuming func prefix(_ maxLength: Int) -> Any${Kind}<Element> {
822+
public __consuming func prefix(_ maxLength: Int = 1) -> Any${Kind}<Element> {
813823
return Any${Kind}(_box: _box._prefix(maxLength))
814824
}
815825

stdlib/public/core/Sequence.swift

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -548,57 +548,75 @@ extension PrefixSequence: Sequence {
548548
}
549549
}
550550

551+
551552
/// A sequence that lazily consumes and drops `n` elements from an underlying
552553
/// `Base` iterator before possibly returning the first available element.
553554
///
554555
/// The underlying iterator's sequence may be infinite.
555556
@_fixed_layout
556-
public struct DropWhileSequence<Base : IteratorProtocol>: Sequence, IteratorProtocol {
557+
public struct DropWhileSequence<Base: Sequence> {
557558
public typealias Element = Base.Element
558-
559+
559560
@usableFromInline
560-
internal var _iterator: Base
561+
internal var _iterator: Base.Iterator
561562
@usableFromInline
562-
internal var _nextElement: Base.Element?
563-
563+
internal var _nextElement: Element?
564+
564565
@inlinable
565-
internal init(
566-
iterator: Base,
567-
nextElement: Base.Element?,
568-
predicate: (Base.Element) throws -> Bool
569-
) rethrows {
570-
self._iterator = iterator
571-
self._nextElement = nextElement ?? _iterator.next()
572-
573-
while try _nextElement.flatMap(predicate) == true {
566+
internal init(iterator: Base.Iterator, predicate: (Element) throws -> Bool) rethrows {
567+
_iterator = iterator
568+
_nextElement = _iterator.next()
569+
570+
while let x = _nextElement, try predicate(x) {
574571
_nextElement = _iterator.next()
575572
}
576573
}
577-
574+
578575
@inlinable
579-
public __consuming func makeIterator() -> DropWhileSequence<Base> {
580-
return self
576+
internal init(_ base: Base, predicate: (Element) throws -> Bool) rethrows {
577+
self = try DropWhileSequence(iterator: base.makeIterator(), predicate: predicate)
581578
}
579+
}
582580

583-
@inlinable
584-
public mutating func next() -> Element? {
585-
guard _nextElement != nil else {
586-
return _iterator.next()
581+
extension DropWhileSequence {
582+
@_fixed_layout
583+
public struct Iterator {
584+
@usableFromInline
585+
internal var _iterator: Base.Iterator
586+
@usableFromInline
587+
internal var _nextElement: Element?
588+
589+
@inlinable
590+
internal init(_ iterator: Base.Iterator, nextElement: Element?) {
591+
_iterator = iterator
592+
_nextElement = nextElement
587593
}
594+
}
595+
}
588596

589-
let next = _nextElement
590-
_nextElement = nil
597+
extension DropWhileSequence.Iterator: IteratorProtocol {
598+
public typealias Element = Base.Element
599+
600+
@inlinable
601+
public mutating func next() -> Element? {
602+
guard let next = _nextElement else { return nil }
603+
_nextElement = _iterator.next()
591604
return next
592605
}
606+
}
593607

608+
extension DropWhileSequence: Sequence {
609+
@inlinable
610+
public func makeIterator() -> Iterator {
611+
return Iterator(_iterator, nextElement: _nextElement)
612+
}
613+
594614
@inlinable
595615
public __consuming func drop(
596616
while predicate: (Element) throws -> Bool
597617
) rethrows -> DropWhileSequence<Base> {
598-
// If this is already a DropWhileSequence, avoid multiple
599-
// layers of wrapping and keep the same iterator.
600-
return try DropWhileSequence(
601-
iterator: _iterator, nextElement: _nextElement, predicate: predicate)
618+
guard let x = _nextElement, try predicate(x) else { return self }
619+
return try DropWhileSequence(iterator: _iterator, predicate: predicate)
602620
}
603621
}
604622

@@ -1093,9 +1111,8 @@ extension Sequence {
10931111
@inlinable
10941112
public __consuming func drop(
10951113
while predicate: (Element) throws -> Bool
1096-
) rethrows -> DropWhileSequence<Iterator> {
1097-
return try DropWhileSequence(
1098-
iterator: makeIterator(), nextElement: nil, predicate: predicate)
1114+
) rethrows -> DropWhileSequence<Self> {
1115+
return try DropWhileSequence(self, predicate: predicate)
10991116
}
11001117

11011118
/// Returns a subsequence, up to the specified maximum length, containing the

validation-test/stdlib/ExistentialCollection.swift.gyb

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -151,34 +151,6 @@ tests.test("${TestedType}: dispatch to wrapped, SequenceLog") {
151151
_ = s.underestimatedCount
152152
}
153153

154-
Log.dropFirst.expectIncrement(Base.self) {
155-
_ = s.dropFirst(0)
156-
}
157-
158-
Log.dropLast.expectIncrement(Base.self) {
159-
_ = s.dropLast(0)
160-
}
161-
162-
Log.dropWhile.expectIncrement(Base.self) {
163-
_ = s.drop { (_) in true }
164-
}
165-
166-
Log.prefixWhile.expectIncrement(Base.self) {
167-
_ = s.prefix { (_) in true }
168-
}
169-
170-
Log.prefixMaxLength.expectIncrement(Base.self) {
171-
_ = s.prefix(0)
172-
}
173-
174-
Log.suffixMaxLength.expectIncrement(Base.self) {
175-
_ = s.suffix(0)
176-
}
177-
178-
Log.split.expectIncrement(Base.self) {
179-
_ = s.split { (_) in true }
180-
}
181-
182154
Log._customContainsEquatableElement.expectIncrement(Base.self) {
183155
_ = s._customContainsEquatableElement(OpaqueValue(42))
184156
}

0 commit comments

Comments
 (0)