Skip to content

Commit 3719883

Browse files
committed
[stdlib] add CollectionOfOne.mutableSpan
1 parent 4abf49a commit 3719883

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

stdlib/public/core/CollectionOfOne.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,19 @@ extension CollectionOfOne {
171171
return unsafe _overrideLifetime(span, borrowing: self)
172172
}
173173
}
174+
175+
@available(SwiftStdlib 6.2, *)
176+
public var mutableSpan: MutableSpan<Element> {
177+
@lifetime(borrow self)
178+
@_alwaysEmitIntoClient
179+
mutating get {
180+
let pointer = unsafe UnsafeMutablePointer<Element>(
181+
Builtin.addressOfBorrow(self)
182+
)
183+
let span = unsafe MutableSpan(_unsafeStart: pointer, count: 1)
184+
return unsafe _overrideLifetime(span, mutating: &self)
185+
}
186+
}
174187
}
175188

176189
@_unavailableInEmbedded

test/stdlib/Span/InlineSpanProperties.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,19 @@ suite.test("CollectionOfOne.span stride test")
7272
expectEqual(bytes.byteCount, MemoryLayout.size(ofValue: c))
7373
}
7474

75+
suite.test("CollectionOfOne.mutableSpan property (simple)")
76+
.require(.stdlib_6_2).code {
77+
guard #available(SwiftStdlib 6.2, *) else { return }
78+
79+
var c = CollectionOfOne(Int.random(in: 0..<100000))
80+
expectEqual(c.count, 1)
81+
var span = c.mutableSpan
82+
expectEqual(span.count, 1)
83+
span[0] = Int.random(in: .min..<0)
84+
let r = span[0]
85+
expectEqual(c[0], r)
86+
}
87+
7588
suite.test("InlineArray.span property")
7689
.skip(.custom(
7790
{ if #available(SwiftStdlib 6.2, *) { false } else { true } },

0 commit comments

Comments
 (0)