Files
swift-collections/Sources/ContainersPreview/Conformances/Span+Iterable.swift
T
Karoy Lorentey ff695132ed Bump availability of Iterable_ and Container protocols to SwiftStdlib 6.4
This is preparing for switching over to the stdlib’s definition of `protocol Iterable`, assuming it will land with macOS 27/iOS 27 etc availability; `Container` is refining `Iterable`, so it needs matching availability.

If `Iterable` won’t ship in Swift 6.4, then we’ll need to bump this again.
2026-07-07 14:22:45 -07:00

80 lines
2.1 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 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 compiler(>=6.4) && UnstableContainersPreview
@available(SwiftStdlib 6.4, *)
extension Span: Iterable_ where Element: ~Copyable {
public typealias Element_ = Element
@inlinable
public var underestimatedCount_: Int { count }
@inlinable
@_lifetime(borrow self)
public func makeBorrowingIterator_() -> BorrowingIterator_ {
BorrowingIterator_(self)
}
}
@available(SwiftStdlib 5.0, *)
extension Span where Element: ~Copyable {
/// A borrowing iterator that provides access to the elements of a single
/// span through a `BorrowingIteratorProtocol` interface.
@available(SwiftStdlib 6.4, *)
public struct BorrowingIterator_:
BorrowingIteratorProtocol_, ~Copyable, ~Escapable
{
public typealias Element_ = Element
@usableFromInline
internal var _span: Span<Element_>
@usableFromInline
internal var _start: Int
@usableFromInline
internal var _count: Int
@_lifetime(copy elements)
public init(_ elements: Span<Element_>) {
_span = elements
_start = 0
_count = elements.count
}
@_alwaysEmitIntoClient
@_lifetime(&self)
@_lifetime(self: copy self)
@_transparent
public mutating func nextSpan_(maxCount: Int) -> Span<Element_> {
let c = Swift.min(maxCount, _count)
defer {
_start &+= c
_count &-= c
}
return _span.extracting(droppingFirst: _start).extracting(first: c)
}
@_alwaysEmitIntoClient
@_lifetime(self: copy self)
public mutating func skip_(by offset: Int) -> Int {
let c = Swift.min(offset, _count)
defer {
_start &+= c
_count &-= c
}
return c
}
}
}
#endif