mirror of
https://github.com/apple/swift-collections
synced 2026-11-28 05:04:21 +00:00
Copy Span prototype to its own branch
This commit is contained in:
1 parent
6c8dcadca4
commit
c290bd3d04
8 files changed
+850
-207
No files matched your search
@@ -24,13 +24,13 @@ extension Array: ContiguousStorage {
|
||||
public var storage: Span<Element> {
|
||||
_read {
|
||||
if let a = _baseAddressIfContiguous {
|
||||
yield Span(unsafeStart: a, count: count, owner: self)
|
||||
yield Span(_unsafeStart: a, count: count)
|
||||
}
|
||||
else {
|
||||
let a = ContiguousArray(copy self)
|
||||
#if true
|
||||
let s = Span(
|
||||
unsafeStart: a._baseAddressIfContiguous!, count: a.count, owner: a
|
||||
_unsafeStart: a._baseAddressIfContiguous!, count: a.count
|
||||
)
|
||||
#else
|
||||
let s = a.storage
|
||||
@@ -45,7 +45,7 @@ extension ContiguousArray: ContiguousStorage {
|
||||
public var storage: Span<Element> {
|
||||
borrowing get {
|
||||
Span(
|
||||
unsafeStart: _baseAddressIfContiguous!, count: count, owner: self
|
||||
_unsafeStart: _baseAddressIfContiguous!, count: count
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -57,13 +57,13 @@ extension CollectionOfOne: ContiguousStorage {
|
||||
/* ideally: (with strawman syntax)
|
||||
@addressable let value = self._element
|
||||
yield Span(
|
||||
unsafePointer: Builtin.addressable(value), count: 1, owner: self
|
||||
)
|
||||
unsafePointer: Builtin.addressable(value), count: 1
|
||||
)
|
||||
*/
|
||||
|
||||
let a = ContiguousArray(self)
|
||||
yield Span(
|
||||
unsafeStart: a._baseAddressIfContiguous!, count: 1, owner: a
|
||||
_unsafeStart: a._baseAddressIfContiguous!, count: 1
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -85,18 +85,18 @@ extension String.UTF8View: ContiguousStorage {
|
||||
let a = ContiguousArray(self)
|
||||
// yield a.storage
|
||||
yield Span(
|
||||
unsafeStart: a._baseAddressIfContiguous!, count: 1, owner: a
|
||||
_unsafeStart: a._baseAddressIfContiguous!, count: 1
|
||||
)
|
||||
}
|
||||
else if let buffer = withContiguousStorageIfAvailable({ $0 }) {
|
||||
// this is totally wrong, but there is a way with stdlib-internal API
|
||||
yield Span(unsafeElements: buffer, owner: self)
|
||||
yield Span(_unsafeElements: buffer)
|
||||
}
|
||||
else { // copy non-fast code units if we don't have eager bridging
|
||||
let a = ContiguousArray(self)
|
||||
// yield a.storage
|
||||
yield Span(
|
||||
unsafeStart: a._baseAddressIfContiguous!, count: 1, owner: a
|
||||
_unsafeStart: a._baseAddressIfContiguous!, count: 1
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,27 +10,22 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import Builtin
|
||||
|
||||
// A RawSpan represents a span of initialized memory
|
||||
// of unspecified type.
|
||||
@frozen
|
||||
public struct RawSpan: Copyable, ~Escapable {
|
||||
@usableFromInline
|
||||
internal let _pointer: UnsafeRawPointer?
|
||||
@usableFromInline let _pointer: UnsafeRawPointer?
|
||||
|
||||
@usableFromInline @inline(__always)
|
||||
internal var _start: UnsafeRawPointer { _pointer.unsafelyUnwrapped }
|
||||
var _start: UnsafeRawPointer { _pointer.unsafelyUnwrapped }
|
||||
|
||||
@usableFromInline
|
||||
internal let _count: Int
|
||||
@usableFromInline let _count: Int
|
||||
|
||||
@inlinable @inline(__always)
|
||||
internal init<Owner: ~Copyable & ~Escapable>(
|
||||
@_alwaysEmitIntoClient
|
||||
internal init(
|
||||
_unchecked start: UnsafeRawPointer?,
|
||||
byteCount: Int,
|
||||
owner: borrowing Owner
|
||||
) {
|
||||
byteCount: Int
|
||||
) -> dependsOn(immortal) Self {
|
||||
_pointer = start
|
||||
_count = byteCount
|
||||
}
|
||||
@@ -50,13 +45,12 @@ extension RawSpan {
|
||||
/// - buffer: an `UnsafeRawBufferPointer` to initialized memory.
|
||||
/// - owner: a binding whose lifetime must exceed that of
|
||||
/// the newly created `RawSpan`.
|
||||
@inlinable @inline(__always)
|
||||
public init<Owner: ~Copyable & ~Escapable>(
|
||||
unsafeBytes buffer: UnsafeRawBufferPointer,
|
||||
owner: borrowing Owner
|
||||
) {
|
||||
@_alwaysEmitIntoClient
|
||||
public init(
|
||||
_unsafeBytes buffer: UnsafeRawBufferPointer
|
||||
) -> dependsOn(immortal) Self {
|
||||
self.init(
|
||||
_unchecked: buffer.baseAddress, byteCount: buffer.count, owner: owner
|
||||
_unchecked: buffer.baseAddress, byteCount: buffer.count
|
||||
)
|
||||
}
|
||||
|
||||
@@ -70,11 +64,10 @@ extension RawSpan {
|
||||
/// - owner: a binding whose lifetime must exceed that of
|
||||
/// the newly created `RawSpan`.
|
||||
@_alwaysEmitIntoClient
|
||||
public init<Owner: ~Copyable & ~Escapable>(
|
||||
unsafeBytes buffer: UnsafeMutableRawBufferPointer,
|
||||
owner: borrowing Owner
|
||||
) {
|
||||
self.init(unsafeBytes: UnsafeRawBufferPointer(buffer), owner: owner)
|
||||
public init(
|
||||
_unsafeBytes buffer: UnsafeMutableRawBufferPointer
|
||||
) -> dependsOn(immortal) Self {
|
||||
self.init(_unsafeBytes: UnsafeRawBufferPointer(buffer))
|
||||
}
|
||||
|
||||
/// Unsafely create a `RawSpan` over initialized memory.
|
||||
@@ -88,14 +81,67 @@ extension RawSpan {
|
||||
/// - byteCount: the number of initialized bytes in the span.
|
||||
/// - owner: a binding whose lifetime must exceed that of
|
||||
/// the newly created `RawSpan`.
|
||||
@inlinable @inline(__always)
|
||||
public init<Owner: ~Copyable & ~Escapable>(
|
||||
unsafeStart pointer: UnsafeRawPointer,
|
||||
byteCount: Int,
|
||||
owner: borrowing Owner
|
||||
) {
|
||||
@_alwaysEmitIntoClient
|
||||
public init(
|
||||
_unsafeStart pointer: UnsafeRawPointer,
|
||||
byteCount: Int
|
||||
) -> dependsOn(immortal) Self {
|
||||
precondition(byteCount >= 0, "Count must not be negative")
|
||||
self.init(_unchecked: pointer, byteCount: byteCount, owner: owner)
|
||||
self.init(_unchecked: pointer, byteCount: byteCount)
|
||||
}
|
||||
|
||||
/// Unsafely create a `RawSpan` over initialized memory.
|
||||
///
|
||||
/// The memory in `buffer` must be owned by the instance `owner`,
|
||||
/// meaning that as long as `owner` is alive the memory will remain valid.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - buffer: an `UnsafeRawBufferPointer` to initialized memory.
|
||||
/// - owner: a binding whose lifetime must exceed that of
|
||||
/// the newly created `RawSpan`.
|
||||
@_alwaysEmitIntoClient
|
||||
public init<T: BitwiseCopyable>(
|
||||
_unsafeElements buffer: UnsafeBufferPointer<T>
|
||||
) -> dependsOn(immortal) Self {
|
||||
self.init(_unsafeBytes: UnsafeRawBufferPointer(buffer))
|
||||
}
|
||||
|
||||
/// Unsafely create a `RawSpan` over initialized memory.
|
||||
///
|
||||
/// The memory in `buffer` must be owned by the instance `owner`,
|
||||
/// meaning that as long as `owner` is alive the memory will remain valid.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - buffer: an `UnsafeMutableRawBufferPointer` to initialized memory.
|
||||
/// - owner: a binding whose lifetime must exceed that of
|
||||
/// the newly created `RawSpan`.
|
||||
@_alwaysEmitIntoClient
|
||||
public init<T: BitwiseCopyable>(
|
||||
_unsafeElements buffer: UnsafeMutableBufferPointer<T>
|
||||
) -> dependsOn(immortal) Self {
|
||||
self.init(_unsafeElements: UnsafeBufferPointer(buffer))
|
||||
}
|
||||
|
||||
/// Unsafely create a `RawSpan` over initialized memory.
|
||||
///
|
||||
/// The memory over `count` bytes starting at
|
||||
/// `pointer` must be owned by the instance `owner`,
|
||||
/// meaning that as long as `owner` is alive the memory will remain valid.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - pointer: a pointer to the first initialized byte.
|
||||
/// - byteCount: the number of initialized bytes in the span.
|
||||
/// - owner: a binding whose lifetime must exceed that of
|
||||
/// the newly created `RawSpan`.
|
||||
@_alwaysEmitIntoClient
|
||||
public init<T: BitwiseCopyable>(
|
||||
_unsafeStart pointer: UnsafePointer<T>,
|
||||
count: Int
|
||||
) -> dependsOn(immortal) Self {
|
||||
precondition(count >= 0, "Count must not be negative")
|
||||
self.init(
|
||||
_unchecked: pointer, byteCount: count*MemoryLayout<T>.stride
|
||||
)
|
||||
}
|
||||
|
||||
/// Create a `RawSpan` over the memory represented by a `Span<T>`
|
||||
@@ -107,8 +153,7 @@ extension RawSpan {
|
||||
public init<T: BitwiseCopyable>(_ span: borrowing Span<T>) {
|
||||
self.init(
|
||||
_unchecked: UnsafeRawPointer(span._start),
|
||||
byteCount: span.count * MemoryLayout<T>.stride,
|
||||
owner: span
|
||||
byteCount: span.count * MemoryLayout<T>.stride
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -247,8 +292,7 @@ extension RawSpan {
|
||||
public func extracting(unchecked bounds: Range<Int>) -> Self {
|
||||
RawSpan(
|
||||
_unchecked: _pointer?.advanced(by: bounds.lowerBound),
|
||||
byteCount: bounds.count,
|
||||
owner: self
|
||||
byteCount: bounds.count
|
||||
)
|
||||
}
|
||||
|
||||
@@ -352,7 +396,7 @@ extension RawSpan {
|
||||
public func unsafeView<T: BitwiseCopyable>(
|
||||
as type: T.Type
|
||||
) -> Span<T> {
|
||||
Span(unsafeStart: _start, byteCount: byteCount, owner: self)
|
||||
Span(_unsafeStart: _start, byteCount: byteCount)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -520,7 +564,7 @@ extension RawSpan {
|
||||
public func extracting(first maxLength: Int) -> Self {
|
||||
precondition(maxLength >= 0, "Can't have a prefix of negative length.")
|
||||
let newCount = min(maxLength, byteCount)
|
||||
return Self(_unchecked: _pointer, byteCount: newCount, owner: self)
|
||||
return Self(_unchecked: _pointer, byteCount: newCount)
|
||||
}
|
||||
|
||||
/// Returns a span over all but the given number of trailing bytes.
|
||||
@@ -541,7 +585,7 @@ extension RawSpan {
|
||||
public func extracting(droppingLast k: Int) -> Self {
|
||||
precondition(k >= 0, "Can't drop a negative number of elements.")
|
||||
let dc = min(k, byteCount)
|
||||
return Self(_unchecked: _pointer, byteCount: byteCount&-dc, owner: self)
|
||||
return Self(_unchecked: _pointer, byteCount: byteCount&-dc)
|
||||
}
|
||||
|
||||
/// Returns a span containing the trailing bytes of the span,
|
||||
@@ -564,7 +608,7 @@ extension RawSpan {
|
||||
precondition(maxLength >= 0, "Can't have a suffix of negative length.")
|
||||
let newCount = min(maxLength, byteCount)
|
||||
let newStart = _pointer?.advanced(by: byteCount&-newCount)
|
||||
return Self(_unchecked: newStart, byteCount: newCount, owner: self)
|
||||
return Self(_unchecked: newStart, byteCount: newCount)
|
||||
}
|
||||
|
||||
/// Returns a span over all but the given number of initial bytes.
|
||||
@@ -586,7 +630,7 @@ extension RawSpan {
|
||||
precondition(k >= 0, "Can't drop a negative number of elements.")
|
||||
let dc = min(k, byteCount)
|
||||
let newStart = _pointer?.advanced(by: dc)
|
||||
return Self(_unchecked: newStart, byteCount: byteCount&-dc, owner: self)
|
||||
return Self(_unchecked: newStart, byteCount: byteCount&-dc)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+75
-80
@@ -16,33 +16,37 @@ import Builtin
|
||||
// contains initialized instances of `Element`.
|
||||
@frozen
|
||||
public struct Span<Element: ~Copyable /*& ~Escapable*/>: Copyable, ~Escapable {
|
||||
@usableFromInline
|
||||
internal let _buffer: UnsafeBufferPointer<Element>
|
||||
@usableFromInline let _buffer: UnsafeBufferPointer<Element>
|
||||
|
||||
@usableFromInline @inline(__always)
|
||||
internal var _pointer: UnsafePointer<Element>? { _buffer.baseAddress }
|
||||
var _pointer: UnsafePointer<Element>? { _buffer.baseAddress }
|
||||
|
||||
@usableFromInline @inline(__always)
|
||||
internal var _start: UnsafePointer<Element> { _pointer.unsafelyUnwrapped }
|
||||
var _start: UnsafePointer<Element> { _pointer.unsafelyUnwrapped }
|
||||
|
||||
// @usableFromInline @inline(__always)
|
||||
// var _start: UnsafeRawPointer { _pointer.unsafelyUnwrapped }
|
||||
|
||||
@usableFromInline @inline(__always)
|
||||
internal var _count: Int { _buffer.count }
|
||||
var _count: Int { _buffer.count }
|
||||
|
||||
@inlinable @inline(__always)
|
||||
internal init<Owner: ~Copyable & ~Escapable>(
|
||||
_unchecked elements: UnsafeBufferPointer<Element>,
|
||||
owner: borrowing Owner
|
||||
) {
|
||||
internal init(
|
||||
_unchecked elements: UnsafeBufferPointer<Element>
|
||||
) -> dependsOn(immortal) Self {
|
||||
_buffer = elements
|
||||
// _pointer = .init(elements.baseAddress)
|
||||
// _count = elements.count
|
||||
}
|
||||
|
||||
@inlinable @inline(__always)
|
||||
internal init<Owner: ~Copyable & ~Escapable>(
|
||||
@_alwaysEmitIntoClient
|
||||
internal init(
|
||||
_unchecked start: UnsafePointer<Element>?,
|
||||
count: Int,
|
||||
owner: borrowing Owner
|
||||
) {
|
||||
self.init(_unchecked: .init(start: start, count: count), owner: owner)
|
||||
count: Int
|
||||
) -> dependsOn(immortal) Self {
|
||||
self.init(_unchecked: .init(start: start, count: count))
|
||||
// _pointer = .init(start)
|
||||
// _count = count
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,16 +72,15 @@ extension Span where Element: ~Copyable /*& ~Escapable*/ {
|
||||
/// - buffer: an `UnsafeBufferPointer` to initialized elements.
|
||||
/// - owner: a binding whose lifetime must exceed that of
|
||||
/// the newly created `Span`.
|
||||
@inlinable @inline(__always)
|
||||
public init<Owner: ~Copyable & ~Escapable>(
|
||||
unsafeElements buffer: UnsafeBufferPointer<Element>,
|
||||
owner: borrowing Owner
|
||||
) {
|
||||
@_alwaysEmitIntoClient
|
||||
public init(
|
||||
_unsafeElements buffer: UnsafeBufferPointer<Element>
|
||||
) -> dependsOn(immortal) Self {
|
||||
precondition(
|
||||
buffer.count == 0 || buffer.baseAddress.unsafelyUnwrapped.isAligned,
|
||||
"baseAddress must be properly aligned for accessing \(Element.self)"
|
||||
)
|
||||
self.init(_unchecked: buffer, owner: owner)
|
||||
self.init(_unchecked: buffer)
|
||||
}
|
||||
|
||||
/// Unsafely create a `Span` over initialized memory.
|
||||
@@ -90,11 +93,10 @@ extension Span where Element: ~Copyable /*& ~Escapable*/ {
|
||||
/// - owner: a binding whose lifetime must exceed that of
|
||||
/// the newly created `Span`.
|
||||
@_alwaysEmitIntoClient
|
||||
public init<Owner: ~Copyable & ~Escapable>(
|
||||
unsafeElements buffer: UnsafeMutableBufferPointer<Element>,
|
||||
owner: borrowing Owner
|
||||
) {
|
||||
self.init(unsafeElements: UnsafeBufferPointer(buffer), owner: owner)
|
||||
public init(
|
||||
_unsafeElements buffer: UnsafeMutableBufferPointer<Element>
|
||||
) -> dependsOn(immortal) Self {
|
||||
self.init(_unsafeElements: UnsafeBufferPointer(buffer))
|
||||
}
|
||||
|
||||
/// Unsafely create a `Span` over initialized memory.
|
||||
@@ -108,18 +110,17 @@ extension Span where Element: ~Copyable /*& ~Escapable*/ {
|
||||
/// - count: the number of initialized elements in the span.
|
||||
/// - owner: a binding whose lifetime must exceed that of
|
||||
/// the newly created `Span`.
|
||||
@inlinable @inline(__always)
|
||||
public init<Owner: ~Copyable & ~Escapable>(
|
||||
unsafeStart start: UnsafePointer<Element>,
|
||||
count: Int,
|
||||
owner: borrowing Owner
|
||||
) {
|
||||
@_alwaysEmitIntoClient
|
||||
public init(
|
||||
_unsafeStart start: UnsafePointer<Element>,
|
||||
count: Int
|
||||
) -> dependsOn(immortal) Self {
|
||||
precondition(count >= 0, "Count must not be negative")
|
||||
precondition(
|
||||
start.isAligned,
|
||||
"baseAddress must be properly aligned for accessing \(Element.self)"
|
||||
)
|
||||
self.init(_unchecked: start, count: count, owner: owner)
|
||||
self.init(_unchecked: start, count: count)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -134,12 +135,11 @@ extension Span where Element: BitwiseCopyable {
|
||||
/// - buffer: an `UnsafeBufferPointer` to initialized elements.
|
||||
/// - owner: a binding whose lifetime must exceed that of
|
||||
/// the newly created `Span`.
|
||||
@inlinable @inline(__always)
|
||||
public init<Owner: ~Copyable & ~Escapable>(
|
||||
unsafeElements buffer: UnsafeBufferPointer<Element>,
|
||||
owner: borrowing Owner
|
||||
) {
|
||||
self.init(_unchecked: buffer, owner: owner)
|
||||
@_alwaysEmitIntoClient
|
||||
public init(
|
||||
_unsafeElements buffer: UnsafeBufferPointer<Element>
|
||||
) -> dependsOn(immortal) Self {
|
||||
self.init(_unchecked: buffer)
|
||||
}
|
||||
|
||||
/// Unsafely create a `Span` over initialized memory.
|
||||
@@ -152,11 +152,10 @@ extension Span where Element: BitwiseCopyable {
|
||||
/// - owner: a binding whose lifetime must exceed that of
|
||||
/// the newly created `Span`.
|
||||
@_alwaysEmitIntoClient
|
||||
public init<Owner: ~Copyable & ~Escapable>(
|
||||
unsafeElements buffer: UnsafeMutableBufferPointer<Element>,
|
||||
owner: borrowing Owner
|
||||
) {
|
||||
self.init(unsafeElements: UnsafeBufferPointer(buffer), owner: owner)
|
||||
public init(
|
||||
_unsafeElements buffer: UnsafeMutableBufferPointer<Element>
|
||||
) -> dependsOn(immortal) Self {
|
||||
self.init(_unsafeElements: UnsafeBufferPointer(buffer))
|
||||
}
|
||||
|
||||
/// Unsafely create a `Span` over initialized memory.
|
||||
@@ -170,14 +169,13 @@ extension Span where Element: BitwiseCopyable {
|
||||
/// - count: the number of initialized elements in the span.
|
||||
/// - owner: a binding whose lifetime must exceed that of
|
||||
/// the newly created `Span`.
|
||||
@inlinable @inline(__always)
|
||||
public init<Owner: ~Copyable & ~Escapable>(
|
||||
unsafeStart start: UnsafePointer<Element>,
|
||||
count: Int,
|
||||
owner: borrowing Owner
|
||||
) {
|
||||
@_alwaysEmitIntoClient
|
||||
public init(
|
||||
_unsafeStart start: UnsafePointer<Element>,
|
||||
count: Int
|
||||
) -> dependsOn(immortal) Self {
|
||||
precondition(count >= 0, "Count must not be negative")
|
||||
self.init(_unchecked: start, count: count, owner: owner)
|
||||
self.init(_unchecked: start, count: count)
|
||||
}
|
||||
|
||||
/// Unsafely create a `Span` over initialized memory.
|
||||
@@ -194,19 +192,17 @@ extension Span where Element: BitwiseCopyable {
|
||||
/// - type: the type to use when interpreting the bytes in memory.
|
||||
/// - owner: a binding whose lifetime must exceed that of
|
||||
/// the newly created `Span`.
|
||||
@inlinable @inline(__always)
|
||||
public init<Owner: ~Copyable & ~Escapable>(
|
||||
unsafeBytes buffer: UnsafeRawBufferPointer,
|
||||
owner: borrowing Owner
|
||||
) {
|
||||
@_alwaysEmitIntoClient
|
||||
public init(
|
||||
_unsafeBytes buffer: UnsafeRawBufferPointer
|
||||
) -> dependsOn(immortal) Self {
|
||||
let (byteCount, stride) = (buffer.count, MemoryLayout<Element>.stride)
|
||||
assert(byteCount >= 0, "Count must not be negative")
|
||||
precondition(byteCount >= 0, "Count must not be negative")
|
||||
let (count, remainder) = byteCount.quotientAndRemainder(dividingBy: stride)
|
||||
precondition(remainder == 0)
|
||||
self.init(
|
||||
_unchecked: buffer.baseAddress?.assumingMemoryBound(to: Element.self),
|
||||
count: count,
|
||||
owner: owner
|
||||
count: count
|
||||
)
|
||||
}
|
||||
|
||||
@@ -225,11 +221,10 @@ extension Span where Element: BitwiseCopyable {
|
||||
/// - owner: a binding whose lifetime must exceed that of
|
||||
/// the newly created `Span`.
|
||||
@_alwaysEmitIntoClient
|
||||
public init<Owner: ~Copyable & ~Escapable>(
|
||||
unsafeBytes buffer: UnsafeMutableRawBufferPointer,
|
||||
owner: borrowing Owner
|
||||
) {
|
||||
self.init(unsafeBytes: UnsafeRawBufferPointer(buffer), owner: owner)
|
||||
public init(
|
||||
_unsafeBytes buffer: UnsafeMutableRawBufferPointer
|
||||
) -> dependsOn(immortal) Self {
|
||||
self.init(_unsafeBytes: UnsafeRawBufferPointer(buffer))
|
||||
}
|
||||
|
||||
/// Unsafely create a `Span` over initialized memory.
|
||||
@@ -243,20 +238,18 @@ extension Span where Element: BitwiseCopyable {
|
||||
/// - count: the number of initialized elements in the span.
|
||||
/// - owner: a binding whose lifetime must exceed that of
|
||||
/// the newly created `Span`.
|
||||
@inlinable @inline(__always)
|
||||
public init<Owner: ~Copyable & ~Escapable>(
|
||||
unsafeStart pointer: UnsafeRawPointer,
|
||||
byteCount: Int,
|
||||
owner: borrowing Owner
|
||||
) {
|
||||
@_alwaysEmitIntoClient
|
||||
public init(
|
||||
_unsafeStart pointer: UnsafeRawPointer,
|
||||
byteCount: Int
|
||||
) -> dependsOn(immortal) Self {
|
||||
precondition(byteCount >= 0, "Count must not be negative")
|
||||
let stride = MemoryLayout<Element>.stride
|
||||
let (count, remainder) = byteCount.quotientAndRemainder(dividingBy: stride)
|
||||
precondition(remainder == 0)
|
||||
self.init(
|
||||
_unchecked: pointer.assumingMemoryBound(to: Element.self),
|
||||
count: count,
|
||||
owner: owner
|
||||
count: count
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -459,7 +452,10 @@ extension Span where Element: ~Copyable /*& ~Escapable*/ {
|
||||
@inlinable @inline(__always)
|
||||
public subscript(unchecked position: Int) -> Element {
|
||||
_read {
|
||||
yield _start.advanced(by: position).pointee
|
||||
let element = UnsafeRawPointer(_start).advanced(by: position&*MemoryLayout<Element>.stride)
|
||||
let binding = Builtin.bindMemory(element._rawValue, count._builtinWordValue, Element.self)
|
||||
defer { Builtin.rebindMemory(element._rawValue, binding) }
|
||||
yield UnsafePointer<Element>(element._rawValue).pointee
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -491,7 +487,7 @@ extension Span where Element: BitwiseCopyable {
|
||||
@inlinable @inline(__always)
|
||||
public subscript(unchecked position: Int) -> Element {
|
||||
get {
|
||||
let address = UnsafeRawPointer(_start.advanced(by: position))
|
||||
let address = UnsafeRawPointer(_start).advanced(by: position&*MemoryLayout<Element>.stride)
|
||||
return address.loadUnaligned(as: Element.self)
|
||||
}
|
||||
}
|
||||
@@ -538,8 +534,7 @@ extension Span where Element: ~Copyable /*& ~Escapable*/ {
|
||||
public func extracting(unchecked bounds: Range<Int>) -> Self {
|
||||
Span(
|
||||
_unchecked: _pointer?.advanced(by: bounds.lowerBound),
|
||||
count: bounds.count,
|
||||
owner: self
|
||||
count: bounds.count
|
||||
)
|
||||
}
|
||||
|
||||
@@ -730,7 +725,7 @@ extension Span where Element: ~Copyable /*& ~Escapable*/ {
|
||||
public func extracting(first maxLength: Int) -> Self {
|
||||
precondition(maxLength >= 0, "Can't have a prefix of negative length.")
|
||||
let newCount = min(maxLength, count)
|
||||
return Self(_unchecked: _pointer, count: newCount, owner: self)
|
||||
return Self(_unchecked: _pointer, count: newCount)
|
||||
}
|
||||
|
||||
/// Returns a span over all but the given number of trailing elements.
|
||||
@@ -751,7 +746,7 @@ extension Span where Element: ~Copyable /*& ~Escapable*/ {
|
||||
public func extracting(droppingLast k: Int) -> Self {
|
||||
precondition(k >= 0, "Can't drop a negative number of elements.")
|
||||
let droppedCount = min(k, count)
|
||||
return Self(_unchecked: _pointer, count: count&-droppedCount, owner: self)
|
||||
return Self(_unchecked: _pointer, count: count&-droppedCount)
|
||||
}
|
||||
|
||||
/// Returns a span containing the final elements of the span,
|
||||
@@ -774,7 +769,7 @@ extension Span where Element: ~Copyable /*& ~Escapable*/ {
|
||||
precondition(maxLength >= 0, "Can't have a suffix of negative length.")
|
||||
let newCount = min(maxLength, count)
|
||||
let newStart = _pointer?.advanced(by: count&-newCount)
|
||||
return Self(_unchecked: newStart, count: newCount, owner: self)
|
||||
return Self(_unchecked: newStart, count: newCount)
|
||||
}
|
||||
|
||||
/// Returns a span over all but the given number of initial elements.
|
||||
@@ -796,6 +791,6 @@ extension Span where Element: ~Copyable /*& ~Escapable*/ {
|
||||
precondition(k >= 0, "Can't drop a negative number of elements.")
|
||||
let droppedCount = min(k, count)
|
||||
let newStart = _pointer?.advanced(by: droppedCount)
|
||||
return Self(_unchecked: newStart, count: count&-droppedCount, owner: self)
|
||||
return Self(_unchecked: newStart, count: count&-droppedCount)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,431 @@
|
||||
//===--- StdlibSpanExtensions.swift ---------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift.org open source project
|
||||
//
|
||||
// Copyright (c) 2024 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
|
||||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
extension UnsafeBufferPointer where Element: ~Copyable {
|
||||
public func withSpan<E: Error, Result: ~Copyable>(
|
||||
_ body: (_ elements: Span<Element>) throws(E) -> Result
|
||||
) throws(E) -> Result {
|
||||
try body(Span(_unsafeElements: self))
|
||||
}
|
||||
|
||||
public func withBytes<E: Error, Result: ~Copyable>(
|
||||
_ body: (_ elements: RawSpan) throws(E) -> Result
|
||||
) throws(E) -> Result where Element: BitwiseCopyable {
|
||||
try body(RawSpan(_unsafeBytes: UnsafeRawBufferPointer(self)))
|
||||
}
|
||||
}
|
||||
|
||||
extension UnsafeMutableBufferPointer where Element: ~Copyable {
|
||||
public func withSpan<E: Error, Result: ~Copyable>(
|
||||
_ body: (_ elements: Span<Element>) throws(E) -> Result
|
||||
) throws(E) -> Result {
|
||||
try body(Span(_unsafeElements: self))
|
||||
}
|
||||
|
||||
public func withBytes<E: Error, Result: ~Copyable>(
|
||||
_ body: (_ elements: RawSpan) throws(E) -> Result
|
||||
) throws(E) -> Result where Element: BitwiseCopyable {
|
||||
try body(RawSpan(_unsafeBytes: UnsafeRawBufferPointer(self)))
|
||||
}
|
||||
}
|
||||
|
||||
extension UnsafeRawBufferPointer {
|
||||
public func withBytes<E: Error, Result: ~Copyable>(
|
||||
_ body: (_ elements: RawSpan) throws(E) -> Result
|
||||
) throws(E) -> Result {
|
||||
try body(RawSpan(_unsafeBytes: self))
|
||||
}
|
||||
}
|
||||
|
||||
extension UnsafeMutableRawBufferPointer {
|
||||
public func withBytes<E: Error, Result: ~Copyable>(
|
||||
_ body: (_ elements: RawSpan) throws(E) -> Result
|
||||
) throws(E) -> Result {
|
||||
try body(RawSpan(_unsafeBytes: self))
|
||||
}
|
||||
}
|
||||
|
||||
extension Slice {
|
||||
public func withSpan<Element, E: Error, Result: ~Copyable>(
|
||||
_ body: (_ elements: Span<Element>) throws(E) -> Result
|
||||
) throws(E) -> Result
|
||||
where Base == UnsafeBufferPointer<Element> {
|
||||
try body(Span(_unsafeElements: UnsafeBufferPointer(rebasing: self)))
|
||||
}
|
||||
|
||||
public func withBytes<Element: BitwiseCopyable, E: Error, Result: ~Copyable>(
|
||||
_ body: (_ elements: RawSpan) throws(E) -> Result
|
||||
) throws(E) -> Result
|
||||
where Base == UnsafeBufferPointer<Element> {
|
||||
try body(RawSpan(_unsafeBytes: .init(UnsafeBufferPointer(rebasing: self))))
|
||||
}
|
||||
|
||||
public func withSpan<Element, E: Error, Result: ~Copyable>(
|
||||
_ body: (_ elements: Span<Element>) throws(E) -> Result
|
||||
) throws(E) -> Result
|
||||
where Base == UnsafeMutableBufferPointer<Element> {
|
||||
try body(Span(_unsafeElements: UnsafeBufferPointer(rebasing: self)))
|
||||
}
|
||||
|
||||
public func withBytes<Element: BitwiseCopyable, E: Error, Result: ~Copyable>(
|
||||
_ body: (_ elements: RawSpan) throws(E) -> Result
|
||||
) throws(E) -> Result
|
||||
where Base == UnsafeMutableBufferPointer<Element> {
|
||||
try body(RawSpan(_unsafeBytes: .init(UnsafeBufferPointer(rebasing: self))))
|
||||
}
|
||||
|
||||
public func withBytes<E: Error, Result: ~Copyable>(
|
||||
_ body: (_ elements: RawSpan) throws(E) -> Result
|
||||
) throws(E) -> Result
|
||||
where Base == UnsafeRawBufferPointer {
|
||||
try body(RawSpan(_unsafeBytes: UnsafeRawBufferPointer(rebasing: self)))
|
||||
}
|
||||
|
||||
public func withBytes<E: Error, Result: ~Copyable>(
|
||||
_ body: (_ elements: RawSpan) throws(E) -> Result
|
||||
) throws(E) -> Result
|
||||
where Base == UnsafeMutableRawBufferPointer {
|
||||
try body(RawSpan(_unsafeBytes: UnsafeRawBufferPointer(rebasing: self)))
|
||||
}
|
||||
}
|
||||
|
||||
#if false //FIXME: rdar://134382237
|
||||
import struct Foundation.Data
|
||||
|
||||
extension Data {
|
||||
public func withSpan<E: Error, Result>(
|
||||
_ body: (_ elements: Span<UInt8>) throws(E) -> Result
|
||||
) throws(E) -> Result {
|
||||
let result: Swift.Result<Result, E> = withUnsafeBytes {
|
||||
do throws(E) {
|
||||
return .success(try body(Span<UInt8>(_unsafeBytes: $0)))
|
||||
} catch {
|
||||
return .failure(error)
|
||||
}
|
||||
}
|
||||
switch result {
|
||||
case .success(let s): return s
|
||||
case .failure(let e): throw e
|
||||
}
|
||||
}
|
||||
|
||||
public func withBytes<E: Error, Result>(
|
||||
_ body: (_ bytes: RawSpan) throws(E) -> Result
|
||||
) throws(E) -> Result {
|
||||
let result: Swift.Result<Result, E> = withUnsafeBytes {
|
||||
do throws(E) {
|
||||
return .success(try body(RawSpan(_unsafeBytes: $0)))
|
||||
} catch {
|
||||
return .failure(error)
|
||||
}
|
||||
}
|
||||
switch result {
|
||||
case .success(let s): return s
|
||||
case .failure(let e): throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
extension Array {
|
||||
|
||||
/// Calls a closure with a `Span` of the array's contiguous storage.
|
||||
///
|
||||
/// Often, the optimizer can eliminate bounds checks within an array
|
||||
/// algorithm, but when that fails, invoking the same algorithm on the
|
||||
/// buffer pointer passed into your closure lets you trade safety for speed.
|
||||
///
|
||||
/// The following example shows how you can iterate over the contents of the
|
||||
/// buffer pointer:
|
||||
///
|
||||
/// let numbers = [1, 2, 3, 4, 5]
|
||||
/// let sum = numbers.withUnsafeBufferPointer { buffer -> Int in
|
||||
/// var result = 0
|
||||
/// for i in stride(from: buffer.startIndex, to: buffer.endIndex, by: 2) {
|
||||
/// result += buffer[i]
|
||||
/// }
|
||||
/// return result
|
||||
/// }
|
||||
/// // 'sum' == 9
|
||||
///
|
||||
/// The pointer passed as an argument to `body` is valid only during the
|
||||
/// execution of `withUnsafeBufferPointer(_:)`. Do not store or return the
|
||||
/// pointer for later use.
|
||||
///
|
||||
/// - Parameter body: A closure with an `UnsafeBufferPointer` parameter that
|
||||
/// points to the contiguous storage for the array. If no such storage exists, it is created. If
|
||||
/// `body` has a return value, that value is also used as the return value
|
||||
/// for the `withUnsafeBufferPointer(_:)` method. The pointer argument is
|
||||
/// valid only for the duration of the method's execution.
|
||||
/// - Returns: The return value, if any, of the `body` closure parameter.
|
||||
public func withSpan<E: Error, Result: ~Copyable>(
|
||||
_ body: (_ elements: Span<Element>) throws(E) -> Result
|
||||
) throws(E) -> Result {
|
||||
let result = withUnsafeTemporaryAllocation(
|
||||
of: Swift.Result<Result, E>.self, capacity: 1
|
||||
) {
|
||||
buffer -> Swift.Result<Result, E> in
|
||||
self.withUnsafeBufferPointer {
|
||||
elements in
|
||||
do throws(E) {
|
||||
let result = try body(Span(_unsafeElements: elements))
|
||||
buffer.initializeElement(at: 0, to: .success(result))
|
||||
} catch {
|
||||
buffer.initializeElement(at: 0, to: .failure(error))
|
||||
}
|
||||
}
|
||||
return buffer.moveElement(from: 0)
|
||||
}
|
||||
switch consume result {
|
||||
case .success(let s): return s
|
||||
case .failure(let e): throw e
|
||||
}
|
||||
}
|
||||
|
||||
public func withBytes<E: Error, Result>(
|
||||
_ body: (_ bytes: RawSpan) throws(E) -> Result
|
||||
) throws(E) -> Result where Element: BitwiseCopyable {
|
||||
let result: Swift.Result<Result, E> = withUnsafeBytes {
|
||||
do throws(E) {
|
||||
return .success(try body(RawSpan(_unsafeBytes: $0)))
|
||||
} catch {
|
||||
return .failure(error)
|
||||
}
|
||||
}
|
||||
switch result {
|
||||
case .success(let s): return s
|
||||
case .failure(let e): throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension ContiguousArray {
|
||||
public func withSpan<E: Error, Result>(
|
||||
_ body: (_ elements: Span<Element>) throws(E) -> Result
|
||||
) throws(E) -> Result {
|
||||
let result: Swift.Result<Result, E> = withUnsafeBufferPointer {
|
||||
do throws(E) {
|
||||
return .success(try body(Span(_unsafeElements: $0)))
|
||||
} catch {
|
||||
return .failure(error)
|
||||
}
|
||||
}
|
||||
switch result {
|
||||
case .success(let s): return s
|
||||
case .failure(let e): throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension ContiguousArray where Element: BitwiseCopyable {
|
||||
public func withBytes<E: Error, Result>(
|
||||
_ body: (_ bytes: RawSpan) throws(E) -> Result
|
||||
) throws(E) -> Result {
|
||||
let result: Swift.Result<Result, E> = withUnsafeBytes {
|
||||
do throws(E) {
|
||||
return .success(try body(RawSpan(_unsafeBytes: $0)))
|
||||
} catch {
|
||||
return .failure(error)
|
||||
}
|
||||
}
|
||||
switch result {
|
||||
case .success(let s): return s
|
||||
case .failure(let e): throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension ArraySlice {
|
||||
public func withSpan<E, Result>(
|
||||
_ body: (_ elements: Span<Element>) throws(E) -> Result
|
||||
) throws(E) -> Result {
|
||||
let result: Swift.Result<Result, E> = withUnsafeBufferPointer {
|
||||
do throws(E) {
|
||||
let span = Span(_unsafeElements: $0)
|
||||
print(span._start, span.count)
|
||||
return .success(try body(span))
|
||||
} catch {
|
||||
return .failure(error)
|
||||
}
|
||||
}
|
||||
switch result {
|
||||
case .success(let s): return s
|
||||
case .failure(let e): throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension ArraySlice where Element: BitwiseCopyable {
|
||||
public func withBytes<E: Error, Result>(
|
||||
_ body: (_ bytes: RawSpan) throws(E) -> Result
|
||||
) throws(E) -> Result {
|
||||
let result: Swift.Result<Result, E> = withUnsafeBytes {
|
||||
do throws(E) {
|
||||
return .success(try body(RawSpan(_unsafeBytes: $0)))
|
||||
} catch {
|
||||
return .failure(error)
|
||||
}
|
||||
}
|
||||
switch result {
|
||||
case .success(let s): return s
|
||||
case .failure(let e): throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension String.UTF8View {
|
||||
public func withSpan<E: Error, Result>(
|
||||
_ body: (_ elements: Span<UTF8.CodeUnit>) throws(E) -> Result
|
||||
) throws(E) -> Result {
|
||||
let result: Swift.Result<Result, E>? = withContiguousStorageIfAvailable {
|
||||
do throws(E) {
|
||||
return .success(try body(Span(_unsafeElements: $0)))
|
||||
} catch {
|
||||
return .failure(error)
|
||||
}
|
||||
}
|
||||
switch result {
|
||||
case .success(let s)?: return s
|
||||
case .failure(let e)?: throw e
|
||||
case nil: return try ContiguousArray(self).withSpan(body)
|
||||
}
|
||||
}
|
||||
|
||||
public func withBytes<E: Error, Result>(
|
||||
_ body: (_ bytes: RawSpan) throws(E) -> Result
|
||||
) throws(E) -> Result {
|
||||
let result: Swift.Result<Result, E>? = withContiguousStorageIfAvailable {
|
||||
do throws(E) {
|
||||
return .success(try body(RawSpan(_unsafeElements: $0)))
|
||||
} catch {
|
||||
return .failure(error)
|
||||
}
|
||||
}
|
||||
switch result {
|
||||
case .success(let s)?: return s
|
||||
case .failure(let e)?: throw e
|
||||
case nil: return try ContiguousArray(self).withBytes(body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension Substring.UTF8View {
|
||||
public func withSpan<E: Error, Result>(
|
||||
_ body: (_ elements: Span<UTF8.CodeUnit>) throws(E) -> Result
|
||||
) throws(E) -> Result {
|
||||
let result: Swift.Result<Result, E>? = withContiguousStorageIfAvailable {
|
||||
do throws(E) {
|
||||
return .success(try body(Span(_unsafeElements: $0)))
|
||||
} catch {
|
||||
return .failure(error)
|
||||
}
|
||||
}
|
||||
switch result {
|
||||
case .success(let s)?: return s
|
||||
case .failure(let e)?: throw e
|
||||
case nil: return try ContiguousArray(self).withSpan(body)
|
||||
}
|
||||
}
|
||||
|
||||
public func withBytes<E: Error, Result>(
|
||||
_ body: (_ bytes: RawSpan) throws(E) -> Result
|
||||
) throws(E) -> Result {
|
||||
let result: Swift.Result<Result, E>? = withContiguousStorageIfAvailable {
|
||||
do throws(E) {
|
||||
return .success(try body(RawSpan(_unsafeElements: $0)))
|
||||
} catch {
|
||||
return .failure(error)
|
||||
}
|
||||
}
|
||||
switch result {
|
||||
case .success(let s)?: return s
|
||||
case .failure(let e)?: throw e
|
||||
case nil: return try ContiguousArray(self).withBytes(body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension CollectionOfOne {
|
||||
public func withSpan<E: Error, Result>(
|
||||
_ body: (_ elements: Span<Element>) throws(E) -> Result
|
||||
) throws(E) -> Result {
|
||||
var collection = self
|
||||
let result: Swift.Result<Result, E> = withUnsafePointer(to: &collection) {
|
||||
$0.withMemoryRebound(to: Element.self, capacity: 1) {
|
||||
do throws(E) {
|
||||
return .success(try body(Span(_unsafeStart: $0, count: 1)))
|
||||
} catch {
|
||||
return .failure(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
switch result {
|
||||
case .success(let s): return s
|
||||
case .failure(let e): throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension CollectionOfOne where Element: BitwiseCopyable {
|
||||
public func withBytes<E: Error, Result>(
|
||||
_ body: (_ bytes: RawSpan) throws(E) -> Result
|
||||
) throws(E) -> Result {
|
||||
var collection = self
|
||||
let r: Swift.Result<Result, E> = Swift.withUnsafeBytes(of: &collection) {
|
||||
do throws(E) {
|
||||
return .success(try body(RawSpan(_unsafeBytes: $0)))
|
||||
} catch {
|
||||
return .failure(error)
|
||||
}
|
||||
}
|
||||
switch r {
|
||||
case .success(let s): return s
|
||||
case .failure(let e): throw e
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension KeyValuePairs {
|
||||
public func withSpan<E: Error, Result>(
|
||||
_ body: (
|
||||
_ elements: Span<(key: Key, value: Value)>
|
||||
) throws(E) -> Result
|
||||
) throws(E) -> Result {
|
||||
try Array(self).withSpan(body)
|
||||
}
|
||||
}
|
||||
|
||||
extension KeyValuePairs where Element: BitwiseCopyable {
|
||||
public func withBytes<E: Error, Result>(
|
||||
_ body: (_ bytes: RawSpan) throws(E) -> Result
|
||||
) throws(E) -> Result {
|
||||
try Array(self).withBytes(body)
|
||||
}
|
||||
}
|
||||
|
||||
extension Span where Element: ~Copyable /*& ~Escapable*/ {
|
||||
public consuming func withSpan<E: Error, Result>(
|
||||
_ body: (_ elements: Span<Element>) throws(E) -> Result
|
||||
) throws(E) -> Result {
|
||||
try body(self)
|
||||
}
|
||||
}
|
||||
|
||||
extension Span where Element: BitwiseCopyable {
|
||||
public consuming func withBytes<E: Error, Result>(
|
||||
_ body: (_ elements: RawSpan) throws(E) -> Result
|
||||
) throws(E) -> Result {
|
||||
try body(RawSpan(self))
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: extend SIMD vectors with `withSpan` and with `withBytes`.
|
||||
Reference in new issue
Block a user