Skip to content

Commit 4d858d5

Browse files
committed
[stdlib] add span properties to array types
1 parent b724a65 commit 4d858d5

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

stdlib/public/core/Array.swift

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -1427,7 +1427,7 @@ extension Array: RangeReplaceableCollection {
14271427
return try unsafe body(bufferPointer)
14281428
}
14291429
}
1430-
1430+
14311431
@inlinable
14321432
public __consuming func _copyToContiguousArray() -> ContiguousArray<Element> {
14331433
if let n = _buffer.requestNativeBuffer() {
@@ -1611,6 +1611,27 @@ extension Array {
16111611
return try unsafe _buffer.withUnsafeBufferPointer(body)
16121612
}
16131613

1614+
@available(SwiftStdlib 6.2, *)
1615+
public var span: Span<Element> {
1616+
@lifetime(borrow self)
1617+
@_alwaysEmitIntoClient
1618+
borrowing get {
1619+
#if _runtime(_ObjC)
1620+
if _slowPath(!_buffer._isNative) {
1621+
let buffer = _buffer.getOrAllocateAssociatedObjectBuffer()
1622+
let pointer = unsafe buffer.firstElementAddress
1623+
let count = buffer.immutableCount
1624+
let span = unsafe Span(_unsafeStart: pointer, count: count)
1625+
return unsafe _overrideLifetime(span, borrowing: self)
1626+
}
1627+
#endif
1628+
let pointer = unsafe _buffer.firstElementAddress
1629+
let count = _buffer.immutableCount
1630+
let span = unsafe Span(_unsafeStart: pointer, count: count)
1631+
return unsafe _overrideLifetime(span, borrowing: self)
1632+
}
1633+
}
1634+
16141635
// Superseded by the typed-throws version of this function, but retained
16151636
// for ABI reasons.
16161637
@_semantics("array.withUnsafeMutableBufferPointer")

stdlib/public/core/ArraySlice.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -1210,6 +1210,17 @@ extension ArraySlice {
12101210
return try _buffer.withUnsafeBufferPointer(body)
12111211
}
12121212

1213+
@available(SwiftStdlib 6.2, *)
1214+
public var span: Span<Element> {
1215+
@lifetime(borrow self)
1216+
@_alwaysEmitIntoClient
1217+
borrowing get {
1218+
let (pointer, count) = unsafe (_buffer.firstElementAddress, _buffer.count)
1219+
let span = unsafe Span(_unsafeStart: pointer, count: count)
1220+
return unsafe _overrideLifetime(span, borrowing: self)
1221+
}
1222+
}
1223+
12131224
// Superseded by the typed-throws version of this function, but retained
12141225
// for ABI reasons.
12151226
@_semantics("array.withUnsafeMutableBufferPointer")

stdlib/public/core/ContiguousArray.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -1152,6 +1152,18 @@ extension ContiguousArray {
11521152
return try unsafe _buffer.withUnsafeBufferPointer(body)
11531153
}
11541154

1155+
@available(SwiftStdlib 6.2, *)
1156+
public var span: Span<Element> {
1157+
@lifetime(borrow self)
1158+
@_alwaysEmitIntoClient
1159+
borrowing get {
1160+
let pointer = unsafe _buffer.firstElementAddress
1161+
let count = _buffer.immutableCount
1162+
let span = unsafe Span(_unsafeStart: pointer, count: count)
1163+
return unsafe _overrideLifetime(span, borrowing: self)
1164+
}
1165+
}
1166+
11551167
// Superseded by the typed-throws version of this function, but retained
11561168
// for ABI reasons.
11571169
@_semantics("array.withUnsafeMutableBufferPointer")

0 commit comments

Comments
 (0)