mirror of
https://github.com/apple/swift-collections
synced 2026-11-28 05:04:21 +00:00
- 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.
129 lines
4.0 KiB
Swift
129 lines
4.0 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#if COLLECTIONS_SINGLE_MODULE
|
|
import Collections
|
|
#else
|
|
import ContainersPreview
|
|
import SpanPreview
|
|
#endif
|
|
|
|
#if compiler(>=6.2)
|
|
/// A silly variant of `CustomDebugStringConvertible` that supports noncopyable
|
|
/// and nonescapable conforming types.
|
|
///
|
|
/// This is expected to be replaced by the stdlib's solution, once we have one.
|
|
/// (It can be as simple as generalizing the standard protocol, but for it serve
|
|
/// its purpose, we also need to be able to perform runtime conformance checks
|
|
/// on noncopyable/nonescapable entities (or otherwise detect and invoke this
|
|
/// property. Additionally, materializing a full `String` instance up front can
|
|
/// be too expensive in some environments -- we also need a solution that
|
|
/// streams descriptions into some caller-provided buffer, eliminating
|
|
/// memory overhead.)
|
|
public protocol TestPrintable: ~Copyable, ~Escapable {
|
|
var testDescription: String { get }
|
|
}
|
|
|
|
extension _Pointer {
|
|
internal var _description: String {
|
|
let hex = String(UInt(bitPattern: self), radix: 16)
|
|
let len = MemoryLayout<Self>.size * 2
|
|
return "0x" + String(repeating: "0", count: len - hex.count) + hex
|
|
}
|
|
}
|
|
|
|
internal func _bufferDescription<P: _Pointer>(
|
|
_ typename: String, start: P?, count: Int
|
|
) -> String {
|
|
"\(typename)(start: \(start?._description ?? "nil"), count: \(count))"
|
|
}
|
|
|
|
extension UnsafeRawBufferPointer: TestPrintable {
|
|
public var testDescription: String {
|
|
_bufferDescription(
|
|
"UnsafeRawBufferPointer", start: baseAddress, count: count)
|
|
}
|
|
}
|
|
extension UnsafeMutableRawBufferPointer: TestPrintable {
|
|
public var testDescription: String {
|
|
_bufferDescription(
|
|
"UnsafeMutableRawBufferPointer", start: baseAddress, count: count)
|
|
}
|
|
}
|
|
extension UnsafeBufferPointer: TestPrintable
|
|
where Element: ~Copyable
|
|
{
|
|
public var testDescription: String {
|
|
_bufferDescription("UnsafeBufferPointer", start: baseAddress, count: count)
|
|
}
|
|
}
|
|
extension UnsafeMutableBufferPointer: TestPrintable
|
|
where Element: ~Copyable
|
|
{
|
|
public var testDescription: String {
|
|
_bufferDescription(
|
|
"UnsafeMutableBufferPointer", start: baseAddress, count: count)
|
|
}
|
|
}
|
|
|
|
@available(SwiftStdlib 5.0, *)
|
|
extension Span: TestPrintable where Element: ~Copyable {
|
|
public var testDescription: String {
|
|
self.withUnsafeBufferPointer { buffer in
|
|
_bufferDescription("Span", start: buffer.baseAddress, count: buffer.count)
|
|
}
|
|
}
|
|
}
|
|
@available(SwiftStdlib 5.0, *)
|
|
extension MutableSpan: TestPrintable where Element: ~Copyable {
|
|
public var testDescription: String {
|
|
self.withUnsafeBufferPointer { buffer in
|
|
_bufferDescription("MutableSpan", start: buffer.baseAddress, count: buffer.count)
|
|
}
|
|
}
|
|
}
|
|
#if compiler(>=6.2)
|
|
@available(SwiftStdlib 5.0, *)
|
|
extension OutputSpan: TestPrintable where Element: ~Copyable {
|
|
public var testDescription: String {
|
|
let capacity = self.capacity
|
|
return self.span.withUnsafeBufferPointer { buffer in
|
|
"""
|
|
OutputSpan(\
|
|
start: \(buffer.baseAddress?._description ?? "nil"), \
|
|
capacity: \(capacity), \
|
|
count: \(buffer.count))
|
|
"""
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
|
|
#if UnstableContainersPreview
|
|
@available(SwiftStdlib 5.0, *)
|
|
extension InputSpan: TestPrintable where Element: ~Copyable {
|
|
public var testDescription: String {
|
|
let capacity = self.capacity
|
|
return self.span.withUnsafeBufferPointer { buffer in
|
|
"""
|
|
InputSpan(\
|
|
start: \(buffer.baseAddress?._description ?? "nil"), \
|
|
capacity: \(capacity), \
|
|
count: \(buffer.count))
|
|
"""
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
#endif
|