Use Iterable from the Standard Library, replacing the local definition

This commit is contained in:
Karoy Lorentey committed 2026-07-27 13:32:46 -07:00
1 parent 0afc8e0e16
commit 669eb09cea
68 files changed
+565 -921

No files matched your search

@@ -256,11 +256,11 @@ extension RigidArray {
@available(SwiftStdlib 6.4, *)
@inlinable
internal mutating func _append<
Source: Iterable_ & ~Copyable & ~Escapable
Source: Iterable & ~Copyable & ~Escapable
>(
copying newElements: borrowing Source
) throws(Source.Failure_)
where Source.Element_ == Element {
) throws(Source.Failure)
where Source.Element == Element {
let target = _freeSpace
_count += try newElements._copyContents(intoPrefixOf: target)
}
@@ -280,11 +280,11 @@ extension RigidArray {
@_alwaysEmitIntoClient
@inline(__always)
public mutating func append<
Source: Iterable_ & ~Copyable & ~Escapable
Source: Iterable & ~Copyable & ~Escapable
>(
copying newElements: borrowing Source
) throws(Source.Failure_)
where Source.Element_ == Element {
) throws(Source.Failure)
where Source.Element == Element {
try _append(copying: newElements)
}
#endif
@@ -324,11 +324,11 @@ extension RigidArray {
@_alwaysEmitIntoClient
@inline(__always)
public mutating func append<
Source: Iterable_ & Sequence<Element>
Source: Iterable & Sequence<Element>
>(
copying newElements: Source
) throws(Source.Failure_)
where Source.Element_ == Element {
) throws(Source.Failure)
where Source.Element == Element {
try _append(copying: newElements)
}
#endif
@@ -20,16 +20,13 @@ import ContainersPreview
#if compiler(>=6.4) && UnstableContainersPreview
@available(SwiftStdlib 6.4, *)
extension RigidArray: Iterable_ where Element: ~Copyable {
public typealias BorrowingIterator_ = Span<Element>.BorrowingIterator_
extension RigidArray: Iterable where Element: ~Copyable {
public typealias BorrowingIterator = Span<Element>.BorrowingIterator
@_alwaysEmitIntoClient
@inline(__always)
public func makeBorrowingIterator_() -> BorrowingIterator_ {
let span = self.span
let it = span.makeBorrowingIterator_()
// FIXME: `it` is borrowing `span`, not self
return _overrideLifetime(it, borrowing: self)
public func makeBorrowingIterator() -> BorrowingIterator {
self.span._makeBorrowingIterator(from: 0, to: count)
}
}
#endif
@@ -41,17 +38,14 @@ extension RigidArray: Container where Element: ~Copyable {
@_lifetime(borrow self)
public func makeBorrowingIterator(
from start: Index, to end: Index
) -> BorrowingIterator_ {
) -> BorrowingIterator {
// FIXME: `makeBorrowingIterator` would be borrowing the temporary `span`, not self
self.span._makeBorrowingIterator(from: start, to: end)
}
@_alwaysEmitIntoClient
public func currentIndex(of iterator: borrowing BorrowingIterator_) -> Index {
precondition(
iterator._span.isTriviallyIdentical(to: self.span),
"Invalid iterator instance")
return iterator._start
public func currentIndex(of iterator: inout BorrowingIterator) -> Index {
self.span.currentIndex(of: &iterator)
}
}
@@ -73,7 +67,7 @@ extension RigidArray: RangeReplaceableContainer where Element: ~Copyable {}
@available(SwiftStdlib 5.0, *)
extension RigidArray where Element: ~Copyable {
@inlinable
public var underestimatedCount_: Int { count }
public var underestimatedCount: Int { count }
/// A Boolean value indicating whether this array contains no elements.
///
@@ -128,11 +128,11 @@ extension RigidArray /*where Element: Copyable*/ {
@available(SwiftStdlib 6.4, *)
@_alwaysEmitIntoClient
@inline(__always)
public init<Source: Iterable_ & ~Copyable & ~Escapable>(
public init<Source: Iterable & ~Copyable & ~Escapable>(
capacity: Int,
copying contents: borrowing Source
) throws(Source.Failure_)
where Source.Element_ == Element {
) throws(Source.Failure)
where Source.Element == Element {
self.init(capacity: capacity)
try self.append(copying: contents)
}
@@ -151,11 +151,11 @@ extension RigidArray /*where Element: Copyable*/ {
@available(SwiftStdlib 6.4, *)
@_alwaysEmitIntoClient
@inline(__always)
public init<Source: Iterable_ & Sequence<Element>>(
public init<Source: Iterable & Sequence<Element>>(
capacity: Int,
copying contents: Source
) throws(Source.Failure_)
where Source.Element_ == Element {
) throws(Source.Failure)
where Source.Element == Element {
self.init(capacity: capacity)
try self.append(copying: contents)
}
@@ -592,17 +592,17 @@ extension RigidArray {
copyingContainer items: borrowing C,
newCount: Int
) {
var it = items.makeBorrowingIterator_()
var it = items.makeBorrowingIterator()
self.replace(removing: subrange, addingCount: newCount) { target in
while !target.isFull {
let source = it.nextSpan_(maxCount: target.freeCapacity)
let source = it.nextSpan(maxCount: target.freeCapacity)
precondition(
!source.isEmpty,
"Broken Container: count doesn't match contents")
target._append(copying: source)
}
precondition(
it.nextSpan_().isEmpty,
it.nextSpan().isEmpty,
"Broken Container: count doesn't match contents")
}
}
@@ -43,14 +43,13 @@ extension RigidDictionary where Key: ~Copyable, Value: ~Copyable {
}
@available(SwiftStdlib 6.4, *)
extension RigidDictionary.Indices: Iterable_
extension RigidDictionary.Indices: Iterable
where Key: ~Copyable, Value: ~Copyable
{
public typealias Element = RigidDictionary.Index
public typealias Element_ = Element
@frozen
public struct BorrowingIterator_: ~Escapable {
public struct BorrowingIterator: ~Escapable {
public typealias Element = RigidDictionary.Index
@usableFromInline
@@ -70,24 +69,22 @@ where Key: ~Copyable, Value: ~Copyable
}
@inlinable
public var underestimatedCount_: Int { self._base.value.count }
public var underestimatedCount: Int { self._base.value.count }
@_lifetime(borrow self) // FIXME: Should be @_lifetime(copy self)
public func makeBorrowingIterator_() -> BorrowingIterator_ {
@_lifetime(borrow self)
public func makeBorrowingIterator() -> BorrowingIterator {
let bit = self._base.value.makeBucketIterator()
// FIXME: This override really should not be necessary. Check if the real `struct Borrow` fixes it.
let override = _overrideLifetime(bit, copying: self)
return BorrowingIterator_(_it: override)
return BorrowingIterator(_it: override)
}
}
@available(SwiftStdlib 6.4, *)
extension RigidDictionary.Indices.BorrowingIterator_: BorrowingIteratorProtocol_
extension RigidDictionary.Indices.BorrowingIterator: BorrowingIteratorProtocol
where Key: ~Copyable, Value: ~Copyable {
public typealias Element_ = Element
@_lifetime(&self)
public mutating func nextSpan_(maxCount: Int) -> Span<Element> {
public mutating func nextSpan(maxCount: Int) -> Span<Element> {
guard _it.advanceToOccupied() else { return .init() }
_current[0] = Element(_bucket: _it.currentBucket)
_it.advanceToNextBit()
@@ -18,14 +18,12 @@ import ContainersPreview
#if compiler(>=6.4) && UnstableHashedContainers && UnstableContainersPreview
@available(SwiftStdlib 5.0, *)
extension RigidSet: Iterable_ where Element: ~Copyable {
public typealias Element_ = Element
extension RigidSet: Iterable where Element: ~Copyable {
@inlinable
public var underestimatedCount_: Int { count }
public var underestimatedCount: Int { count }
@inlinable
public func _customContainsEquatableElement_(
public func _customContainsEquatableElement(
_ element: borrowing Element
) -> Bool? {
self.contains(element)
@@ -33,20 +31,18 @@ extension RigidSet: Iterable_ where Element: ~Copyable {
@inlinable
@_lifetime(borrow self)
public borrowing func makeBorrowingIterator_() -> BorrowingIterator_ {
BorrowingIterator_(_set: self)
public borrowing func makeBorrowingIterator() -> BorrowingIterator {
BorrowingIterator(_set: self)
}
@frozen
public struct BorrowingIterator_:
BorrowingIteratorProtocol_,
public struct BorrowingIterator:
BorrowingIteratorProtocol,
~Copyable,
~Escapable
{
public typealias Element_ = Element
@_alwaysEmitIntoClient
internal var _baseAddress: UnsafePointer<Element_>?
internal var _baseAddress: UnsafePointer<Element>?
@_alwaysEmitIntoClient
internal var _bucketIterator: _HTable.BucketIterator
@@ -54,7 +50,7 @@ extension RigidSet: Iterable_ where Element: ~Copyable {
@_alwaysEmitIntoClient
@_lifetime(borrow _set)
internal init(
_set: borrowing RigidSet<Element_>
_set: borrowing RigidSet<Element>
) {
self._baseAddress = .init(_set._members)
self._bucketIterator = _set._table.makeBucketIterator()
@@ -62,7 +58,7 @@ extension RigidSet: Iterable_ where Element: ~Copyable {
@_alwaysEmitIntoClient
@_lifetime(copy self)
internal func _span(over buckets: Range<_Bucket>) -> Span<Element_> {
internal func _span(over buckets: Range<_Bucket>) -> Span<Element> {
let items = UnsafeBufferPointer(
start: _baseAddress.unsafelyUnwrapped + buckets.lowerBound.offset,
count: buckets.upperBound.offset - buckets.lowerBound.offset)
@@ -71,7 +67,7 @@ extension RigidSet: Iterable_ where Element: ~Copyable {
@_alwaysEmitIntoClient
@_lifetime(&self)
public mutating func nextSpan_(maxCount: Int) -> Span<Element_> {
public mutating func nextSpan(maxCount: Int = .max) -> Span<Element> {
precondition(maxCount > 0, "maxCount must be positive")
guard
let next = _bucketIterator.nextOccupiedRegion(maxCount: maxCount)
@@ -40,9 +40,9 @@ extension RigidSet where Element: ~Copyable {
guard left.count == right.count else { return false }
#if UnstableContainersPreview
var lit = left.makeBorrowingIterator_()
var lit = left.makeBorrowingIterator()
while true {
let l = lit.nextSpan_()
let l = lit.nextSpan(maxCount: .max)
if l.isEmpty { break }
var i = 0
while i < l.count {
@@ -37,9 +37,9 @@ extension RigidSet where Element: ~Copyable {
public func _rawHashValue(seed: Int) -> Int {
#if UnstableContainersPreview
var hash = 0
var it = self.makeBorrowingIterator_()
var it = self.makeBorrowingIterator()
while true {
let next = it.nextSpan_()
let next = it.nextSpan()
var i = 0
while i < next.count {
hash ^= next[unchecked: i]._rawHashValue(seed: seed)
@@ -86,12 +86,12 @@ extension RigidSet /* where Element: Copyable */ {
@_alwaysEmitIntoClient
@inline(__always)
public init<
S: Iterable_ & ~Copyable & ~Escapable
S: Iterable & ~Copyable & ~Escapable
>(
capacity: Int,
copying contents: borrowing S
) throws(S.Failure_)
where S.Element_ == Element {
) throws(S.Failure)
where S.Element == Element {
self.init(capacity: capacity)
try self._insert(copying: contents)
}
@@ -122,12 +122,12 @@ extension RigidSet /* where Element: Copyable */ {
@_alwaysEmitIntoClient
@inline(__always)
public init<
S: Iterable_ & Sequence<Element>
S: Iterable & Sequence<Element>
>(
capacity: Int,
copying contents: borrowing S
) throws(S.Failure_)
where S.Element_ == Element {
) throws(S.Failure)
where S.Element == Element {
self.init(capacity: capacity)
try self._insert(copying: contents)
}
@@ -136,12 +136,12 @@ extension RigidSet /* where Element: Copyable */ {
@_alwaysEmitIntoClient
@inline(__always)
public init<
S: Iterable_ & Collection<Element>
S: Iterable & Collection<Element>
>(
capacity: Int? = nil,
copying contents: S
) throws(S.Failure_)
where S.Element_ == Element {
) throws(S.Failure)
where S.Element == Element {
self.init(capacity: capacity ?? contents.count)
try self._insert(copying: contents)
}
@@ -239,14 +239,14 @@ extension RigidSet /* where Element: Copyable */ {
@available(SwiftStdlib 6.4, *)
@_alwaysEmitIntoClient
package mutating func _insert<
S: Iterable_ & ~Copyable & ~Escapable
S: Iterable & ~Copyable & ~Escapable
>(
copying items: borrowing S
) throws(S.Failure_)
where S.Element_ == Element {
var it = items.makeBorrowingIterator_()
) throws(S.Failure)
where S.Element == Element {
var it = items.makeBorrowingIterator()
while true {
let span = try it.nextSpan_()
let span = try it.nextSpan()
guard !span.isEmpty else { break }
self.insert(copying: span)
}
@@ -258,11 +258,11 @@ extension RigidSet /* where Element: Copyable */ {
@_alwaysEmitIntoClient
@inline(__always)
public mutating func insert<
S: Iterable_ & ~Copyable & ~Escapable
S: Iterable & ~Copyable & ~Escapable
>(
copying items: borrowing S
) throws(S.Failure_)
where S.Element_ == Element {
) throws(S.Failure)
where S.Element == Element {
try _insert(copying: items)
}
#endif
@@ -281,11 +281,11 @@ extension RigidSet /* where Element: Copyable */ {
@_alwaysEmitIntoClient
@inline(__always)
public mutating func insert<
S: Iterable_ & Sequence<Element>
S: Iterable & Sequence<Element>
>(
copying items: borrowing S
) throws(S.Failure_)
where S.Element_ == Element {
) throws(S.Failure)
where S.Element == Element {
try _insert(copying: items)
}
#endif
@@ -261,15 +261,15 @@ extension UniqueArray {
@available(SwiftStdlib 6.4, *)
@inlinable
internal mutating func _append<
Source: Iterable_ & ~Copyable & ~Escapable
Source: Iterable & ~Copyable & ~Escapable
>(
copying newElements: borrowing Source
) throws(Source.Failure_)
where Source.Element_ == Element {
_ensureFreeCapacity(newElements.underestimatedCount_)
var it = newElements.makeBorrowingIterator_()
) throws(Source.Failure)
where Source.Element == Element {
_ensureFreeCapacity(newElements.underestimatedCount)
var it = newElements.makeBorrowingIterator()
while true {
let span = try it.nextSpan_()
let span = try it.nextSpan()
if span.isEmpty { break }
_ensureFreeCapacity(span.count)
_storage.append(copying: span)
@@ -296,11 +296,11 @@ extension UniqueArray {
@_alwaysEmitIntoClient
@inline(__always)
public mutating func append<
Source: Iterable_ & ~Copyable & ~Escapable
Source: Iterable & ~Copyable & ~Escapable
>(
copying newElements: borrowing Source
) throws(Source.Failure_)
where Source.Element_ == Element {
) throws(Source.Failure)
where Source.Element == Element {
try self._append(copying: newElements)
}
@@ -352,11 +352,11 @@ extension UniqueArray {
@_alwaysEmitIntoClient
@inline(__always)
public mutating func append<
Source: Iterable_ & Sequence<Element>
Source: Iterable & Sequence<Element>
>(
copying newElements: Source
) throws(Source.Failure_)
where Source.Element_ == Element {
) throws(Source.Failure)
where Source.Element == Element {
try self._append(copying: newElements)
}
#endif
@@ -20,16 +20,16 @@ import ContainersPreview
#if compiler(>=6.4) && UnstableContainersPreview
@available(SwiftStdlib 6.4, *)
extension UniqueArray: Iterable_ where Element: ~Copyable {
public typealias BorrowingIterator_ = Span<Element>.BorrowingIterator_
extension UniqueArray: Iterable where Element: ~Copyable {
public typealias BorrowingIterator = Span<Element>.BorrowingIterator
@inlinable
public var underestimatedCount_: Int { count }
public var underestimatedCount: Int { count }
@_alwaysEmitIntoClient
@inline(__always)
public func makeBorrowingIterator_() -> BorrowingIterator_ {
self._storage.makeBorrowingIterator_()
public func makeBorrowingIterator() -> BorrowingIterator {
self._storage.makeBorrowingIterator()
}
}
#endif
@@ -41,13 +41,13 @@ extension UniqueArray: Container where Element: ~Copyable {
@_lifetime(borrow self)
public func makeBorrowingIterator(
from start: Index, to end: Index
) -> BorrowingIterator_ {
) -> BorrowingIterator {
_storage.makeBorrowingIterator(from: start, to: end)
}
@_alwaysEmitIntoClient
public func currentIndex(of iterator: borrowing BorrowingIterator_) -> Index {
_storage.currentIndex(of: iterator)
public func currentIndex(of iterator: inout BorrowingIterator) -> Index {
_storage.currentIndex(of: &iterator)
}
}
@@ -97,11 +97,11 @@ extension UniqueArray /*where Element: Copyable*/ {
@available(SwiftStdlib 6.4, *)
@_alwaysEmitIntoClient
@inline(__always)
public init<Source: Iterable_ & ~Copyable & ~Escapable>(
public init<Source: Iterable & ~Copyable & ~Escapable>(
capacity: Int? = nil,
copying contents: borrowing Source
) throws(Source.Failure_)
where Source.Element_ == Element {
) throws(Source.Failure)
where Source.Element == Element {
self.init(minimumCapacity: capacity ?? 0)
try self.append(copying: contents)
}
@@ -135,11 +135,11 @@ extension UniqueArray /*where Element: Copyable*/ {
@available(SwiftStdlib 6.4, *)
@_alwaysEmitIntoClient
@inline(__always)
public init<Source: Iterable_ & Sequence<Element>>(
public init<Source: Iterable & Sequence<Element>>(
capacity: Int? = nil,
copying contents: Source
) throws(Source.Failure_)
where Source.Element_ == Element {
) throws(Source.Failure)
where Source.Element == Element {
self.init(minimumCapacity: capacity ?? 0)
try self.append(copying: contents)
}
@@ -19,15 +19,14 @@ import ContainersPreview
#if compiler(>=6.4) && UnstableHashedContainers && UnstableContainersPreview
@available(SwiftStdlib 5.0, *)
extension UniqueSet: Iterable_ where Element: ~Copyable {
public typealias Element_ = Element
public typealias BorrowingIterator_ = RigidSet<Element>.BorrowingIterator_
extension UniqueSet: Iterable where Element: ~Copyable {
public typealias BorrowingIterator = RigidSet<Element>.BorrowingIterator
@inlinable
public var underestimatedCount_: Int { count }
public var underestimatedCount: Int { count }
@inlinable
public func _customContainsEquatableElement_(
public func _customContainsEquatableElement(
_ element: borrowing Element
) -> Bool? {
self.contains(element)
@@ -35,8 +34,8 @@ extension UniqueSet: Iterable_ where Element: ~Copyable {
@inlinable
@_lifetime(borrow self)
public borrowing func makeBorrowingIterator_() -> BorrowingIterator_ {
BorrowingIterator_(_set: _storage)
public borrowing func makeBorrowingIterator() -> BorrowingIterator {
BorrowingIterator(_set: _storage)
}
}
#endif
@@ -77,12 +77,10 @@ extension UniqueSet where Element: Copyable {
#if UnstableContainersPreview
@available(SwiftStdlib 6.4, *)
@_alwaysEmitIntoClient
public init<
S: Iterable_ & ~Copyable & ~Escapable
>(
public init<S: Iterable & ~Copyable & ~Escapable>(
copying items: borrowing S
) throws(S.Failure_)
where S.Element_ == Element {
) throws(S.Failure)
where S.Element == Element {
self.init()
try self.insert(copying: items)
}
@@ -97,12 +95,10 @@ extension UniqueSet where Element: Copyable {
#if UnstableContainersPreview
@available(SwiftStdlib 6.4, *)
@_alwaysEmitIntoClient
public init<
S: Iterable_ & Sequence<Element>
>(
public init<S: Iterable & Sequence<Element>>(
copying items: borrowing S
) throws(S.Failure_)
where S.Element_ == Element {
) throws(S.Failure)
where S.Element == Element {
self.init()
try self._insert(copying: items)
}
@@ -134,15 +134,15 @@ extension UniqueSet /* where Element: Copyable */ {
@available(SwiftStdlib 6.4, *)
@_alwaysEmitIntoClient
package mutating func _insert<
S: Iterable_ & ~Copyable & ~Escapable
S: Iterable & ~Copyable & ~Escapable
>(
copying items: borrowing S
) throws(S.Failure_)
where S.Element_ == Element {
_ensureFreeCapacity(items.underestimatedCount_)
var it = items.makeBorrowingIterator_()
) throws(S.Failure)
where S.Element == Element {
_ensureFreeCapacity(items.underestimatedCount)
var it = items.makeBorrowingIterator()
while true {
let span = try it.nextSpan_()
let span = try it.nextSpan()
guard !span.isEmpty else { break }
self.insert(copying: span)
}
@@ -154,10 +154,10 @@ extension UniqueSet /* where Element: Copyable */ {
@_alwaysEmitIntoClient
@inline(__always)
public mutating func insert<
S: Iterable_ & ~Copyable & ~Escapable
S: Iterable & ~Copyable & ~Escapable
>(
copying items: borrowing S
) throws(S.Failure_) where S.Element_ == Element {
) throws(S.Failure) where S.Element == Element {
try _insert(copying: items)
}
#endif
@@ -177,11 +177,11 @@ extension UniqueSet /* where Element: Copyable */ {
@_alwaysEmitIntoClient
@inline(__always)
public mutating func insert<
S: Iterable_ & Sequence<Element>
S: Iterable & Sequence<Element>
>(
copying items: borrowing S
) throws(S.Failure_)
where S.Element_ == Element {
) throws(S.Failure)
where S.Element == Element {
try _insert(copying: items)
}
#endif