Files
swift-collections/Sources/Future/Containers/MutableContainer.swift
T
Karoy Lorentey 9fc69b4f56 Revive most Container protocols
We cannot express the right subscript requirements yet; make do by requiring hidden members that return (unsafe, escaping) pointers, and provide a default subscript that’s based on unsafe addressors.

This is very unsatisfying, but currently there is no way for a protocol to require subscripts with the right semantics.
2025-04-08 19:25:35 -07:00

50 lines
1.7 KiB
Swift

//===----------------------------------------------------------------------===//
//
// 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.3) // FIXME: Turn this on once we have a new enough toolchain
@available(SwiftStdlib 6.2, *) // for MutableSpan
public protocol Muterator: ~Copyable, ~Escapable {
associatedtype Element: ~Copyable
@lifetime(copy self)
mutating func nextChunk(maximumCount: Int) -> MutableSpan<Element>
}
@available(SwiftStdlib 6.2, *) // for MutableSpan
public protocol MutableContainer: Container, ~Copyable, ~Escapable {
associatedtype MutatingIterationState: ~Copyable, ~Escapable
@lifetime(&self)
mutating func startMutatingIteration() -> MutatingIterationState
/// Return a pointer addressing the element at the given index.
/// This is wildly unsafe; please do not use this outside of `unsafeAddress` accessors.
///
/// This is a temporary stand-in for the subscript requirement that we actually want:
///
/// subscript(index: Index) -> Element { borrow mutate }
@unsafe
mutating func _unsafeMutableAddressOfElement(at index: Index) -> UnsafePointer<Element>
}
extension MutableContainer where Self: ~Copyable & ~Escapable {
@inlinable
public subscript(index: Index) -> Element {
unsafeAddress {
unsafe _unsafeAddressOfElement(at: index)
}
unsafeMutableAddress {
unsafe _unsafeMutableAddressOfElement(at: index)
}
}
}
#endif