Files
Tolga Unal 68fd4f2cc8 [DequeModule] Make UniqueDeque Iterable without ContainersPreview
842f125c removed the trait conditions around the Iterable APIs, but
UniqueDeque only got its conformance through Container in ContainersPreview.
RigidDeque had no underestimatedCount, so it was 0 through Iterable.
2026-11-05 09:09:48 -05:00

93 lines
2.8 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2025 - 2026 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
// SPDX-License-Identifier: Apache-2.0 WITH Swift-exception
//
//===----------------------------------------------------------------------===//
#if !COLLECTIONS_SINGLE_MODULE
import InternalCollectionsUtilities
#endif
#if compiler(>=6.4)
@available(SwiftStdlib 5.0, *)
extension RigidDeque: Iterable where Element: ~Copyable {
@frozen
@safe
public struct BorrowingIterator: ~Escapable, BorrowingIteratorProtocol {
@usableFromInline
package var _currentSegment: Span<Element>
@usableFromInline
package var _nextSegment: Span<Element>
@usableFromInline
package var _position: Int
@_alwaysEmitIntoClient
@_lifetime(borrow _deque)
package init(_deque: borrowing RigidDeque<Element>) {
let segments = unsafe _deque._handle.segments()
self._currentSegment = unsafe _overrideLifetime(
Span(_unsafeElements: segments.first),
borrowing: _deque)
self._nextSegment = unsafe _overrideLifetime(
Span(
_unsafeElements: segments.second ?? UnsafeBufferPointer._empty),
borrowing: _deque)
self._position = 0
}
@_alwaysEmitIntoClient
@_lifetime(borrow _deque)
package init(
_deque: borrowing RigidDeque<Element>,
from start: Int,
to end: Int
) {
precondition(start >= 0 && start <= _deque.count, "Index out of bounds")
precondition(end >= 0 && end <= _deque.count, "Index out of bounds")
precondition(start <= end, "The start must not be greater than the end")
let segments = unsafe _deque._handle.segments(forOffsets: start ..< end)
self._currentSegment = unsafe _overrideLifetime(
Span(_unsafeElements: segments.first),
borrowing: _deque)
self._nextSegment = unsafe _overrideLifetime(
Span(
_unsafeElements: segments.second ?? UnsafeBufferPointer._empty),
borrowing: _deque)
self._position = start
}
@_alwaysEmitIntoClient
@_lifetime(&self)
@_lifetime(self: copy self)
public mutating func nextSpan(maxCount: Int) -> Span<Element> {
let result = _currentSegment._trim(first: maxCount)
if _currentSegment.isEmpty {
_currentSegment = _nextSegment
_nextSegment = Span()
}
_position &+= result.count
return result
}
}
@inlinable
public var underestimatedCount: Int { count }
@_alwaysEmitIntoClient
@_lifetime(borrow self)
public borrowing func makeBorrowingIterator() -> BorrowingIterator {
BorrowingIterator(_deque: self)
}
}
#endif