Skip to content

Commit 70a6c65

Browse files
committed
[stdlib] add mutableSpan to array types
1 parent 6b73161 commit 70a6c65

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

stdlib/public/core/Array.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,6 +1730,19 @@ extension Array {
17301730
return try unsafe body(&inoutBufferPointer)
17311731
}
17321732

1733+
@available(SwiftStdlib 6.2, *)
1734+
public var mutableSpan: MutableSpan<Element> {
1735+
@lifetime(/*inout*/borrow self)
1736+
@_alwaysEmitIntoClient
1737+
mutating get {
1738+
_makeMutableAndUnique()
1739+
let pointer = unsafe _buffer.firstElementAddress
1740+
let count = _buffer.mutableCount
1741+
let span = unsafe MutableSpan(_unsafeStart: pointer, count: count)
1742+
return unsafe _overrideLifetime(span, mutating: &self)
1743+
}
1744+
}
1745+
17331746
@inlinable
17341747
public __consuming func _copyContents(
17351748
initializing buffer: UnsafeMutableBufferPointer<Element>

stdlib/public/core/ArraySlice.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,6 +1300,18 @@ extension ArraySlice {
13001300
return try unsafe body(&inoutBufferPointer)
13011301
}
13021302

1303+
@available(SwiftStdlib 6.2, *)
1304+
public var mutableSpan: MutableSpan<Element> {
1305+
@lifetime(/*inout*/borrow self)
1306+
@_alwaysEmitIntoClient
1307+
mutating get {
1308+
_makeMutableAndUnique()
1309+
let (pointer, count) = unsafe (_buffer.firstElementAddress, _buffer.count)
1310+
let span = unsafe MutableSpan(_unsafeStart: pointer, count: count)
1311+
return unsafe _overrideLifetime(span, mutating: &self)
1312+
}
1313+
}
1314+
13031315
@inlinable
13041316
public __consuming func _copyContents(
13051317
initializing buffer: UnsafeMutableBufferPointer<Element>

stdlib/public/core/ContiguousArray.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,6 +1242,22 @@ extension ContiguousArray {
12421242
return try unsafe body(&inoutBufferPointer)
12431243
}
12441244

1245+
@available(SwiftStdlib 6.2, *)
1246+
public var mutableSpan: MutableSpan<Element> {
1247+
@lifetime(/*inout*/borrow self)
1248+
@_alwaysEmitIntoClient
1249+
mutating get {
1250+
_makeMutableAndUnique()
1251+
// NOTE: We don't have the ability to schedule a call to
1252+
// ContiguousArrayBuffer.endCOWMutation().
1253+
// rdar://146785284 (lifetime analysis for end of mutation)
1254+
let pointer = unsafe _buffer.firstElementAddress
1255+
let count = _buffer.mutableCount
1256+
let span = unsafe MutableSpan(_unsafeStart: pointer, count: count)
1257+
return unsafe _overrideLifetime(span, mutating: &self)
1258+
}
1259+
}
1260+
12451261
@inlinable
12461262
public __consuming func _copyContents(
12471263
initializing buffer: UnsafeMutableBufferPointer<Element>

0 commit comments

Comments
 (0)