Skip to content

Commit 7bbb834

Browse files
committed
[stdlib] add span properties to array types
1 parent acdfb37 commit 7bbb834

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

stdlib/public/core/Array.swift

Lines changed: 20 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
@@ -1427,6 +1427,25 @@ extension Array: RangeReplaceableCollection {
14271427
return try unsafe body(bufferPointer)
14281428
}
14291429
}
1430+
1431+
@available(SwiftStdlib 6.2, *)
1432+
public var span: Span<Element> {
1433+
@lifetime(borrow self)
1434+
@_alwaysEmitIntoClient
1435+
borrowing get {
1436+
#if _runtime(_ObjC)
1437+
if _slowPath(!_buffer._isNative) {
1438+
let buffer = _buffer.getOrAllocateAssociatedObjectBuffer()
1439+
let (pointer, count) = (buffer.firstElementAddress, buffer.count)
1440+
let span = Span(_unsafeStart: pointer, count: count)
1441+
return _overrideLifetime(span, borrowing: self)
1442+
}
1443+
#endif
1444+
let (pointer, count) = (_buffer.firstElementAddress, _buffer.count)
1445+
let span = Span(_unsafeStart: pointer, count: count)
1446+
return _overrideLifetime(span, borrowing: self)
1447+
}
1448+
}
14301449

14311450
@inlinable
14321451
public __consuming func _copyToContiguousArray() -> ContiguousArray<Element> {

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
@@ -1116,6 +1116,17 @@ extension ArraySlice: RangeReplaceableCollection {
11161116
}
11171117
}
11181118

1119+
@available(SwiftStdlib 6.2, *)
1120+
public var span: Span<Element> {
1121+
@lifetime(borrow self)
1122+
@_alwaysEmitIntoClient
1123+
borrowing get {
1124+
let (pointer, count) = (_buffer.firstElementAddress, _buffer.count)
1125+
let span = Span(_unsafeStart: pointer, count: count)
1126+
return _overrideLifetime(span, borrowing: self)
1127+
}
1128+
}
1129+
11191130
@inlinable
11201131
public __consuming func _copyToContiguousArray() -> ContiguousArray<Element> {
11211132
if let n = _buffer.requestNativeBuffer() {

stdlib/public/core/ContiguousArray.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
@@ -1021,6 +1021,17 @@ extension ContiguousArray: RangeReplaceableCollection {
10211021
}
10221022
}
10231023

1024+
@available(SwiftStdlib 6.2, *)
1025+
public var span: Span<Element> {
1026+
@lifetime(borrow self)
1027+
@_alwaysEmitIntoClient
1028+
borrowing get {
1029+
let (pointer, count) = (_buffer.firstElementAddress, _buffer.count)
1030+
let span = Span(_unsafeStart: pointer, count: count)
1031+
return _overrideLifetime(span, borrowing: self)
1032+
}
1033+
}
1034+
10241035
@inlinable
10251036
public __consuming func _copyToContiguousArray() -> ContiguousArray<Element> {
10261037
if let n = _buffer.requestNativeBuffer() {

0 commit comments

Comments
 (0)