diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Container.swift b/Sources/BasicContainers/RigidArray/RigidArray+Container.swift index ab074f8e..4b6c7167 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+Container.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+Container.swift @@ -22,17 +22,40 @@ import ContainersPreview @available(SwiftStdlib 5.0, *) extension RigidArray: Iterable_ where Element: ~Copyable { public typealias BorrowingIterator_ = SpanIterator + @_alwaysEmitIntoClient @inline(__always) public func makeBorrowingIterator_() -> BorrowingIterator_ { - SpanIterator(self.span) + let span = self.span + let it = span.makeBorrowingIterator_() + // FIXME: `it` is borrowing `span`, not self + return _overrideLifetime(it, borrowing: self) } } #endif #if compiler(>=6.4) && UnstableContainersPreview @available(SwiftStdlib 5.0, *) -extension RigidArray: Container where Element: ~Copyable {} +extension RigidArray: Container where Element: ~Copyable { + @_alwaysEmitIntoClient + @_lifetime(borrow self) + public func makeBorrowingIterator( + from start: Index + ) -> BorrowingIterator_ { + let span = self.span + let it = span.makeBorrowingIterator(from: start) + // FIXME: `it` is borrowing `span`, not self + return _overrideLifetime(it, borrowing: self) + } + + @_alwaysEmitIntoClient + public func currentIndex(of iterator: borrowing BorrowingIterator_) -> Index { + precondition( + iterator._span.isTriviallyIdentical(to: self.span), + "Invalid iterator instance") + return iterator._start + } +} @available(SwiftStdlib 5.0, *) extension RigidArray: BidirectionalContainer where Element: ~Copyable {} diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray+Container.swift b/Sources/BasicContainers/UniqueArray/UniqueArray+Container.swift index 2b5e4e87..6d158025 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray+Container.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray+Container.swift @@ -36,7 +36,20 @@ extension UniqueArray: Iterable_ where Element: ~Copyable { #if compiler(>=6.4) && UnstableContainersPreview @available(SwiftStdlib 5.0, *) -extension UniqueArray: Container where Element: ~Copyable {} +extension UniqueArray: Container where Element: ~Copyable { + @_alwaysEmitIntoClient + @_lifetime(borrow self) + public func makeBorrowingIterator( + from start: Index + ) -> BorrowingIterator_ { + _storage.makeBorrowingIterator(from: start) + } + + @_alwaysEmitIntoClient + public func currentIndex(of iterator: borrowing BorrowingIterator_) -> Index { + _storage.currentIndex(of: iterator) + } +} @available(SwiftStdlib 5.0, *) extension UniqueArray: BidirectionalContainer where Element: ~Copyable {} diff --git a/Sources/ContainersPreview/Conformances/InputSpan+Container.swift b/Sources/ContainersPreview/Conformances/InputSpan+Container.swift index 805907ce..24bd2cc4 100644 --- a/Sources/ContainersPreview/Conformances/InputSpan+Container.swift +++ b/Sources/ContainersPreview/Conformances/InputSpan+Container.swift @@ -21,6 +21,22 @@ import InternalCollectionsUtilities extension InputSpan: RandomAccessContainer, MutableContainer where Element: ~Copyable { + @_alwaysEmitIntoClient + @_lifetime(borrow self) + public func makeBorrowingIterator( + from start: Index + ) -> BorrowingIterator_ { + let span = self.span + let it = span.makeBorrowingIterator(from: start) + // FIXME: `it` is borrowing `span`, not self + return _overrideLifetime(it, borrowing: self) + } + + @_alwaysEmitIntoClient + public func currentIndex(of iterator: borrowing BorrowingIterator_) -> Index { + self.span.currentIndex(of: iterator) + } + @_alwaysEmitIntoClient @inline(__always) public var startIndex: Index { 0 } diff --git a/Sources/ContainersPreview/Conformances/MutableSpan+Container.swift b/Sources/ContainersPreview/Conformances/MutableSpan+Container.swift index 4b524e46..52d2723c 100644 --- a/Sources/ContainersPreview/Conformances/MutableSpan+Container.swift +++ b/Sources/ContainersPreview/Conformances/MutableSpan+Container.swift @@ -18,6 +18,27 @@ import InternalCollectionsUtilities #endif extension MutableSpan: RandomAccessContainer where Element: ~Copyable { + @_alwaysEmitIntoClient + @_lifetime(borrow self) + public func makeBorrowingIterator(from start: Int) -> SpanIterator { + var it = SpanIterator(self.span) + var remainder = start + while remainder > 0 { + let d = it.skip_(by: remainder) + precondition(d > 0) + remainder &-= d + } + return it + } + + @_alwaysEmitIntoClient + public func currentIndex(of iterator: borrowing SpanIterator) -> Int { + precondition( + self.span.isTriviallyIdentical(to: iterator._span), + "Invalid iterator") + return iterator._start + } + @_alwaysEmitIntoClient public var startIndex: Int { 0 diff --git a/Sources/ContainersPreview/Conformances/OutputSpan+Container.swift b/Sources/ContainersPreview/Conformances/OutputSpan+Container.swift index d3f5ceb8..2368855b 100644 --- a/Sources/ContainersPreview/Conformances/OutputSpan+Container.swift +++ b/Sources/ContainersPreview/Conformances/OutputSpan+Container.swift @@ -20,6 +20,22 @@ import InternalCollectionsUtilities extension OutputSpan: RandomAccessContainer, MutableContainer where Element: ~Copyable { + @_alwaysEmitIntoClient + @_lifetime(borrow self) + public func makeBorrowingIterator( + from start: Index + ) -> BorrowingIterator_ { + let span = self.span + let it = span.makeBorrowingIterator(from: start) + // FIXME: `it` is borrowing `span`, not self + return _overrideLifetime(it, borrowing: self) + } + + @_alwaysEmitIntoClient + public func currentIndex(of iterator: borrowing BorrowingIterator_) -> Index { + self.span.currentIndex(of: iterator) + } + @_alwaysEmitIntoClient @inline(__always) public var startIndex: Index { 0 } diff --git a/Sources/ContainersPreview/Conformances/Span+Container.swift b/Sources/ContainersPreview/Conformances/Span+Container.swift index 4d5c81d5..9276491d 100644 --- a/Sources/ContainersPreview/Conformances/Span+Container.swift +++ b/Sources/ContainersPreview/Conformances/Span+Container.swift @@ -14,6 +14,30 @@ #if compiler(>=6.4) && UnstableContainersPreview extension Span: RandomAccessContainer where Element: ~Copyable { + @_alwaysEmitIntoClient + @_lifetime(borrow self) + public func makeBorrowingIterator(from start: Int) -> SpanIterator { + precondition(start >= 0 && start <= self.count, "Index out of bounds") + // FIXME: SpanIterator needs to have a direct initializer that takes `start` + var it = SpanIterator(self) + var remainder = start + while remainder > 0 { + let d = it.skip_(by: remainder) + precondition(d > 0) + remainder &-= d + } + return it + } + + @_alwaysEmitIntoClient + public func currentIndex(of iterator: borrowing SpanIterator) -> Int { + // FIXME: SpanIterator needs to have public `base` and `position` properties + precondition( + self.isTriviallyIdentical(to: iterator._span), + "Invalid iterator") + return iterator._start + } + @_alwaysEmitIntoClient public var startIndex: Int { 0 diff --git a/Sources/ContainersPreview/Protocols/BorrowingIteratorProtocol.swift b/Sources/ContainersPreview/Protocols/BorrowingIteratorProtocol.swift index ea33fd18..b8009880 100644 --- a/Sources/ContainersPreview/Protocols/BorrowingIteratorProtocol.swift +++ b/Sources/ContainersPreview/Protocols/BorrowingIteratorProtocol.swift @@ -101,28 +101,28 @@ extension BorrowingIteratorProtocol_ where Self: ~Copyable & ~Escapable, Element_: ~Copyable { @_lifetime(self: copy self) @inlinable - public mutating func skip_(by offset: Int) throws(Failure_) -> Int { - var remainder = offset + public mutating func skip_(by maxDistance: Int) throws(Failure_) -> Int { + var remainder = maxDistance while remainder > 0 { let span = try nextSpan_(maxCount: remainder) if span.isEmpty { break } remainder &-= span.count } - return offset &- remainder + return maxDistance &- remainder } } @available(SwiftStdlib 5.0, *) extension BorrowingIteratorProtocol_ where Self: ~Copyable & ~Escapable { -#if false // FIXME: This doesn't work, but it should? + @available(SwiftStdlib 6.4, *) + @_unsafeNonescapableResult // FIXME: Eep; _overrideLifetime(_:mutating:) doesn't work @_lifetime(&self) @_lifetime(self: copy self) - public mutating func next() -> Ref? { - let span = nextSpan(maxCount: 1) + public mutating func next() throws(Failure_) -> Ref? { + let span = try nextSpan_(maxCount: 1) guard !span.isEmpty else { return nil } - return Ref(_borrowing: span[unchecked: 0]) + return Ref(span[unchecked: 0]) } -#endif } @available(SwiftStdlib 5.0, *) @@ -145,5 +145,4 @@ extension BorrowingIteratorProtocol_ } } - #endif diff --git a/Sources/ContainersPreview/Protocols/Container/Container.swift b/Sources/ContainersPreview/Protocols/Container/Container.swift index dd45a270..ed7be60c 100644 --- a/Sources/ContainersPreview/Protocols/Container/Container.swift +++ b/Sources/ContainersPreview/Protocols/Container/Container.swift @@ -29,6 +29,10 @@ public protocol Container: // somewhere -- `RandomAccessContainer` or `BidirectionalContainer` would be // the obvious candidates. + @_lifetime(borrow self) + func makeBorrowingIterator(from start: Index) -> BorrowingIterator_ + func currentIndex(of iterator: borrowing BorrowingIterator_) -> Index + var isEmpty: Bool { get } var count: Int { get } diff --git a/Sources/ContainersPreview/Protocols/Iterable+Standard Conformances.swift b/Sources/ContainersPreview/Protocols/Iterable+Standard Conformances.swift index 46d5a7a8..6fd17844 100644 --- a/Sources/ContainersPreview/Protocols/Iterable+Standard Conformances.swift +++ b/Sources/ContainersPreview/Protocols/Iterable+Standard Conformances.swift @@ -15,8 +15,8 @@ @available(SwiftStdlib 5.0, *) extension Span: Iterable_ where Element: ~Copyable { - public typealias BorrowingIterator_ = SpanIterator - public typealias Element_ = Element + public typealias BorrowingIterator_ = SpanIterator + public typealias Element_ = Element @inlinable public var underestimatedCount_: Int { count } @@ -31,6 +31,7 @@ extension Span: Iterable_ where Element: ~Copyable { @available(SwiftStdlib 5.0, *) extension MutableSpan: Iterable_ where Element: ~Copyable { public typealias BorrowingIterator_ = SpanIterator + public typealias Element_ = Element @inlinable public var underestimatedCount_: Int { count } diff --git a/Sources/ContainersPreview/Types/InputSpan.swift b/Sources/ContainersPreview/Types/InputSpan.swift index 1c09bd20..1bc52c51 100644 --- a/Sources/ContainersPreview/Types/InputSpan.swift +++ b/Sources/ContainersPreview/Types/InputSpan.swift @@ -465,6 +465,7 @@ extension InputSpan where Element: ~Copyable { /// Borrow the underlying initialized memory for read-only access. @available(SwiftStdlib 5.0, *) @_alwaysEmitIntoClient + @_transparent public var span: Span { @_lifetime(borrow self) borrowing get { diff --git a/Sources/DequeModule/RigidDeque/RigidDeque+Container.swift b/Sources/DequeModule/RigidDeque/RigidDeque+Container.swift index fcd287dc..6377acb5 100644 --- a/Sources/DequeModule/RigidDeque/RigidDeque+Container.swift +++ b/Sources/DequeModule/RigidDeque/RigidDeque+Container.swift @@ -31,7 +31,10 @@ extension RigidDeque where Element: ~Copyable { @usableFromInline internal var _nextSegment: Span - + + @usableFromInline + internal var _position: Int + @_alwaysEmitIntoClient @_lifetime(borrow _deque) internal init(_deque: borrowing RigidDeque) { @@ -43,10 +46,28 @@ extension RigidDeque where Element: ~Copyable { Span( _unsafeElements: segments.second ?? UnsafeBufferPointer._empty), borrowing: _deque) + self._position = 0 } - + @_alwaysEmitIntoClient - @_lifetime(&self) // FIXME: This should be `@_lifetime(copy self)` + @_lifetime(borrow _deque) + internal init( + _deque: borrowing RigidDeque, + from start: Int + ) { + precondition(start >= 0 && start <= _deque.count, "Index out of bounds") + self.init(_deque: _deque) + var remainder = start + while remainder > 0 { + let d = self.skip_(by: remainder) + precondition(d > 0) + remainder &-= d + } + self._position = start + } + + @_alwaysEmitIntoClient + @_lifetime(&self) @_lifetime(self: copy self) public mutating func nextSpan_(maxCount: Int) -> Span { let result = _currentSegment._trim(first: maxCount) @@ -54,6 +75,7 @@ extension RigidDeque where Element: ~Copyable { _currentSegment = _nextSegment _nextSegment = Span() } + _position &+= result.count return result } } @@ -68,7 +90,21 @@ extension RigidDeque where Element: ~Copyable { #if compiler(>=6.4) && UnstableContainersPreview @available(SwiftStdlib 5.0, *) -extension RigidDeque: Container where Element: ~Copyable {} +extension RigidDeque: Container where Element: ~Copyable { + @_alwaysEmitIntoClient + @_lifetime(borrow self) + public func makeBorrowingIterator( + from start: Index + ) -> BorrowingIterator_ { + BorrowingIterator(_deque: self, from: start) + } + + @_alwaysEmitIntoClient + public func currentIndex(of iterator: borrowing BorrowingIterator_) -> Index { + // FIXME: This should validate that the iterator belongs to this deque. + return iterator._position + } +} @available(SwiftStdlib 5.0, *) extension RigidDeque: BidirectionalContainer where Element: ~Copyable {} diff --git a/Sources/DequeModule/UniqueDeque/UniqueDeque+Container.swift b/Sources/DequeModule/UniqueDeque/UniqueDeque+Container.swift index 4b556154..d5527471 100644 --- a/Sources/DequeModule/UniqueDeque/UniqueDeque+Container.swift +++ b/Sources/DequeModule/UniqueDeque/UniqueDeque+Container.swift @@ -34,7 +34,20 @@ extension UniqueDeque where Element: ~Copyable { #if compiler(>=6.4) && UnstableContainersPreview @available(SwiftStdlib 5.0, *) -extension UniqueDeque: Container where Element: ~Copyable {} +extension UniqueDeque: Container where Element: ~Copyable { + @_alwaysEmitIntoClient + @_lifetime(borrow self) + public func makeBorrowingIterator( + from start: Index + ) -> BorrowingIterator_ { + _storage.makeBorrowingIterator(from: start) + } + + @_alwaysEmitIntoClient + public func currentIndex(of iterator: borrowing BorrowingIterator_) -> Index { + _storage.currentIndex(of: iterator) + } +} @available(SwiftStdlib 5.0, *) extension UniqueDeque: BidirectionalContainer where Element: ~Copyable {}