From a2cf2304009618f4b46d0f28fef4f7cb8089cbd0 Mon Sep 17 00:00:00 2001 From: Nate Cook Date: Wed, 25 Mar 2026 12:24:48 -0500 Subject: [PATCH] Align BorrowingSequence etc with proposal This updates the ContainersPreview module to use the same API as the proposed BorrowingSequence protocol and its supporting types. In particular, instead of using Span as the iterator for itself and other contiguously-stored types, this introduces SpanIterator. --- .../RigidArray/RigidArray+Container.swift | 4 +- .../Protocols/BorrowingIteratorProtocol.swift | 40 +---------- .../BorrowingSequence+SpanIterator.swift | 61 ++++++++++++++++ ...rowingSequence+Standard Conformances.swift | 71 ++++++++++++------- 4 files changed, 112 insertions(+), 64 deletions(-) create mode 100644 Sources/ContainersPreview/Protocols/BorrowingSequence+SpanIterator.swift diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Container.swift b/Sources/BasicContainers/RigidArray/RigidArray+Container.swift index ca6f0a0e..c5b4e9ca 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+Container.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+Container.swift @@ -21,7 +21,7 @@ import ContainersPreview #if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW @available(SwiftStdlib 5.0, *) extension RigidArray: BorrowingSequence where Element: ~Copyable { - public typealias BorrowingIterator = Span.BorrowingIterator + public typealias BorrowingIterator = SpanIterator @inlinable public var underestimatedCount: Int { count } @@ -29,7 +29,7 @@ extension RigidArray: BorrowingSequence where Element: ~Copyable { @_alwaysEmitIntoClient @inline(__always) public func makeBorrowingIterator() -> BorrowingIterator { - self.span.makeBorrowingIterator() + SpanIterator(self.span) } } #endif diff --git a/Sources/ContainersPreview/Protocols/BorrowingIteratorProtocol.swift b/Sources/ContainersPreview/Protocols/BorrowingIteratorProtocol.swift index 1e7a6050..e27119fd 100644 --- a/Sources/ContainersPreview/Protocols/BorrowingIteratorProtocol.swift +++ b/Sources/ContainersPreview/Protocols/BorrowingIteratorProtocol.swift @@ -96,7 +96,7 @@ public protocol BorrowingIteratorProtocol: ~Copyable, ~Escapable { } @available(SwiftStdlib 5.0, *) -extension BorrowingIteratorProtocol where Self: ~Copyable & ~Escapable { +extension BorrowingIteratorProtocol where Self: ~Copyable & ~Escapable, Element: ~Copyable { @_lifetime(&self) @_lifetime(self: copy self) @_transparent @@ -106,7 +106,7 @@ extension BorrowingIteratorProtocol where Self: ~Copyable & ~Escapable { } @available(SwiftStdlib 5.0, *) -extension BorrowingIteratorProtocol where Self: ~Copyable & ~Escapable { +extension BorrowingIteratorProtocol where Self: ~Copyable & ~Escapable, Element: ~Copyable { @_lifetime(self: copy self) @inlinable public mutating func skip(by offset: Int) -> Int { @@ -159,40 +159,4 @@ where Self: ~Copyable & ~Escapable, Element: Copyable } } -@available(SwiftStdlib 5.0, *) -extension Span: BorrowingIteratorProtocol where Element: ~Copyable { - @_lifetime(copy self) - @_lifetime(self: copy self) - @inlinable - public mutating func nextSpan(maximumCount: Int) -> Span { - _trim(first: maximumCount) - } -} - -// FIXME: Decide if we want UnsafeBufferPointer types to be borrow iterators - -#if false // FIXME: Implement this -@available(SwiftStdlib 5.0, *) -extension MutableSpan: BorrowingIteratorProtocol where Element: ~Copyable { - @_lifetime(copy self) - @_lifetime(self: copy self) - @inlinable - public mutating func nextSpan(maximumCount: Int) -> Span { - //??? - } -} -#endif - -#if false // FIXME: Implement this -@available(SwiftStdlib 5.0, *) -extension OutputSpan: BorrowingIteratorProtocol where Element: ~Copyable { - @_lifetime(copy self) - @_lifetime(self: copy self) - @inlinable - public mutating func nextSpan(maximumCount: Int) -> Span { - //??? - } -} -#endif - #endif diff --git a/Sources/ContainersPreview/Protocols/BorrowingSequence+SpanIterator.swift b/Sources/ContainersPreview/Protocols/BorrowingSequence+SpanIterator.swift new file mode 100644 index 00000000..095f60d8 --- /dev/null +++ b/Sources/ContainersPreview/Protocols/BorrowingSequence+SpanIterator.swift @@ -0,0 +1,61 @@ +//===----------------------------------------------------------------------===// +// +// 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.2) && COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW + +/// A borrowing iterator that provides access to the elements of a single +/// span through a `BorrowingIteratorProtocol` interface. +@available(SwiftStdlib 5.0, *) +public struct SpanIterator: BorrowingIteratorProtocol, ~Copyable, ~Escapable + where Element: ~Copyable +{ + @usableFromInline + internal var _span: Span + @usableFromInline + internal var _start: Int + @usableFromInline + internal var _count: Int + + @_lifetime(copy elements) + public init(_ elements: Span) { + _span = elements + _start = 0 + _count = elements.count + } + + @_alwaysEmitIntoClient + @_lifetime(&self) + @_lifetime(self: copy self) + @_transparent + public mutating func nextSpan(maximumCount: Int) -> Span { + let c = Swift.min(maximumCount, _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 diff --git a/Sources/ContainersPreview/Protocols/BorrowingSequence+Standard Conformances.swift b/Sources/ContainersPreview/Protocols/BorrowingSequence+Standard Conformances.swift index 5febd035..b2a953a8 100644 --- a/Sources/ContainersPreview/Protocols/BorrowingSequence+Standard Conformances.swift +++ b/Sources/ContainersPreview/Protocols/BorrowingSequence+Standard Conformances.swift @@ -15,62 +15,85 @@ @available(SwiftStdlib 5.0, *) extension Span: BorrowingSequence where Element: ~Copyable { - // FIXME: This simple definition cannot also be a backward (or bidirectional) - // iterator, nor a random-access iterator. If we want to go in that direction, - // we'll need to rather introduce a type more like `RigidArray.BorrowingIterator`. - public typealias BorrowingIterator = Self - - @_lifetime(copy self) + public typealias BorrowingIterator = SpanIterator + @inlinable - public func makeBorrowingIterator() -> Span { - self + public var underestimatedCount: Int { count } + + @_lifetime(borrow self) + @inlinable + public func makeBorrowingIterator() -> SpanIterator { + SpanIterator(self) } } @available(SwiftStdlib 5.0, *) extension MutableSpan: BorrowingSequence where Element: ~Copyable { - public typealias BorrowingIterator = Span.BorrowingIterator - + public typealias BorrowingIterator = SpanIterator + + @inlinable + public var underestimatedCount: Int { count } + @_lifetime(borrow self) @inlinable - public func makeBorrowingIterator() -> Span { - self.span + public func makeBorrowingIterator() -> BorrowingIterator { + SpanIterator(self.span) } } @available(SwiftStdlib 5.0, *) extension OutputSpan: BorrowingSequence where Element: ~Copyable { - public typealias BorrowingIterator = Span.BorrowingIterator - + public typealias BorrowingIterator = SpanIterator + + @inlinable + public var underestimatedCount: Int { count } + @_lifetime(borrow self) @inlinable - public func makeBorrowingIterator() -> Span { - self.span + public func makeBorrowingIterator() -> BorrowingIterator { + SpanIterator(self.span) } } @available(SwiftStdlib 5.0, *) extension InputSpan: BorrowingSequence where Element: ~Copyable { - public typealias BorrowingIterator = Span.BorrowingIterator - + public typealias BorrowingIterator = SpanIterator + + @inlinable + public var underestimatedCount: Int { count } + @_lifetime(borrow self) @inlinable - public func makeBorrowingIterator() -> Span { - self.span + public func makeBorrowingIterator() -> BorrowingIterator { + SpanIterator(self.span) } } @available(SwiftStdlib 6.2, *) extension Array: BorrowingSequence { - public typealias BorrowingIterator = Span.BorrowingIterator - + public typealias BorrowingIterator = SpanIterator + @inlinable public var underestimatedCount: Int { count } @_lifetime(borrow self) @inlinable - public func makeBorrowingIterator() -> Span { - self.span + public func makeBorrowingIterator() -> BorrowingIterator { + SpanIterator(self.span) + } +} + +@available(SwiftStdlib 6.2, *) +extension InlineArray: BorrowingSequence where Element: ~Copyable { + public typealias BorrowingIterator = SpanIterator + + @inlinable + public var underestimatedCount: Int { count } + + @_lifetime(borrow self) + @inlinable + public func makeBorrowingIterator() -> BorrowingIterator { + SpanIterator(self.span) } }