[BasicContainers] RigidArray, UniqueArray: Add Drain-based consumption

This commit is contained in:
Karoy Lorentey committed 2026-02-27 18:00:47 -08:00
1 parent 3f6a49ea86
commit d0825fe2e0
2 files changed
+168

No files matched your search

@@ -19,6 +19,11 @@ import ContainersPreview
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
@available(SwiftStdlib 5.0, *)
extension UniqueArray where Element: ~Copyable {
@_lifetime(&self)
public mutating func _consumeAll() -> InputSpan<Element> {
_storage._consumeAll()
}
/// Remove the specified subrange of items from this array,
/// passing an input span to the given function to consume them in place.
///
@@ -109,4 +114,54 @@ extension UniqueArray where Element: ~Copyable {
}
#endif
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
@available(SwiftStdlib 5.0, *)
extension UniqueArray where Element: ~Copyable {
public typealias SubrangeConsumer = RigidArray<Element>.SubrangeConsumer
@_alwaysEmitIntoClient
@inline(__always)
@_lifetime(&self)
public mutating func consume(_ subrange: Range<Index>) -> SubrangeConsumer {
SubrangeConsumer(_base: &self._storage, offsetRange: subrange)
}
@_alwaysEmitIntoClient
@inline(__always)
@_lifetime(&self)
public mutating func consume<R: RangeExpression<Index>>(
_ subrange: R
) -> SubrangeConsumer {
consume(subrange.relative(to: indices))
}
@_alwaysEmitIntoClient
@inline(__always)
@_lifetime(&self)
public mutating func consumeAll() -> SubrangeConsumer {
consume(indices)
}
@_alwaysEmitIntoClient
@inline(__always)
@_lifetime(&self)
public mutating func consumeLast(_ n: Int) -> SubrangeConsumer {
precondition(
n >= 0 && n <= self.count,
"Count of elements to consume is out of bounds")
return consume(self.count - n ..< self.count)
}
@_alwaysEmitIntoClient
@inline(__always)
@_lifetime(&self)
public mutating func consumeFirst(_ n: Int) -> SubrangeConsumer {
precondition(
n >= 0 && n <= self.count,
"Count of elements to consume is out of bounds")
return consume(0 ..< n)
}
}
#endif
#endif