Files
swift-collections/Tests/BasicContainersTests/ArrayLayout.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

144 lines
3.8 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2025 - 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
//
//===----------------------------------------------------------------------===//
import XCTest
#if COLLECTIONS_SINGLE_MODULE
import Collections
#else
import _CollectionsTestSupport
import SpanPreview
import BasicContainers
import ContainersPreview
#endif
struct ArrayLayout {
var capacity: Int
var count: Int
init(capacity: Int, count: Int) {
precondition(count >= 0 && count <= capacity)
self.capacity = capacity
self.count = count
}
var isFull: Bool { count == capacity }
var isEmpty: Bool { count == 0 }
var freeCapacity: Int { capacity - count }
}
func withSomeArrayLayouts<E: Error>(
_ label: String,
ofCapacities capacities: some Sequence<Int>,
file: StaticString = #filePath,
line: UInt = #line,
run body: (ArrayLayout) throws(E) -> Void
) throws(E) {
let context = TestContext.current
for capacity in capacities {
var counts: Set<Int> = []
counts.insert(0)
counts.insert(capacity)
counts.insert(capacity / 2)
if capacity >= 1 {
counts.insert(1)
counts.insert(capacity - 1)
}
if capacity >= 2 {
counts.insert(2)
counts.insert(capacity - 2)
}
for count in counts.sorted() {
let layout = ArrayLayout(capacity: capacity, count: count)
let entry = context.push("\(label): \(layout)", file: file, line: line)
var done = false
defer {
context.pop(entry)
if !done {
print(context.currentTrace(title: "Throwing trace"))
}
}
try body(layout)
done = true
}
}
}
#if compiler(>=6.2)
extension RigidArray where Element: ~Copyable {
init(layout: ArrayLayout, using generator: (Int) -> Element) {
self.init(capacity: layout.capacity) { span in
for i in 0 ..< layout.count {
span.append(generator(i))
}
}
}
}
extension UniqueArray where Element: ~Copyable {
init(layout: ArrayLayout, using generator: (Int) -> Element) {
self.init(consuming: RigidArray(layout: layout, using: generator))
}
}
extension LifetimeTracker {
func rigidArray<Element>(
layout: ArrayLayout,
using generator: (Int) -> Element = { $0 }
) -> RigidArray<LifetimeTracked<Element>> {
RigidArray(layout: layout, using: { self.instance(for: generator($0)) })
}
func uniqueArray<Element>(
layout: ArrayLayout,
using generator: (Int) -> Element = { $0 }
) -> UniqueArray<LifetimeTracked<Element>> {
let contents = RigidArray(layout: layout) {
self.instance(for: generator($0))
}
return UniqueArray(consuming: contents)
}
#if UnstableContainersPreview
func withInputSpan<Element>(
layout: ArrayLayout,
using generator: (Int) -> Element,
_ body: (inout InputSpan<LifetimeTracked<Element>>) -> Void
) {
let buffer = UnsafeMutableBufferPointer<LifetimeTracked<Element>>
.allocate(capacity: layout.capacity)
for i in 0 ..< layout.count {
buffer.initializeElement(
at: layout.capacity - layout.count + i,
to: self.instance(for: generator(i)))
}
var span = InputSpan(buffer: buffer, initializedCount: layout.count)
body(&span)
_ = consume span
buffer.deallocate()
}
#endif
func withOutputSpan<Element>(
layout: ArrayLayout,
using generator: (Int) -> Element,
_ body: (inout OutputSpan<LifetimeTracked<Element>>) -> Void
) {
var contents = RigidArray(layout: layout) {
self.instance(for: generator($0))
}
contents.edit(body)
}
}
#endif