Skip to content

Commit 7a4df7f

Browse files
committed
[stdlib] add InlineArray.mutableSpan
1 parent 3719883 commit 7a4df7f

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

stdlib/public/core/InlineArray.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,17 @@ extension InlineArray where Element: ~Copyable {
473473
return unsafe _overrideLifetime(span, borrowing: self)
474474
}
475475
}
476+
477+
@available(SwiftStdlib 6.2, *)
478+
public var mutableSpan: MutableSpan<Element> {
479+
@lifetime(borrow self)
480+
@_alwaysEmitIntoClient
481+
mutating get {
482+
let pointer = _mutableAddress
483+
let span = unsafe MutableSpan(_unsafeStart: pointer, count: count)
484+
return unsafe _overrideLifetime(span, mutating: &self)
485+
}
486+
}
476487
}
477488

478489
//===----------------------------------------------------------------------===//

test/stdlib/Span/InlineSpanProperties.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,31 @@ suite.test("InlineArray.span property (String)")
118118
expectEqual(span[i], s[i])
119119
}
120120
}
121+
122+
suite.test("InlineArray.mutableSpan property")
123+
.require(.stdlib_6_2).code
124+
{
125+
guard #available(SwiftStdlib 6.2, *) else { return }
126+
127+
var v = InlineArray<5, Int>(repeating: 0)
128+
let c = v.count
129+
var span = v.mutableSpan
130+
expectEqual(span.count, c)
131+
span[3] = Int.random(in: .min..<0)
132+
let r = span[3]
133+
expectEqual(v[3], r)
134+
}
135+
136+
suite.test("InlineArray.mutableSpan property (String)")
137+
.require(.stdlib_6_2).code
138+
{
139+
guard #available(SwiftStdlib 6.2, *) else { return }
140+
141+
var v = InlineArray<5, String>(repeating: "0")
142+
let c = v.count
143+
var span = v.mutableSpan
144+
expectEqual(span.count, c)
145+
span[3] = String(repeating: "0", count: Int.random(in: 100..<500))
146+
let s = span[3]
147+
expectTrue(s._isIdentical(to: v[3]))
148+
}

0 commit comments

Comments
 (0)