Skip to content

Commit 52cf9e5

Browse files
author
Dave Abrahams
authored
Merge pull request #9811 from palimondo/struct-the-class
[stdlib] Sequence internals with struct instead of class.
2 parents a6e1734 + b57aa7c commit 52cf9e5

File tree

1 file changed

+16
-25
lines changed

1 file changed

+16
-25
lines changed

stdlib/public/core/Sequence.swift

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ public protocol Sequence {
499499
/// - Returns: A subsequence starting at the beginning of this sequence
500500
/// with at most `maxLength` elements.
501501
func prefix(_ maxLength: Int) -> SubSequence
502-
502+
503503
/// Returns a subsequence containing the initial, consecutive elements that
504504
/// satisfy the given predicate.
505505
///
@@ -615,7 +615,7 @@ public protocol Sequence {
615615
/// in the same order.
616616
func _copyToContiguousArray() -> ContiguousArray<Element>
617617

618-
/// Copy `self` into an unsafe buffer, returning a partially-consumed
618+
/// Copy `self` into an unsafe buffer, returning a partially-consumed
619619
/// iterator with any elements that didn't fit remaining.
620620
func _copyContents(
621621
initializing ptr: UnsafeMutableBufferPointer<Element>
@@ -637,12 +637,9 @@ extension Sequence where Self.Iterator == Self {
637637
/// `Base` iterator before possibly returning the first available element.
638638
///
639639
/// The underlying iterator's sequence may be infinite.
640-
///
641-
/// This is a class - we require reference semantics to keep track
642-
/// of how many elements we've already dropped from the underlying sequence.
643640
@_versioned
644641
@_fixed_layout
645-
internal class _DropFirstSequence<Base : IteratorProtocol>
642+
internal struct _DropFirstSequence<Base : IteratorProtocol>
646643
: Sequence, IteratorProtocol {
647644

648645
@_versioned
@@ -668,7 +665,7 @@ internal class _DropFirstSequence<Base : IteratorProtocol>
668665

669666
@_versioned
670667
@_inlineable
671-
internal func next() -> Base.Element? {
668+
internal mutating func next() -> Base.Element? {
672669
while _dropped < _limit {
673670
if _iterator.next() == nil {
674671
_dropped = _limit
@@ -696,13 +693,10 @@ internal class _DropFirstSequence<Base : IteratorProtocol>
696693
/// A sequence that only consumes up to `n` elements from an underlying
697694
/// `Base` iterator.
698695
///
699-
/// The underlying iterator's sequence may be infinite.
700-
///
701-
/// This is a class - we require reference semantics to keep track
702-
/// of how many elements we've already taken from the underlying sequence.
696+
/// The underlying iterator's sequence may be infinite
703697
@_fixed_layout
704698
@_versioned
705-
internal class _PrefixSequence<Base : IteratorProtocol>
699+
internal struct _PrefixSequence<Base : IteratorProtocol>
706700
: Sequence, IteratorProtocol {
707701
@_versioned
708702
internal let _maxLength: Int
@@ -727,7 +721,7 @@ internal class _PrefixSequence<Base : IteratorProtocol>
727721

728722
@_versioned
729723
@_inlineable
730-
internal func next() -> Base.Element? {
724+
internal mutating func next() -> Base.Element? {
731725
if _taken >= _maxLength { return nil }
732726
_taken += 1
733727

@@ -754,14 +748,11 @@ internal class _PrefixSequence<Base : IteratorProtocol>
754748
/// `Base` iterator before possibly returning the first available element.
755749
///
756750
/// The underlying iterator's sequence may be infinite.
757-
///
758-
/// This is a class - we require reference semantics to keep track
759-
/// of how many elements we've already dropped from the underlying sequence.
760751
@_fixed_layout
761752
@_versioned
762-
internal class _DropWhileSequence<Base : IteratorProtocol>
753+
internal struct _DropWhileSequence<Base : IteratorProtocol>
763754
: Sequence, IteratorProtocol {
764-
755+
765756
typealias Element = Base.Element
766757

767758
@_versioned
@@ -778,7 +769,7 @@ internal class _DropWhileSequence<Base : IteratorProtocol>
778769
) rethrows {
779770
self._iterator = iterator
780771
self._nextElement = nextElement ?? _iterator.next()
781-
772+
782773
while try _nextElement.flatMap(predicate) == true {
783774
_nextElement = _iterator.next()
784775
}
@@ -792,11 +783,11 @@ internal class _DropWhileSequence<Base : IteratorProtocol>
792783

793784
@_versioned
794785
@_inlineable
795-
internal func next() -> Element? {
786+
internal mutating func next() -> Element? {
796787
guard _nextElement != nil else {
797788
return _iterator.next()
798789
}
799-
790+
800791
let next = _nextElement
801792
_nextElement = nil
802793
return next
@@ -973,7 +964,7 @@ extension Sequence {
973964
///
974965
/// print(
975966
/// line.split(
976-
/// omittingEmptySubsequences: false,
967+
/// omittingEmptySubsequences: false,
977968
/// whereSeparator: { $0 == " " }
978969
/// ).map(String.init))
979970
/// // Prints "["BLANCHE:", "", "", "I", "don\'t", "want", "realism.", "I", "want", "magic!"]"
@@ -1277,7 +1268,7 @@ extension Sequence where
12771268
}
12781269
return AnySequence(result)
12791270
}
1280-
1271+
12811272
/// Returns a subsequence by skipping the initial, consecutive elements that
12821273
/// satisfy the given predicate.
12831274
///
@@ -1337,7 +1328,7 @@ extension Sequence where
13371328
return AnySequence(
13381329
_PrefixSequence(_iterator: makeIterator(), maxLength: maxLength))
13391330
}
1340-
1331+
13411332
/// Returns a subsequence containing the initial, consecutive elements that
13421333
/// satisfy the given predicate.
13431334
///
@@ -1403,7 +1394,7 @@ extension Sequence {
14031394
/// Returns a subsequence containing all but the last element of the
14041395
/// sequence.
14051396
///
1406-
/// The sequence must be finite.
1397+
/// The sequence must be finite.
14071398
///
14081399
/// let numbers = [1, 2, 3, 4, 5]
14091400
/// print(numbers.dropLast())

0 commit comments

Comments
 (0)