Files
swift-collections/Sources/ContainersPreview/Conformances/MutableSpan+Container.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

95 lines
2.8 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2024 - 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
#if !COLLECTIONS_SINGLE_MODULE
import InternalCollectionsUtilities
#endif
@available(SwiftStdlib 6.4, *)
extension MutableSpan: RandomAccessContainer where Element: ~Copyable {
@_alwaysEmitIntoClient
@_lifetime(borrow self)
public func makeBorrowingIterator(
from start: Int
) -> BorrowingIterator_ {
var it = Span<Element>.BorrowingIterator_(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 BorrowingIterator_
) -> Int {
precondition(
self.span.isTriviallyIdentical(to: iterator._span),
"Invalid iterator")
return iterator._start
}
@_alwaysEmitIntoClient
public var startIndex: Int {
0
}
@_alwaysEmitIntoClient
public var endIndex: Int {
count
}
@_alwaysEmitIntoClient
@_lifetime(borrow self)
public func nextSpan(
after index: inout Int, maxCount: Int
) -> Span<Element> {
precondition(index >= 0 && index <= count, "Index out of bounds")
precondition(maxCount > 0, "maxCount must be positive")
let limit = index &+ Swift.min(maxCount, count &- index)
return self.span.extracting(unchecked: Range(uncheckedBounds: (index, limit)))
}
@_alwaysEmitIntoClient
@_lifetime(borrow self)
public func previousSpan(
before index: inout Int, maxCount: Int
) -> Span<Element> {
precondition(index >= 0 && index <= count, "Index out of bounds")
precondition(maxCount > 0, "maxCount must be positive")
let limit = index &- Swift.min(maxCount, index)
return self.span.extracting(unchecked: Range(uncheckedBounds: (limit, index)))
}
}
@available(SwiftStdlib 6.4, *)
extension MutableSpan: MutableContainer where Element: ~Copyable {
@_alwaysEmitIntoClient
@_lifetime(&self)
public mutating func nextMutableSpan(
after index: inout Int, maxCount: Int
) -> MutableSpan<Element> {
precondition(index >= 0 && index <= count, "Index out of bounds")
precondition(maxCount > 0, "maxCount must be positive")
let limit = index &+ Swift.min(maxCount, count &- index)
return self._mutatingExtracting(unchecked: Range(uncheckedBounds: (index, limit)))
}
}
#endif