Update Span/* to build with current snapshots

This commit is contained in:
Karoy Lorentey committed 2025-04-08 14:48:07 -07:00
1 parent 64eab2812b
commit 9e2fc09c3e
8 files changed
+75 -36

No files matched your search

+1 -1
View File
@@ -35,7 +35,7 @@ internal func _overrideLifetime<
@_unsafeNonescapableResult
@_alwaysEmitIntoClient
@_transparent
@lifetime(source)
@lifetime(copy source)
internal func _overrideLifetime<
T: ~Copyable & ~Escapable, U: ~Copyable & ~Escapable
>(
+2
View File
@@ -10,6 +10,7 @@
//
//===----------------------------------------------------------------------===//
#if false // FIXME: Revive
// A MutableRawSpan represents a span of memory which
// contains initialized `Element` instances.
@safe
@@ -628,3 +629,4 @@ extension MutableRawSpan {
return unsafe _overrideLifetime(newSpan, mutating: &self)
}
}
#endif
+21
View File
@@ -10,6 +10,7 @@
//
//===----------------------------------------------------------------------===//
#if false // FIXME: Revive
import Builtin
// A MutableSpan<Element> represents a span of memory which
@@ -382,6 +383,7 @@ extension MutableSpan where Element: ~Copyable {
//FIXME: mark closure parameter as non-escaping
@_alwaysEmitIntoClient
@lifetime(self: copy self)
public func withUnsafeBufferPointer<E: Error, Result: ~Copyable>(
_ body: (_ buffer: UnsafeBufferPointer<Element>) throws(E) -> Result
) throws(E) -> Result {
@@ -390,6 +392,7 @@ extension MutableSpan where Element: ~Copyable {
//FIXME: mark closure parameter as non-escaping
@_alwaysEmitIntoClient
@lifetime(self: copy self)
public mutating func withUnsafeMutableBufferPointer<
E: Error, Result: ~Copyable
>(
@@ -412,6 +415,7 @@ extension MutableSpan where Element: BitwiseCopyable {
//FIXME: mark closure parameter as non-escaping
@_alwaysEmitIntoClient
@lifetime(self: copy self)
public func withUnsafeBytes<E: Error, Result: ~Copyable>(
_ body: (_ buffer: UnsafeRawBufferPointer) throws(E) -> Result
) throws(E) -> Result {
@@ -420,6 +424,7 @@ extension MutableSpan where Element: BitwiseCopyable {
//FIXME: mark closure parameter as non-escaping
@_alwaysEmitIntoClient
@lifetime(self: copy self)
public mutating func withUnsafeMutableBytes<E: Error, Result: ~Copyable>(
_ body: (_ buffer: UnsafeMutableRawBufferPointer) throws(E) -> Result
) throws(E) -> Result {
@@ -436,6 +441,7 @@ extension MutableSpan where Element: BitwiseCopyable {
extension MutableSpan {
@_alwaysEmitIntoClient
@lifetime(self: copy self)
public mutating func update(repeating repeatedValue: consuming Element) {
unsafe _start().withMemoryRebound(to: Element.self, capacity: count) {
unsafe $0.update(repeating: repeatedValue, count: count)
@@ -443,6 +449,7 @@ extension MutableSpan {
}
@_alwaysEmitIntoClient
@lifetime(self: copy self)
public mutating func update<S: Sequence>(
from source: S
) -> (unwritten: S.Iterator, index: Index) where S.Element == Element {
@@ -452,6 +459,7 @@ extension MutableSpan {
}
@_alwaysEmitIntoClient
@lifetime(self: copy self)
public mutating func update(
from elements: inout some IteratorProtocol<Element>
) -> Index {
@@ -465,6 +473,7 @@ extension MutableSpan {
}
@_alwaysEmitIntoClient
@lifetime(self: copy self)
public mutating func update(
fromContentsOf source: some Collection<Element>
) -> Index {
@@ -487,6 +496,7 @@ extension MutableSpan {
}
@_alwaysEmitIntoClient
@lifetime(self: copy self)
public mutating func update(fromContentsOf source: Span<Element>) -> Index {
guard !source.isEmpty else { return 0 }
precondition(
@@ -504,6 +514,7 @@ extension MutableSpan {
}
@_alwaysEmitIntoClient
@lifetime(self: copy self)
public mutating func update(
fromContentsOf source: borrowing MutableSpan<Element>
) -> Index {
@@ -515,6 +526,7 @@ extension MutableSpan {
extension MutableSpan where Element: ~Copyable {
@_alwaysEmitIntoClient
@lifetime(self: copy self)
public mutating func moveUpdate(
fromContentsOf source: consuming OutputSpan<Element>
) -> Index {
@@ -532,6 +544,7 @@ extension MutableSpan where Element: ~Copyable {
}
@_alwaysEmitIntoClient
@lifetime(self: copy self)
public mutating func moveUpdate(
fromContentsOf source: UnsafeMutableBufferPointer<Element>
) -> Index {
@@ -544,6 +557,7 @@ extension MutableSpan where Element: ~Copyable {
extension MutableSpan {
@_alwaysEmitIntoClient
@lifetime(self: copy self)
public mutating func moveUpdate(
fromContentsOf source: Slice<UnsafeMutableBufferPointer<Element>>
) -> Index {
@@ -555,6 +569,7 @@ extension MutableSpan {
extension MutableSpan where Element: BitwiseCopyable {
@_alwaysEmitIntoClient
@lifetime(self: copy self)
public mutating func update(
repeating repeatedValue: Element
) where Element: BitwiseCopyable {
@@ -568,6 +583,7 @@ extension MutableSpan where Element: BitwiseCopyable {
}
@_alwaysEmitIntoClient
@lifetime(self: copy self)
public mutating func update<S: Sequence>(
from source: S
) -> (unwritten: S.Iterator, index: Index)
@@ -578,6 +594,7 @@ extension MutableSpan where Element: BitwiseCopyable {
}
@_alwaysEmitIntoClient
@lifetime(self: copy self)
public mutating func update(
from elements: inout some IteratorProtocol<Element>
) -> Index {
@@ -591,6 +608,7 @@ extension MutableSpan where Element: BitwiseCopyable {
}
@_alwaysEmitIntoClient
@lifetime(self: copy self)
public mutating func update(
fromContentsOf source: some Collection<Element>
) -> Index where Element: BitwiseCopyable {
@@ -613,6 +631,7 @@ extension MutableSpan where Element: BitwiseCopyable {
}
@_alwaysEmitIntoClient
@lifetime(self: copy self)
public mutating func update(
fromContentsOf source: Span<Element>
) -> Index where Element: BitwiseCopyable {
@@ -631,6 +650,7 @@ extension MutableSpan where Element: BitwiseCopyable {
}
@_alwaysEmitIntoClient
@lifetime(self: copy self)
public mutating func update(
fromContentsOf source: borrowing MutableSpan<Element>
) -> Index where Element: BitwiseCopyable {
@@ -858,3 +878,4 @@ extension MutableSpan where Element: ~Copyable {
return unsafe _overrideLifetime(newSpan, mutating: &self)
}
}
#endif
+12 -6
View File
@@ -10,6 +10,7 @@
//
//===----------------------------------------------------------------------===//
#if false // FIXME: Revive
//MARK: Option 1 extracting() function
@available(macOS 9999, *)
extension MutableSpan where Element: ~Copyable {
@@ -21,7 +22,7 @@ extension MutableSpan where Element: ~Copyable {
UInt(bitPattern: bounds.upperBound) <= UInt(bitPattern: _count),
"Index range out of bounds"
)
let newSpan = MutableSpan(_unchecked: _start(), uncheckedBounds: bounds)
let newSpan = unsafe MutableSpan(_unchecked: _start(), uncheckedBounds: bounds)
return unsafe _overrideLifetime(newSpan, mutating: &self)
}
}
@@ -36,7 +37,7 @@ extension MutableSpan where Element: ~Copyable {
) {
let delta = bounds.lowerBound &* MemoryLayout<Element>.stride
let newStart = unsafe pointer.advanced(by: delta)
let newSpan = Self(
let newSpan = unsafe Self(
_unchecked: newStart, count: bounds.upperBound &- bounds.lowerBound
)
self = unsafe _overrideLifetime(newSpan, borrowing: pointer)
@@ -51,7 +52,7 @@ extension Span where Element: ~Copyable {
_unchecked pointer: UnsafeMutableRawPointer,
uncheckedBounds bounds: Range<Index>
) {
let mut = MutableSpan<Element>(_unchecked: pointer, uncheckedBounds: bounds)
let mut = unsafe MutableSpan<Element>(_unchecked: pointer, uncheckedBounds: bounds)
let newSpan = mut.span
self = unsafe _overrideLifetime(newSpan, borrowing: pointer)
}
@@ -69,7 +70,7 @@ extension MutableSpan where Element: ~Copyable {
UInt(bitPattern: bounds.upperBound) <= UInt(bitPattern: _count),
"Index range out of bounds"
)
let newSpan = MutableSpan(_unchecked: _start(), uncheckedBounds: bounds)
let newSpan = unsafe MutableSpan(_unchecked: _start(), uncheckedBounds: bounds)
return unsafe _overrideLifetime(newSpan, mutating: &self)
}
}
@@ -84,6 +85,7 @@ public struct SubMutableSpan<Element: ~Copyable & ~Escapable>
public fileprivate(set) var offset: MutableSpan<Element>.Index
public /*exclusive*/ var base: MutableSpan<Element>
@lifetime(copy _base)
init(_ _offset: Index, _ _base: consuming MutableSpan<Element>) {
offset = _offset
base = _base
@@ -100,6 +102,7 @@ extension SubMutableSpan where Element: ~Copyable {
precondition(offset <= index && index < base.count)
return unsafe UnsafePointer(base._unsafeAddressOfElement(unchecked: index))
}
@lifetime(self: copy self)
unsafeMutableAddress {
precondition(offset <= index && index < base.count)
return unsafe base._unsafeAddressOfElement(unchecked: index)
@@ -109,7 +112,7 @@ extension SubMutableSpan where Element: ~Copyable {
public var span: Span<Element> {
@lifetime(borrow self)
borrowing get {
let newSpan = Span<Element>(
let newSpan = unsafe Span<Element>(
_unchecked: base._start(), uncheckedBounds: offset..<base.count
)
return unsafe _overrideLifetime(newSpan, borrowing: self)
@@ -119,6 +122,7 @@ extension SubMutableSpan where Element: ~Copyable {
@available(macOS 9999, *)
extension SubMutableSpan {
@lifetime(self: copy self)
public mutating func update(repeating repeatedValue: consuming Element) {
base.update(repeating: repeatedValue)
}
@@ -135,7 +139,7 @@ extension MutableSpan where Element: ~Copyable {
UInt(bitPattern: bounds.upperBound) <= UInt(bitPattern: _count),
"Index range out of bounds"
)
let prefix = Self(
let prefix = unsafe Self(
_unchecked: _start(), uncheckedBounds: 0..<bounds.upperBound
)
let subSpan = SubMutableSpan<Element>(bounds.lowerBound, prefix)
@@ -148,6 +152,7 @@ extension MutableSpan where Element: ~Copyable {
@available(macOS 9999, *)
extension MutableSpan where Element: Copyable {
@lifetime(self: copy self)
public mutating func update(
in bounds: Range<Index>,
repeating repeatedValue: consuming Element
@@ -163,3 +168,4 @@ extension MutableSpan where Element: Copyable {
}
}
}
#endif
+28 -18
View File
@@ -56,7 +56,7 @@ public struct OutputSpan<Element: ~Copyable>: ~Copyable, ~Escapable {
capacity: Int,
initialized: Int
) {
_pointer = unsafe start
unsafe _pointer = start
self.capacity = capacity
_initialized = initialized
}
@@ -75,7 +75,7 @@ extension OutputSpan where Element: ~Copyable {
_unchecked buffer: UnsafeMutableBufferPointer<Element>,
initialized: Int
) {
_pointer = .init(buffer.baseAddress)
unsafe _pointer = .init(buffer.baseAddress)
capacity = buffer.count
_initialized = initialized
}
@@ -91,7 +91,7 @@ extension OutputSpan where Element: ~Copyable {
(MemoryLayout<Element>.alignment&-1)) == 0),
"baseAddress must be properly aligned to access Element"
)
self.init(_unchecked: buffer, initialized: initialized)
unsafe self.init(_unchecked: buffer, initialized: initialized)
}
@_alwaysEmitIntoClient
@@ -103,7 +103,7 @@ extension OutputSpan where Element: ~Copyable {
) {
precondition(capacity >= 0, "Capacity must be 0 or greater")
let buf = unsafe UnsafeMutableBufferPointer(start: pointer, count: capacity)
let os = OutputSpan(_initializing: buf, initialized: initialized)
let os = unsafe OutputSpan(_initializing: buf, initialized: initialized)
self = unsafe _overrideLifetime(os, borrowing: pointer)
}
}
@@ -118,7 +118,7 @@ extension OutputSpan {
initialized: Int = 0
) {
let rebased = unsafe UnsafeMutableBufferPointer(rebasing: buffer)
let os = OutputSpan(_initializing: rebased, initialized: 0)
let os = unsafe OutputSpan(_initializing: rebased, initialized: 0)
self = unsafe unsafe _overrideLifetime(os, borrowing: buffer)
}
}
@@ -141,7 +141,7 @@ extension OutputSpan where Element: BitwiseCopyable {
let (count, remainder) = byteCount.quotientAndRemainder(dividingBy: stride)
precondition(remainder == 0, "Span must contain a whole number of elements")
let pointer = bytes.baseAddress
let os = OutputSpan(
let os = unsafe OutputSpan(
_unchecked: pointer, capacity: count, initialized: initialized
)
self = unsafe _overrideLifetime(os, borrowing: bytes)
@@ -156,7 +156,7 @@ extension OutputSpan where Element: BitwiseCopyable {
) {
precondition(capacity >= 0, "Capacity must be 0 or greater")
let buf = unsafe UnsafeMutableRawBufferPointer(start: pointer, count: capacity)
let os = OutputSpan(_initializing: buf, initialized: initialized)
let os = unsafe OutputSpan(_initializing: buf, initialized: initialized)
self = unsafe _overrideLifetime(os, borrowing: pointer)
}
@@ -167,7 +167,7 @@ extension OutputSpan where Element: BitwiseCopyable {
initialized: Int = 0
) {
let rebased = unsafe UnsafeMutableRawBufferPointer(rebasing: buffer)
let os = OutputSpan(_initializing: rebased, initialized: initialized)
let os = unsafe OutputSpan(_initializing: rebased, initialized: initialized)
self = unsafe _overrideLifetime(os, borrowing: buffer)
}
}
@@ -176,6 +176,7 @@ extension OutputSpan where Element: BitwiseCopyable {
extension OutputSpan where Element: ~Copyable {
@_alwaysEmitIntoClient
@lifetime(self: copy self)
public mutating func append(_ value: consuming Element) {
precondition(_initialized < capacity, "Output buffer overflow")
let p = unsafe _start().advanced(by: _initialized&*MemoryLayout<Element>.stride)
@@ -205,6 +206,7 @@ extension OutputSpan where Element: ~Copyable {
extension OutputSpan {
@_alwaysEmitIntoClient
@lifetime(self: copy self)
public mutating func append(repeating repeatedValue: Element, count: Int) {
let available = capacity &- _initialized
precondition(
@@ -220,6 +222,7 @@ extension OutputSpan {
}
@_alwaysEmitIntoClient
@lifetime(self: copy self)
public mutating func append<S>(
from elements: S
) -> S.Iterator where S: Sequence, S.Element == Element {
@@ -229,6 +232,7 @@ extension OutputSpan {
}
@_alwaysEmitIntoClient
@lifetime(self: copy self)
public mutating func append(
from elements: inout some IteratorProtocol<Element>
) {
@@ -241,11 +245,12 @@ extension OutputSpan {
}
@_alwaysEmitIntoClient
@lifetime(self: copy self)
public mutating func append(
fromContentsOf source: some Collection<Element>
) {
let void: Void? = source.withContiguousStorageIfAvailable {
append(fromContentsOf: Span(_unsafeElements: $0))
append(fromContentsOf: unsafe Span(_unsafeElements: $0))
}
if void != nil {
return
@@ -267,6 +272,7 @@ extension OutputSpan {
}
@_alwaysEmitIntoClient
@lifetime(self: copy self)
public mutating func append(
fromContentsOf source: Span<Element>
) {
@@ -276,7 +282,7 @@ extension OutputSpan {
"destination span cannot contain every element from source."
)
let tail = unsafe _start().advanced(by: _initialized&*MemoryLayout<Element>.stride)
_ = source.withUnsafeBufferPointer {
_ = unsafe source.withUnsafeBufferPointer {
unsafe tail.initializeMemory(
as: Element.self, from: $0.baseAddress!, count: $0.count
)
@@ -285,8 +291,9 @@ extension OutputSpan {
}
@_alwaysEmitIntoClient
@lifetime(self: copy self)
public mutating func append(fromContentsOf source: borrowing MutableSpan<Element>) {
source.withUnsafeBufferPointer { append(fromContentsOf: $0) }
unsafe source.withUnsafeBufferPointer { unsafe append(fromContentsOf: $0) }
}
}
@@ -294,6 +301,7 @@ extension OutputSpan {
extension OutputSpan where Element: ~Copyable {
@_alwaysEmitIntoClient
@lifetime(self: copy self)
public mutating func moveAppend(
fromContentsOf source: consuming Self
) {
@@ -312,10 +320,11 @@ extension OutputSpan where Element: ~Copyable {
}
@_alwaysEmitIntoClient
@lifetime(self: copy self)
public mutating func moveAppend(
fromContentsOf source: UnsafeMutableBufferPointer<Element>
) {
let source = OutputSpan(_initializing: source, initialized: source.count)
let source = unsafe OutputSpan(_initializing: source, initialized: source.count)
moveAppend(fromContentsOf: source)
}
}
@@ -324,11 +333,12 @@ extension OutputSpan where Element: ~Copyable {
extension OutputSpan {
@_alwaysEmitIntoClient
@lifetime(self: copy self)
public mutating func moveAppend(
fromContentsOf source: Slice<UnsafeMutableBufferPointer<Element>>
) {
moveAppend(
fromContentsOf: unsafe UnsafeMutableBufferPointer(rebasing: source)
unsafe moveAppend(
fromContentsOf: UnsafeMutableBufferPointer(rebasing: source)
)
}
}
@@ -347,7 +357,7 @@ extension OutputSpan where Element: ~Copyable {
borrowing get {
let pointer = unsafe _pointer?.assumingMemoryBound(to: Element.self)
let buffer = unsafe UnsafeBufferPointer(start: pointer, count: _initialized)
let span = Span(_unsafeElements: buffer)
let span = unsafe Span(_unsafeElements: buffer)
return unsafe _overrideLifetime(span, borrowing: self)
}
}
@@ -360,7 +370,7 @@ extension OutputSpan where Element: ~Copyable {
let buffer = unsafe UnsafeMutableBufferPointer(
start: pointer, count: _initialized
)
let span = MutableSpan(_unsafeElements: buffer)
let span = unsafe MutableSpan(_unsafeElements: buffer)
return unsafe _overrideLifetime(span, mutating: &self)
}
}
@@ -373,7 +383,7 @@ extension OutputSpan where Element: ~Copyable {
@_alwaysEmitIntoClient
public consuming func relinquishBorrowedMemory(
) -> UnsafeMutableBufferPointer<Element> {
let (start, count) = (self._pointer, self._initialized)
let (start, count) = unsafe (self._pointer, self._initialized)
discard self
let typed = unsafe start?.bindMemory(to: Element.self, capacity: count)
return unsafe UnsafeMutableBufferPointer(start: typed, count: count)
@@ -387,7 +397,7 @@ extension OutputSpan where Element: BitwiseCopyable {
@_alwaysEmitIntoClient
public consuming func relinquishBorrowedBytes(
) -> UnsafeMutableRawBufferPointer {
let (start, count) = (self._pointer, self._initialized)
let (start, count) = unsafe (self._pointer, self._initialized)
discard self
let byteCount = count&*MemoryLayout<Element>.stride
return unsafe UnsafeMutableRawBufferPointer(start: start, count: byteCount)
+3 -3
View File
@@ -20,7 +20,7 @@ extension Span {
@lifetime(immortal)
get {
let empty = unsafe UnsafeBufferPointer<Element>(start: nil, count: 0)
let span = Span(_unsafeElements: empty)
let span = unsafe Span(_unsafeElements: empty)
return unsafe _overrideLifetime(span, borrowing: immortalThing)
}
}
@@ -29,7 +29,7 @@ extension Span {
@lifetime(immortal)
public init() {
let empty = unsafe UnsafeBufferPointer<Element>(start: nil, count: 0)
let span = Span(_unsafeElements: empty)
let span = unsafe Span(_unsafeElements: empty)
self = unsafe _overrideLifetime(span, borrowing: immortalThing)
}
}
@@ -77,7 +77,7 @@ extension Span where Element: Equatable {
@_alwaysEmitIntoClient
public func _elementsEqual(_ other: some Collection<Element>) -> Bool {
let equal = other.withContiguousStorageIfAvailable {
_elementsEqual(Span(_unsafeElements: $0))
_elementsEqual(unsafe Span(_unsafeElements: $0))
}
if let equal { return equal }
@@ -21,7 +21,7 @@ extension Array {
unsafeUninitializedCapacity: capacity,
initializingWith: { (buffer, count) in
let pointer = unsafe buffer.baseAddress.unsafelyUnwrapped
var output = OutputSpan<Element>(
var output = unsafe OutputSpan<Element>(
_initializing: pointer, capacity: buffer.count
)
try initializer(&output)
@@ -47,7 +47,7 @@ extension String {
unsafeUninitializedCapacity: capacity,
initializingUTF8With: { buffer in
let pointer = unsafe buffer.baseAddress.unsafelyUnwrapped
var output = OutputSpan<UTF8.CodeUnit>(
var output = unsafe OutputSpan<UTF8.CodeUnit>(
_initializing: pointer, capacity: buffer.count
)
try initializer(&output)
@@ -73,7 +73,7 @@ extension Data {
unsafe try rawBuffer.withMemoryRebound(to: UInt8.self) { buffer in
unsafe buffer.deinitialize()
let pointer = unsafe buffer.baseAddress.unsafelyUnwrapped
var output = OutputSpan<UInt8>(
var output = unsafe OutputSpan<UInt8>(
_initializing: pointer, capacity: capacity
)
try initializer(&output)
@@ -2,7 +2,7 @@
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// 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
@@ -14,7 +14,7 @@ extension UnsafeBufferPointer where Element: ~Copyable {
/// instances refer to the same region in memory.
@inlinable @inline(__always)
public static func ===(_ a: Self, _ b: Self) -> Bool {
(a.baseAddress == b.baseAddress) && (a.count == b.count)
unsafe (a.baseAddress == b.baseAddress) && (a.count == b.count)
}
}
@@ -24,7 +24,7 @@ extension UnsafeMutableBufferPointer where Element: ~Copyable {
/// memory.
@inlinable @inline(__always)
public static func ===(_ a: Self, _ b: Self) -> Bool {
(a.baseAddress == b.baseAddress) && (a.count == b.count)
unsafe (a.baseAddress == b.baseAddress) && (a.count == b.count)
}
}
@@ -33,7 +33,7 @@ extension UnsafeRawBufferPointer {
/// instances refer to the same region in memory.
@inlinable @inline(__always)
public static func ===(_ a: Self, _ b: Self) -> Bool {
(a.baseAddress == b.baseAddress) && (a.count == b.count)
unsafe (a.baseAddress == b.baseAddress) && (a.count == b.count)
}
}
@@ -43,6 +43,6 @@ extension UnsafeMutableRawBufferPointer {
/// memory.
@inlinable @inline(__always)
public static func ===(_ a: Self, _ b: Self) -> Bool {
(a.baseAddress == b.baseAddress) && (a.count == b.count)
unsafe (a.baseAddress == b.baseAddress) && (a.count == b.count)
}
}