mirror of
https://github.com/apple/swift-collections
synced 2026-11-29 05:14:20 +00:00
Revive Shared and NewArray
Now that unsafeAddress accessors have the expected semantics, `struct Shared` now appears to be functional! The expected syntax builds without issues, and appears to have the right semantics, although I haven’t tried running anything yet
This commit is contained in:
3 files changed
+37
-90
No files matched your search
@@ -53,6 +53,7 @@ var defines: [String] = [
|
||||
]
|
||||
|
||||
let availabilityMacros: KeyValuePairs<String, String> = [
|
||||
"SwiftStdlib 6.0": "macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0",
|
||||
"SwiftStdlib 6.1": "macOS 15.4, iOS 18.4, watchOS 11.4, tvOS 18.4, visionOS 2.4",
|
||||
"SwiftStdlib 6.2": "macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, visionOS 9999",
|
||||
]
|
||||
|
||||
@@ -9,7 +9,6 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if false
|
||||
/// What `Array` might look like if we defined it today.
|
||||
@frozen
|
||||
public struct NewArray<Element> {
|
||||
@@ -23,19 +22,18 @@ public struct NewArray<Element> {
|
||||
}
|
||||
|
||||
extension NewArray {
|
||||
public var storage: Span<Element> {
|
||||
#if false
|
||||
// FIXME: This is what I want to write; alas, lifetimes are messed up.
|
||||
return _storage.value.storage
|
||||
#else
|
||||
return Span(_unsafeElements: _storage.value._items)
|
||||
#endif
|
||||
@available(SwiftStdlib 6.2, *)
|
||||
public var span: Span<Element> {
|
||||
@lifetime(borrow self)
|
||||
get {
|
||||
_storage.value.span
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension NewArray {
|
||||
@inlinable
|
||||
public var capacity: Int { _storage.read { $0.capacity } }
|
||||
public var capacity: Int { _storage.value.capacity }
|
||||
|
||||
@inlinable
|
||||
internal mutating func _ensureUnique() {
|
||||
@@ -47,36 +45,22 @@ extension NewArray {
|
||||
minimumCapacity: Int,
|
||||
linear: Bool = false
|
||||
) {
|
||||
// FIXME: Handle resizing
|
||||
if !_storage.isUnique() {
|
||||
let c = Swift.max(count, minimumCapacity)
|
||||
_storage = Shared(_storage.read { $0._copy(capacity: c) })
|
||||
_storage = Shared(_storage.value._copy(capacity: c))
|
||||
} else if minimumCapacity > self.capacity {
|
||||
_storage = Shared(_storage.update { $0._move(capacity: minimumCapacity) })
|
||||
_storage = Shared(_storage.value._move(capacity: minimumCapacity))
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable
|
||||
internal func _read<E: Error, Result: ~Copyable>(
|
||||
_ body: (borrowing RigidArray<Element>) throws(E) -> Result
|
||||
) throws(E) -> Result {
|
||||
try _storage.read(body)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
internal mutating func _update<E: Error, Result: ~Copyable>(
|
||||
_ body: (inout RigidArray<Element>) throws(E) -> Result
|
||||
) throws(E) -> Result {
|
||||
_ensureUnique()
|
||||
return try _storage.update(body)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
internal mutating func _update<E: Error, Result: ~Copyable>(
|
||||
minimumCapacity: Int,
|
||||
_ body: (inout RigidArray<Element>) throws(E) -> Result
|
||||
) throws(E) -> Result {
|
||||
_ensureUnique(minimumCapacity: minimumCapacity)
|
||||
return try _storage.update(body)
|
||||
return try body(&_storage.value)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,22 +71,22 @@ extension NewArray: RandomAccessCollection, MutableCollection {
|
||||
public var startIndex: Int { 0 }
|
||||
|
||||
@inlinable
|
||||
public var endIndex: Int { _storage.read { $0.count } }
|
||||
public var endIndex: Int { _storage.value.count }
|
||||
|
||||
public subscript(position: Int) -> Element {
|
||||
@inlinable
|
||||
get {
|
||||
_read { $0[position] }
|
||||
unsafeAddress {
|
||||
unsafe _storage.value._ptr(at: position)
|
||||
}
|
||||
@inlinable
|
||||
@inline(__always)
|
||||
_modify {
|
||||
unsafeMutableAddress {
|
||||
_ensureUnique()
|
||||
yield &_storage.value[position]
|
||||
return unsafe _storage.value._mutablePtr(at: position)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if false // FIXME: Implement
|
||||
extension NewArray: RangeReplaceableCollection {
|
||||
public init() {
|
||||
// FIXME: Figure out if we can implement empty singletons in this setup.
|
||||
|
||||
@@ -9,7 +9,6 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#if false
|
||||
import Builtin // For Shared.isIdentical
|
||||
|
||||
/// A utility adapter that wraps a noncopyable storage type in a copy-on-write
|
||||
@@ -19,6 +18,7 @@ import Builtin // For Shared.isIdentical
|
||||
/// 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
|
||||
@@ -26,13 +26,14 @@ public struct Shared<Storage: ~Copyable> {
|
||||
|
||||
@inlinable
|
||||
public init(_ storage: consuming Storage) {
|
||||
self._box = _Box(storage)
|
||||
unsafe self._box = _Box(storage)
|
||||
}
|
||||
}
|
||||
|
||||
extension Shared: @unchecked Sendable where Storage: Sendable & ~Copyable {}
|
||||
|
||||
#if true // FIXME: Silent error on class definition nested in noncopyable struct
|
||||
#if false // FIXME: Silent error on class definition nested in noncopyable struct
|
||||
@unsafe
|
||||
@usableFromInline
|
||||
internal final class _SharedBox<Storage: ~Copyable> {
|
||||
@exclusivity(unchecked)
|
||||
@@ -41,7 +42,7 @@ internal final class _SharedBox<Storage: ~Copyable> {
|
||||
|
||||
@inlinable
|
||||
internal init(_ storage: consuming Storage) {
|
||||
self.storage = storage
|
||||
unsafe self.storage = storage
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,6 +52,7 @@ extension Shared where Storage: ~Copyable {
|
||||
}
|
||||
#else
|
||||
extension Shared where Storage: ~Copyable {
|
||||
@unsafe
|
||||
@usableFromInline
|
||||
internal final class _Box {
|
||||
@exclusivity(unchecked)
|
||||
@@ -59,7 +61,7 @@ extension Shared where Storage: ~Copyable {
|
||||
|
||||
@inlinable
|
||||
internal init(_ storage: consuming Storage) {
|
||||
self.storage = storage
|
||||
unsafe self.storage = storage
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -69,7 +71,7 @@ extension Shared where Storage: ~Copyable {
|
||||
@inlinable
|
||||
@inline(__always)
|
||||
public mutating func isUnique() -> Bool {
|
||||
isKnownUniquelyReferenced(&_box)
|
||||
unsafe isKnownUniquelyReferenced(&_box)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
@@ -77,7 +79,7 @@ extension Shared where Storage: ~Copyable {
|
||||
cloner: (borrowing Storage) -> Storage
|
||||
) {
|
||||
if isUnique() { return }
|
||||
_box = _Box(cloner(_box.storage))
|
||||
unsafe _box = _Box(cloner(_box.storage))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,73 +90,34 @@ extension Shared where Storage: ~Copyable {
|
||||
@inlinable
|
||||
internal var _address: UnsafePointer<Storage> {
|
||||
// Adapted from _getUnsafePointerToStoredProperties
|
||||
let p = (
|
||||
let p = unsafe (
|
||||
UnsafeRawPointer(Builtin.bridgeToRawPointer(_box))
|
||||
+ MemoryLayout<HeapObject>.size)
|
||||
return p.alignedUp(for: Storage.self).assumingMemoryBound(to: Storage.self)
|
||||
return unsafe p.alignedUp(for: Storage.self).assumingMemoryBound(to: Storage.self)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
internal var _mutableAddress: UnsafeMutablePointer<Storage> {
|
||||
// Adapted from _getUnsafePointerToStoredProperties
|
||||
let p = (
|
||||
let p = unsafe (
|
||||
UnsafeMutableRawPointer(Builtin.bridgeToRawPointer(_box))
|
||||
+ MemoryLayout<HeapObject>.size)
|
||||
return p.alignedUp(for: Storage.self).assumingMemoryBound(to: Storage.self)
|
||||
return unsafe p.alignedUp(for: Storage.self).assumingMemoryBound(to: Storage.self)
|
||||
}
|
||||
}
|
||||
|
||||
extension Shared where Storage: ~Copyable {
|
||||
@inlinable
|
||||
@inline(__always)
|
||||
public var value: /*FIXME: dependsOn(self)*/ Storage {
|
||||
// FIXME: This implements the wrong shape.
|
||||
// FIXME: Semantically it yields a borrow scoped to an access of this `value` variable,
|
||||
// FIXME: not the much wider borrow of `self`, which we'd actually want.
|
||||
_read {
|
||||
yield _box.storage
|
||||
}
|
||||
_modify {
|
||||
precondition(isUnique())
|
||||
yield &_box.storage
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: This builds, but attempts to use it don't: they fail with an unexpected exclusivity violation.
|
||||
@inlinable
|
||||
@lifetime(self)
|
||||
public subscript() -> Storage {
|
||||
//@_transparent
|
||||
public var value: Storage {
|
||||
unsafeAddress {
|
||||
_address
|
||||
unsafe _address
|
||||
}
|
||||
|
||||
//@_transparent
|
||||
unsafeMutableAddress {
|
||||
precondition(isUnique())
|
||||
return _mutableAddress
|
||||
return unsafe _mutableAddress
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable
|
||||
@inline(__always)
|
||||
public func read<E: Error, R: ~Copyable>(
|
||||
_ body: (borrowing Storage) throws(E) -> R
|
||||
) throws(E) -> R {
|
||||
// FIXME: This also implements the wrong shape.
|
||||
// FIXME: The borrow of `Storage` isn't tied to `self` at all, and
|
||||
// FIXME: it obviously cannot legally escape the `read` call.
|
||||
try body(_box.storage)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
@inline(__always)
|
||||
public mutating func update<E: Error, R: ~Copyable>(
|
||||
_ body: (inout Storage) throws(E) -> R
|
||||
) throws(E) -> R {
|
||||
precondition(isUnique())
|
||||
return try body(&_box.storage)
|
||||
}
|
||||
}
|
||||
|
||||
#if false // FIXME: Use it or lose it
|
||||
@@ -176,14 +139,13 @@ 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 self._box === other._box
|
||||
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 = Builtin.bridgeToRawPointer(self._box)
|
||||
let b = Builtin.bridgeToRawPointer(other._box)
|
||||
let a = unsafe Builtin.bridgeToRawPointer(self._box)
|
||||
let b = unsafe Builtin.bridgeToRawPointer(other._box)
|
||||
return Bool(Builtin.cmp_eq_RawPointer(a, b))
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
Reference in new issue
Block a user