Iterable → BorrowingSequence, BorrowIterator → BorrowingIterator

This commit is contained in:
Karoy Lorentey committed 2026-01-25 16:42:37 -08:00
1 parent fabe5d8c87
commit e4aba4c1c9
24 files changed
+287 -187

No files matched your search

@@ -0,0 +1,83 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2025 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
//
//===----------------------------------------------------------------------===//
#if compiler(>=6.2) && COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
import Builtin
@available(SwiftStdlib 5.0, *)
@frozen
@safe
public struct Borrow<Target: ~Copyable /* FIXME: ~Escapable */>: Copyable, ~Escapable {
@usableFromInline
package let _pointer: UnsafePointer<Target>
@_lifetime(borrow value)
@_alwaysEmitIntoClient
@_transparent
internal init(_borrowing value: borrowing @_addressable Target) {
unsafe _pointer = UnsafePointer(Builtin.unprotectedAddressOfBorrow(value))
}
#if compiler(>=6.3) // rdar://161844406 (https://github.com/swiftlang/swift/pull/84748)
@_lifetime(borrow value)
@_alwaysEmitIntoClient
@_transparent
public init(_ value: borrowing @_addressable Target) {
unsafe _pointer = UnsafePointer(Builtin.unprotectedAddressOfBorrow(value))
}
#endif
@_lifetime(borrow owner)
@_alwaysEmitIntoClient
@_transparent
public init<Owner: ~Copyable & ~Escapable>(
unsafeAddress: UnsafePointer<Target>,
borrowing owner: borrowing Owner
) {
unsafe _pointer = unsafeAddress
}
@_lifetime(copy owner)
@_alwaysEmitIntoClient
@_transparent
public init<Owner: ~Copyable & ~Escapable>(
unsafeAddress: UnsafePointer<Target>,
copying owner: borrowing Owner
) {
unsafe _pointer = unsafeAddress
}
@_alwaysEmitIntoClient
public subscript() -> Target {
@_transparent
unsafeAddress {
unsafe _pointer
}
}
}
extension Optional where Wrapped: ~Copyable /* FIXME: ~Escapable */ {
@available(SwiftStdlib 5.0, *)
@_lifetime(borrow self)
@_addressableSelf
public func borrow() -> Borrow<Wrapped>? {
if self == nil {
return nil
}
let pointer = unsafe UnsafePointer<Wrapped>(
Builtin.unprotectedAddressOfBorrow(self)
)
return unsafe Borrow(unsafeAddress: pointer, borrowing: self)
}
}
#endif
+156
View File
@@ -0,0 +1,156 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2024 - 2025 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
//
//===----------------------------------------------------------------------===//
#if compiler(<6.2)
/// A wrapper type that forms a noncopyable, heap allocated box around an
/// arbitrary value.
///
/// This can be used to form a noncopyable, uniquely referenced box around any
/// Swift value.
@available(*, unavailable, message: "struct Box requires a Swift 6.2 toolchain")
@frozen
public struct Box<T: ~Copyable>: ~Copyable {
@usableFromInline
internal let _pointer: UnsafeMutablePointer<T>
@_alwaysEmitIntoClient
@_transparent
public init(_ value: consuming T) {
fatalError()
}
@_alwaysEmitIntoClient
@inlinable
deinit {
fatalError()
}
}
#else
/// A wrapper type that forms a noncopyable, heap allocated box around an
/// arbitrary value.
///
/// This can be used to form a noncopyable, uniquely referenced box around any
/// Swift value.
@frozen
@safe
public struct Box<T: ~Copyable>: ~Copyable {
@usableFromInline
internal let _pointer: UnsafeMutablePointer<T>
@_alwaysEmitIntoClient
@_transparent
public init(_ value: consuming T) {
unsafe _pointer = UnsafeMutablePointer<T>.allocate(capacity: 1)
unsafe _pointer.initialize(to: value)
}
@_alwaysEmitIntoClient
@inlinable
deinit {
unsafe _pointer.deinitialize(count: 1)
unsafe _pointer.deallocate()
}
}
extension Box where T: ~Copyable {
/// Dereference this box, accessing its contents in a borrowing or
/// mutating way.
@_alwaysEmitIntoClient
public subscript() -> T {
@_transparent
unsafeAddress {
unsafe UnsafePointer<T>(_pointer)
}
@_transparent
unsafeMutableAddress {
unsafe _pointer
}
}
/// Open and destroy this box, returning its contents.
@_alwaysEmitIntoClient
@_transparent
public consuming func consume() -> T {
let result = unsafe _pointer.move()
unsafe _pointer.deallocate()
discard self
return result
}
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
/// Leak the heap allocation behind this box, converting it into an
/// immortal mutating reference.
@_alwaysEmitIntoClient
@_transparent
@_lifetime(immortal)
public consuming func leak() -> Inout<T> {
let result = unsafe Inout<T>(unsafeImmortalAddress: _pointer)
discard self
return result
}
#endif
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
/// Return a borrowing reference to the contents of this box.
@available(SwiftStdlib 5.0, *)
@_alwaysEmitIntoClient
@_transparent
@_lifetime(borrow self)
public func borrow() -> Borrow<T> {
unsafe Borrow(unsafeAddress: UnsafePointer(_pointer), borrowing: self)
}
#endif
#if COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
/// Return a mutating reference to the contents of this box.
@_alwaysEmitIntoClient
@_transparent
@_lifetime(&self)
public mutating func mutate() -> Inout<T> {
unsafe Inout(unsafeAddress: _pointer, mutating: &self)
}
#endif
}
extension Box where T: Copyable {
/// Copy the contents of this box, returning it.
@_alwaysEmitIntoClient
@_transparent
public borrowing func copy() -> T {
unsafe _pointer.pointee
}
}
extension Box where T: ~Copyable {
/// Return a single-item span over the contents of this box.
@available(SwiftStdlib 5.0, *)
public var span: Span<T> {
@_alwaysEmitIntoClient
@_lifetime(borrow self)
get {
unsafe Span(_unsafeStart: _pointer, count: 1)
}
}
/// Return a single-item mutable span over the contents of this box.
@available(SwiftStdlib 5.0, *)
public var mutableSpan: MutableSpan<T> {
@_alwaysEmitIntoClient
@_lifetime(&self)
mutating get {
unsafe MutableSpan(_unsafeStart: _pointer, count: 1)
}
}
}
#endif
+119
View File
@@ -0,0 +1,119 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#if compiler(>=6.2) && COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
import Builtin
/// A safe mutable reference allowing in-place mutation to an exclusive value.
///
/// In order to get an instance of a `Inout<Target>`, one must have exclusive access
/// to the instance of `Target`. This is achieved through the 'inout' operator, '&'.
@frozen
@safe
public struct Inout<Target: ~Copyable /* FIXME: ~Escapable */>: ~Copyable, ~Escapable {
@usableFromInline
package let _pointer: UnsafeMutablePointer<Target>
/// Initializes an instance of 'Mut' extending the exclusive access of the
/// passed instance.
///
/// - Parameter instance: The desired instance to get a mutable reference to.
@_alwaysEmitIntoClient
@_transparent
public init(_ instance: inout Target) {
unsafe _pointer = UnsafeMutablePointer<Target>(Builtin.unprotectedAddressOf(&instance))
}
/// Unsafely initializes an instance of 'Mut' using the given 'unsafeAddress'
/// as the mutable reference based on the lifetime of the given 'owner'
/// argument.
///
/// - Parameter unsafeAddress: The address to use to mutably reference an
/// instance of type 'Target'.
/// - Parameter owner: The owning instance that this 'Mut' instance's
/// lifetime is based on.
@unsafe
@_alwaysEmitIntoClient
@_transparent
@_lifetime(&owner)
public init<Owner: ~Copyable & ~Escapable>(
unsafeAddress: UnsafeMutablePointer<Target>,
mutating owner: inout Owner
) {
unsafe _pointer = unsafeAddress
}
/// Unsafely initializes an instance of 'Mut' using the given
/// 'unsafeImmortalAddress' as the mutable reference acting as though its
/// lifetime is immortal.
///
/// - Parameter unsafeImmortalAddress: The address to use to mutably reference
/// an immortal instance of type 'Target'.
@_lifetime(immortal)
@unsafe
@_alwaysEmitIntoClient
@_transparent
public init(
unsafeImmortalAddress: UnsafeMutablePointer<Target>
) {
unsafe _pointer = unsafeImmortalAddress
}
}
extension Inout where Target: ~Copyable {
/// Dereferences the mutable reference allowing for in-place reads and writes
/// to the underlying instance.
@_alwaysEmitIntoClient
public subscript() -> Target {
@_transparent
unsafeAddress {
unsafe UnsafePointer<Target>(_pointer)
}
@_transparent
unsafeMutableAddress {
unsafe _pointer
}
}
}
extension Inout where Target: ~Copyable {
package func _withUnsafeTarget<E: Error, R: ~Copyable>(
_ body: (inout Target) throws(E) -> R
) throws(E) -> R {
try body(&_pointer.pointee)
}
}
extension Optional where Wrapped: ~Copyable /* FIXME: ~Escapable */ {
@_lifetime(&self)
@_alwaysEmitIntoClient
public mutating func mutate() -> Inout<Wrapped>? {
if self == nil {
return nil
}
let pointer = unsafe UnsafeMutablePointer<Wrapped>(
Builtin.unprotectedAddressOf(&self))
return unsafe Inout(unsafeAddress: pointer, mutating: &self)
}
@_lifetime(&self)
@_alwaysEmitIntoClient
@_transparent
public mutating func insert(_ value: consuming Wrapped) -> Inout<Wrapped> {
self = .some(value)
return mutate()._consumingUnsafelyUnwrap()
}
}
#endif
@@ -0,0 +1,518 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#if compiler(>=6.2) && COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
#if !COLLECTIONS_SINGLE_MODULE
import InternalCollectionsUtilities
#endif
import Builtin
@available(SwiftStdlib 5.0, *)
@safe
@frozen
public struct InputSpan<Element: ~Copyable>: ~Copyable, ~Escapable {
@usableFromInline
internal let _pointer: UnsafeMutableRawPointer?
public let capacity: Int
@usableFromInline
internal var _count: Int
@_alwaysEmitIntoClient
@inlinable
deinit {
if _count > 0 {
let c = _count
unsafe _unsafeRawAddressOfSlot(uncheckedOffset: 0).withMemoryRebound(
to: Element.self, capacity: _count
) {
_ = unsafe $0.deinitialize(count: c)
}
}
}
@_lifetime(immortal)
public init() {
_pointer = nil
capacity = 0
_count = 0
}
}
@available(SwiftStdlib 5.0, *)
extension InputSpan: @unchecked Sendable where Element: Sendable & ~Copyable {}
@available(SwiftStdlib 5.0, *)
extension InputSpan where Element: ~Copyable {
@_alwaysEmitIntoClient
@_transparent
@unsafe
internal func _start() -> UnsafeMutableRawPointer {
unsafe _pointer.unsafelyUnwrapped
}
@unsafe
@_alwaysEmitIntoClient
@_transparent
internal func _unsafeRawAddressOfSlot(
uncheckedOffset offset: Int
) -> UnsafeMutableRawPointer {
let offset = (capacity &- _count &+ offset) &* MemoryLayout<Element>.stride
return unsafe _start().advanced(by: offset)
}
@unsafe
@_alwaysEmitIntoClient
@_transparent
internal func _unsafeAddressOfElement(
uncheckedOffset offset: Index
) -> UnsafeMutablePointer<Element> {
_unsafeRawAddressOfSlot(
uncheckedOffset: offset
).assumingMemoryBound(to: Element.self)
}
}
@available(SwiftStdlib 5.0, *)
extension InputSpan where Element: ~Copyable {
/// Consume the input span and return the number of initialized elements
/// remaining at the end of the underlying memory region.
///
/// This method should be invoked in the scope where the `InputSpan` was
/// created, when it is time to commit the contents of the updated buffer
/// back into the construct that was accessed.
///
/// The context that created the input span is expected to remember what
/// memory region the span is addressing. This consuming method expects to
/// receive a copy of the same buffer pointer as a (loose) proof of ownership.
///
/// - Parameter buffer: The buffer we expect the `InputSpan` to reference.
/// This must be the same region of memory passed to
/// the `InputSpan` initializer.
/// - Returns: The number of initialized elements remaining at the end of the
/// underlying buffer.
@unsafe
@_alwaysEmitIntoClient
public consuming func finalize(
for buffer: UnsafeMutableBufferPointer<Element>
) -> Int {
precondition(
unsafe UnsafeMutableRawPointer(buffer.baseAddress) == self._pointer
&& buffer.count == self.capacity,
"InputSpan identity mismatch")
let count = self._count
discard self
return count
}
}
@available(SwiftStdlib 5.0, *)
extension InputSpan {
/// Consume the input span and return the number of initialized elements
/// remaining at the end of the underlying memory region.
///
/// This method should be invoked in the scope where the `InputSpan` was
/// created, when it is time to commit the contents of the updated buffer
/// back into the construct that was accessed.
///
/// The context that created the input span is expected to remember what
/// memory region the span is addressing. This consuming method expects to
/// receive a copy of the same buffer pointer as a (loose) proof of ownership.
///
/// - Parameter buffer: The buffer we expect the `InputSpan` to reference.
/// This must be the same region of memory passed to
/// the `InputSpan` initializer.
/// - Returns: The number of initialized elements remaining at the end of the
/// underlying buffer.
@unsafe
@_alwaysEmitIntoClient
public consuming func finalize(
for buffer: Slice<UnsafeMutableBufferPointer<Element>>
) -> Int {
unsafe finalize(for: UnsafeMutableBufferPointer(rebasing: buffer))
}
}
@available(SwiftStdlib 5.0, *)
extension InputSpan where Element: ~Copyable {
/// The number of initialized elements in this span.
@_alwaysEmitIntoClient
public var count: Int { _count }
/// The number of additional elements that can be added to this span.
@_alwaysEmitIntoClient
public var freeCapacity: Int { capacity &- _count }
/// A Boolean value indicating whether the span is empty.
@_alwaysEmitIntoClient
public var isEmpty: Bool { _count == 0 }
/// A Boolean value indicating whether the span is full.
@_alwaysEmitIntoClient
public var isFull: Bool { _count == capacity }
}
@available(SwiftStdlib 5.0, *)
extension InputSpan where Element: ~Copyable {
@unsafe
@_alwaysEmitIntoClient
@_lifetime(borrow buffer)
public init(
_uncheckedBuffer buffer: UnsafeMutableBufferPointer<Element>,
initializedCount: Int
) {
unsafe _pointer = .init(buffer.baseAddress)
capacity = buffer.count
_count = initializedCount
}
@unsafe
@_alwaysEmitIntoClient
@_lifetime(borrow buffer)
public init(
buffer: UnsafeMutableBufferPointer<Element>, initializedCount: Int
) {
precondition(buffer._isWellAligned(), "Misaligned OutputSpan")
if let baseAddress = buffer.baseAddress {
precondition(
unsafe baseAddress.advanced(by: buffer.count) >= baseAddress,
"Buffer must not wrap around the address space")
}
precondition(
0 <= initializedCount && initializedCount <= buffer.count,
"OutputSpan count is not within capacity")
unsafe self.init(
_uncheckedBuffer: buffer, initializedCount: initializedCount)
}
}
@available(SwiftStdlib 5.0, *)
extension InputSpan {
/// Unsafely create an input span over partially initialized memory.
///
/// The memory in `buffer` must remain valid throughout the lifetime
/// of the newly-created `InputSpan`. There must be exactly
/// `initializedCount` initialized instances at the end of the buffer,
/// preceded by uninitialized memory.
///
/// - Parameters:
/// - buffer: an `UnsafeMutableBufferPointer` with some number of
/// initialized elements, all compacted into the end of the buffer
/// - initializedCount: the number of initialized elements
/// at the end of `buffer`.
@unsafe
@_alwaysEmitIntoClient
@_lifetime(borrow buffer)
public init(
buffer: borrowing Slice<UnsafeMutableBufferPointer<Element>>,
initializedCount: Int
) {
let rebased = unsafe UnsafeMutableBufferPointer(rebasing: buffer)
let os = unsafe InputSpan(
buffer: rebased, initializedCount: initializedCount)
self = unsafe _overrideLifetime(os, borrowing: buffer)
}
}
@available(SwiftStdlib 5.0, *)
extension InputSpan where Element: ~Copyable {
/// The type that represents an initialized position in an `InputSpan`.
public typealias Index = Int
/// The range of initialized positions for this `InputSpan`.
@_alwaysEmitIntoClient
public var indices: Range<Index> {
unsafe Range(uncheckedBounds: (0, _count))
}
/// Accesses the element at the specified position.
///
/// - Parameter index: A valid index into this span.
///
/// - Complexity: O(1)
@_alwaysEmitIntoClient
public subscript(_ index: Index) -> Element {
unsafeAddress {
precondition(indices.contains(index), "Index out of bounds")
return unsafe UnsafePointer(_unsafeAddressOfElement(uncheckedOffset: index))
}
@_lifetime(self: copy self)
unsafeMutableAddress {
precondition(indices.contains(index), "Index out of bounds")
return unsafe _unsafeAddressOfElement(uncheckedOffset: index)
}
}
/// Accesses the element at the specified position.
///
/// This subscript does not validate `position`; this is an unsafe operation.
///
/// - Parameter index: A valid index into this span.
///
/// - Complexity: O(1)
@unsafe
@_alwaysEmitIntoClient
public subscript(unchecked index: Index) -> Element {
unsafeAddress {
unsafe UnsafePointer(_unsafeAddressOfElement(uncheckedOffset: index))
}
@_lifetime(self: copy self)
unsafeMutableAddress {
unsafe _unsafeAddressOfElement(uncheckedOffset: index)
}
}
/// Exchange the elements at the two given offsets
///
/// - Parameter i: A valid index into this span.
/// - Parameter j: A valid index into this span.
@_alwaysEmitIntoClient
@_lifetime(self: copy self)
public mutating func swapAt(_ i: Index, _ j: Index) {
precondition(indices.contains(Index(i)))
precondition(indices.contains(Index(j)))
unsafe swapAt(unchecked: i, unchecked: j)
}
/// Exchange the elements at the two given offsets
///
/// This subscript does not validate `i` or `j`; this is an unsafe operation.
///
/// - Parameter i: A valid index into this span.
/// - Parameter j: A valid index into this span.
@unsafe
@_alwaysEmitIntoClient
@_lifetime(self: copy self)
public mutating func swapAt(unchecked i: Index, unchecked j: Index) {
guard i != j else { return }
let pi = unsafe _unsafeAddressOfElement(uncheckedOffset: i)
let pj = unsafe _unsafeAddressOfElement(uncheckedOffset: j)
let temporary = unsafe pi.move()
unsafe pi.initialize(to: pj.move())
unsafe pj.initialize(to: consume temporary)
}
}
@available(SwiftStdlib 5.0, *)
extension InputSpan where Element: ~Copyable {
/// Prepend a single element to this span.
@_alwaysEmitIntoClient
@_lifetime(self: copy self)
public mutating func prepend(_ value: consuming Element) {
precondition(_count < capacity, "InputSpan capacity overflow")
unsafe _unsafeRawAddressOfSlot(
uncheckedOffset: -1
).initializeMemory(as: Element.self, to: value)
_count &+= 1
}
/// Remove the last initialized element from this span.
///
/// Returns the last element. The `InputSpan` must not be empty.
@_alwaysEmitIntoClient
@_lifetime(self: copy self)
public mutating func removeFirst() -> Element {
precondition(_count > 0, "InputSpan underflow")
defer { _count &-= 1 }
return _unsafeAddressOfElement(uncheckedOffset: 0).move()
}
/// Remove the last N elements of this span, returning the memory they occupy
/// to the uninitialized state.
///
/// `n` must not be greater than `count`
@_alwaysEmitIntoClient
@_lifetime(self: copy self)
public mutating func removeFirst(_ k: Int) {
precondition(k >= 0, "Can't remove a negative number of elements")
precondition(k <= _count, "InputSpan underflow")
unsafe _unsafeRawAddressOfSlot(
uncheckedOffset: 0
).withMemoryRebound(to: Element.self, capacity: k) {
_ = unsafe $0.deinitialize(count: k)
}
_count &-= k
}
/// Removes and returns the first element of this input span, if it exists.
///
/// - Returns: The first element of the original span if it wasn't empty;
/// otherwise, `nil`.
///
/// - Complexity: O(1)
@inlinable
public mutating func popFirst() -> Element? {
guard _count > 0 else { return nil }
let result = unsafe _unsafeAddressOfElement(uncheckedOffset: 0).move()
_count &-= 1
return result
}
/// Remove all this span's elements and return its memory
/// to the uninitialized state.
@_alwaysEmitIntoClient
@_lifetime(self: copy self)
public mutating func removeAll() {
_ = unsafe _start().withMemoryRebound(to: Element.self, capacity: _count) {
unsafe $0.deinitialize(count: _count)
}
_count = 0
}
}
//MARK: Bulk append functions
@available(SwiftStdlib 5.0, *)
extension InputSpan {
/// Repeatedly append an element to this span.
@_alwaysEmitIntoClient
@_lifetime(self: copy self)
public mutating func prepend(repeating repeatedValue: Element, count: Int) {
precondition(count <= freeCapacity, "InputSpan capacity overflow")
unsafe _unsafeRawAddressOfSlot(
uncheckedOffset: -count
).initializeMemory(as: Element.self, repeating: repeatedValue, count: count)
_count &+= count
}
}
@available(SwiftStdlib 5.0, *)
extension InputSpan where Element: ~Copyable {
/// Borrow the underlying initialized memory for read-only access.
@available(SwiftStdlib 5.0, *)
@_alwaysEmitIntoClient
public var span: Span<Element> {
@_lifetime(borrow self)
borrowing get {
let pointer = unsafe _pointer?
.assumingMemoryBound(to: Element.self)
.advanced(by: capacity &- _count)
let buffer = unsafe UnsafeBufferPointer(start: pointer, count: _count)
let span = unsafe Span(_unsafeElements: buffer)
return unsafe _overrideLifetime(span, borrowing: self)
}
}
/// Exclusively borrow the underlying initialized memory for mutation.
@available(SwiftStdlib 5.0, *)
@_alwaysEmitIntoClient
public var mutableSpan: MutableSpan<Element> {
@_lifetime(&self)
mutating get {
let pointer = unsafe _pointer?
.assumingMemoryBound(to: Element.self)
.advanced(by: capacity &- _count)
let buffer = unsafe UnsafeMutableBufferPointer(
start: pointer, count: _count)
let span = unsafe MutableSpan(_unsafeElements: buffer)
return unsafe _overrideLifetime(span, borrowing: self)
}
}
}
@available(SwiftStdlib 5.0, *)
extension InputSpan where Element: ~Copyable {
/// Call the given closure with the unsafe buffer pointer addressed by this
/// InputSpan and a mutable reference to its count of initialized elements.
///
/// This method provides a way to process or consume the contents of an
/// `InputSpan` using unsafe operations, such as by dispatching to code
/// written in legacy (memory-unsafe) languages.
///
/// The supplied closure may process the buffer in any way it wants; however,
/// when it finishes (whether by returning or throwing), it must leave the
/// buffer in a state that satisfies the invariants of the input span:
///
/// 1. The inout integer passed in as the second argument must be the exact
/// number of initialized items in the buffer passed in as the first
/// argument.
/// 2. These initialized elements must be located in a single contiguous
/// region located at the end of the buffer. The initialized region must
/// be preceded by uninitialized memory.
///
/// This function cannot verify these two invariants, and therefore
/// this is an unsafe operation. Violating the invariants of `InputSpan`
/// may result in undefined behavior.
@_alwaysEmitIntoClient
@_lifetime(self: copy self)
public mutating func withUnsafeMutableBufferPointer<E: Error, R: ~Copyable>(
_ body: (
UnsafeMutableBufferPointer<Element>,
_ initializedCount: inout Int
) throws(E) -> R
) throws(E) -> R {
guard let start = unsafe _pointer, capacity > 0 else {
let buffer = UnsafeMutableBufferPointer<Element>(_empty: ())
var initializedCount = 0
defer {
precondition(initializedCount == 0, "InputSpan capacity overflow")
}
return unsafe try body(buffer, &initializedCount)
}
// bind memory by hand to sidestep alignment concerns
let binding = Builtin.bindMemory(
start._rawValue, capacity._builtinWordValue, Element.self
)
defer { Builtin.rebindMemory(start._rawValue, binding) }
let buffer = unsafe UnsafeMutableBufferPointer<Element>(
/*_uncheckedStart*/start: .init(start._rawValue), count: capacity
)
var initializedCount = self._count
defer {
precondition(
0 <= initializedCount && initializedCount <= capacity,
"InputSpan capacity overflow"
)
self._count = initializedCount
}
return unsafe try body(buffer, &initializedCount)
}
}
@available(SwiftStdlib 5.0, *)
extension InputSpan where Element: ~Copyable {
@_alwaysEmitIntoClient
@_lifetime(&self)
public mutating func consumePrefix(upTo n: Int) -> InputSpan<Element> {
precondition(n >= 0, "Cannot consume a negative number of elements")
let c = Swift.min(n, self.count)
let buffer = unsafe _unsafeRawAddressOfSlot(
uncheckedOffset: 0
).withMemoryRebound(to: Element.self, capacity: c) { start in
UnsafeMutableBufferPointer(start: start, count: c)
}
_count -= c
return _overrideLifetime(
InputSpan(buffer: buffer, initializedCount: c),
mutating: &self)
}
}
@available(SwiftStdlib 5.0, *)
internal func withTemporaryInputSpan<Element: ~Copyable, E: Error, R: ~Copyable>(
of type: Element.Type,
capacity: Int,
_ body: (inout InputSpan<Element>) throws(E) -> R
) throws(E) -> R {
try _withUnsafeTemporaryAllocation(
of: Element.self, capacity: capacity
) { buffer throws(E) in
var span = InputSpan(buffer: buffer, initializedCount: 0)
return try body(&span)
}
}
#endif
@@ -0,0 +1,164 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2024 - 2025 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
//
//===----------------------------------------------------------------------===//
#if compiler(>=6.2) && COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW
#if false // TODO
import Builtin // For Shared.isIdentical
/// A utility adapter that wraps a noncopyable storage type in a copy-on-write
/// struct, enabling efficient implementation of value semantics. The type
/// allows safe borrowing and mutating access to its storage, with minimal fuss.
///
/// Like `ManagedBufferPointer`, this type is intended to be used within the
/// internal implementation of public types. Instances of it aren't designed
/// to be exposed as public.
@safe
@frozen
public struct Shared<Storage: ~Copyable> {
@usableFromInline
internal var _box: _Box
@inlinable
public init(_ storage: consuming Storage) {
unsafe self._box = _Box(storage)
}
}
extension Shared: @unchecked Sendable where Storage: Sendable & ~Copyable {}
extension Shared where Storage: ~Copyable {
@unsafe
@usableFromInline
internal final class _Box {
@exclusivity(unchecked)
@usableFromInline
internal var storage: Storage
@inlinable
internal init(_ storage: consuming Storage) {
unsafe self.storage = storage
}
}
}
extension Shared where Storage: ~Copyable {
@inlinable
@inline(__always)
public mutating func isUnique() -> Bool {
unsafe isKnownUniquelyReferenced(&_box)
}
/// - Returns true if this instance was already uniquely referenced.
@discardableResult
@inlinable
public mutating func ensureUnique(
cloner: (borrowing Storage) -> Storage
) -> Bool {
if isUnique() { return true }
replace(using: cloner)
return false
}
@inlinable
public mutating func replace(
using body: (borrowing Storage) -> Storage
) {
unsafe _box = _Box(body(_box.storage))
}
@inlinable
public mutating func edit(
shared cloner: (borrowing Storage) -> Storage,
unique updater: (inout Storage) -> Void
) {
if isUnique() {
unsafe updater(&_box.storage)
} else {
unsafe _box = _Box(cloner(_box.storage))
}
}
}
import struct SwiftShims.HeapObject
extension Shared where Storage: ~Copyable {
// FIXME: Can we avoid hacks like this? If not, perhaps `_Box.storage` should be tail-allocated.
@inlinable
internal var _address: UnsafePointer<Storage> {
// Adapted from _getUnsafePointerToStoredProperties
let p = unsafe (
UnsafeRawPointer(Builtin.bridgeToRawPointer(_box))
+ MemoryLayout<HeapObject>.size)
return unsafe p.alignedUp(for: Storage.self).assumingMemoryBound(to: Storage.self)
}
@inlinable
internal var _mutableAddress: UnsafeMutablePointer<Storage> {
// Adapted from _getUnsafePointerToStoredProperties
let p = unsafe (
UnsafeMutableRawPointer(Builtin.bridgeToRawPointer(_box))
+ MemoryLayout<HeapObject>.size)
return unsafe p.alignedUp(for: Storage.self).assumingMemoryBound(to: Storage.self)
}
}
extension Shared where Storage: ~Copyable {
@inlinable
@inline(__always)
public var value: Storage {
unsafeAddress {
unsafe _address
}
unsafeMutableAddress {
precondition(isUnique())
return unsafe _mutableAddress
}
}
}
extension Shared where Storage: ~Copyable {
@inlinable
@_lifetime(borrow self)
public borrowing func borrow() -> Ref<Storage> {
// This is gloriously (and very explicitly) unsafe, as it should be.
// `Shared` is carefully constructed to guarantee that
// lifetime(self) == lifetime(_box.storage).
unsafe Ref(unsafeAddress: _address, borrowing: self)
}
@inlinable
@_lifetime(&self)
public mutating func mutate() -> Mut<Storage> {
// This is gloriously (and very explicitly) unsafe, as it should be.
// `Shared` is carefully constructed to guarantee that
// lifetime(self) == lifetime(_box.storage).
unsafe Mut(unsafeAddress: _mutableAddress, mutating: &self)
}
}
extension Shared where Storage: ~Copyable {
@inlinable
public func isIdentical(to other: Self) -> Bool {
if #available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) {
return unsafe self._box === other._box
} else {
// To call the standard `===`, we need to do `_SharedBox` -> AnyObject conversions
// that are only supported in the Swift 6+ runtime.
let a = unsafe Builtin.bridgeToRawPointer(self._box)
let b = unsafe Builtin.bridgeToRawPointer(other._box)
return Bool(Builtin.cmp_eq_RawPointer(a, b))
}
}
}
#endif
#endif