Files
swift-collections/Sources/BasicContainers/UniqueArray/UniqueArray+Consumption.swift
T
Karoy Lorentey 842f125c47 Invert dependencies to ContainersPreview, moving all container APIs there
- Move `InputSpan` into its own module, `SpanPreview`. Add dependencies to it.
- Invert dependencies to `ContainersPreview`, so that no production target imports it, but it now depends on its previous dependents.
- Move all generic APIs and conformances using `Producer`/`Drain`/`Container` to the `ContainersPreview` module.
- Remove `UnstableContainersPreview` conditions around `Iterable` APIs

In summary, importing `BasicContainers` and `DequeModule` continue to get us the basic definitions for `RigidArray`, `UniqueSet`, `RigidDeque` etc., without most of their unstable APIs. (With the exception of core APIs that require `InputSpan`.)

To get any API that uses an unstable protocol like `Container` or `Drain`, we’ll now need to import `ContainersPreview` in addition to the type-defining module.

This will provide a slightly less horrible migration path when (if) the container/producer protocol family gets adopted/adapted into the stdlib.
2026-08-25 18:29:58 -07:00

134 lines
4.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 !COLLECTIONS_SINGLE_MODULE
import InternalCollectionsUtilities
import SpanPreview
#endif
#if compiler(>=6.2)
#if UnstableContainersPreview
@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.
///
/// - Parameter subrange: The subrange of items to consume from this array.
/// - Parameter consumer: A function taking an input span of the removed items,
/// allowing them to be consumed straight out of the array's storage.
/// The function is not required to consume all items in the span;
/// however, the span's remaining items will still be removed from
/// the array.
///
/// - Complexity: O(`self.count`)
@_alwaysEmitIntoClient
public mutating func consume(
_ subrange: Range<Int>,
consumingWith consumer: (inout InputSpan<Element>) -> Void
) {
_storage.consume(subrange, consumingWith: consumer)
}
/// Remove the specified subrange of items from this deque,
/// passing a series of input spans to a given callback function to consume
/// them in place.
///
/// The callback is not required to fully consume the contents of its
/// argument; any items it leaves in the input span get automatically
/// consumed. The underlying storage isn't necessarily contiguous, so
/// the callback may be called more than once, whether or not it fully
/// consumes its input. If the specified range is empty, the callback
/// may not be called at all.
///
/// - Parameter subrange: The subrange of items to consume from this array.
/// - Parameter consumer: A function taking an input span of the removed items,
/// allowing them to be consumed straight out of the array's storage.
/// The function is called at most once.
///
/// - Complexity: O(`self.count`)
@_alwaysEmitIntoClient
@inline(__always)
public mutating func consume<R: RangeExpression<Index>>(
_ subrange: R,
consumingWith consumer: (inout InputSpan<Element>) -> Void
) {
_storage.consume(subrange.relative(to: indices), consumingWith: consumer)
}
/// Remove all items currently in this array, passing an input
/// span to a given callback function to consume them in place.
///
/// The callback is not required to fully consume the contents of its
/// argument; any items it leaves in the input span get automatically
/// consumed.
///
/// - Parameter consumer: A function taking an input span of the removed items,
/// allowing them to be consumed straight out of the array's storage.
/// The function is called at most once.
///
/// - Complexity: O(`self.count`)
@_alwaysEmitIntoClient
public mutating func consumeAll(
consumingWith consumer: (inout InputSpan<Element>) -> Void
) {
_storage.consume(indices, consumingWith: consumer)
}
/// Remove the specified number of items from the end of this array,
/// passing an input span to a given callback function to consume them in
/// place.
///
/// The callback is not required to fully consume the contents of its
/// argument; any items it leaves in the input span get automatically
/// consumed.
///
/// - Parameter n: The number of items to consume from the end of the array.
/// `n` must be greater than or equal to zero and must not exceed
/// the count of the array.
/// - Parameter consumer: A function taking an input span of the removed items,
/// allowing them to be consumed straight out of the array's storage.
/// The function is called at most once.
/// - Complexity: O(`n`)
@inline(__always)
@_alwaysEmitIntoClient
public mutating func consumeLast(
_ n: Int,
consumingWith consumer: (inout InputSpan<Element>) -> Void
) {
_storage.consumeLast(n, consumingWith: consumer)
}
}
#endif
#if compiler(>=6.4) && UnstableContainersPreview
@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)
}
}
#endif
#endif