Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions stdlib/public/core/InlineArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,10 @@ extension InlineArray where Element: ~Copyable {
@lifetime(borrow self)
@_transparent
borrowing get {
guard count > 0 else {
let span = Span<Element>()
return unsafe _overrideLifetime(span, borrowing: self)
}
let span = unsafe Span(_unsafeStart: _protectedAddress, count: count)
return unsafe _overrideLifetime(span, borrowing: self)
}
Expand All @@ -598,6 +602,10 @@ extension InlineArray where Element: ~Copyable {
@lifetime(&self)
@_transparent
mutating get {
guard count > 0 else {
let span = MutableSpan<Element>()
return unsafe _overrideLifetime(span, mutating: &self)
}
let span = unsafe MutableSpan(
_unsafeStart: _protectedMutableAddress,
count: count
Expand Down
21 changes: 21 additions & 0 deletions test/stdlib/Span/InlineSpanProperties.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ suite.test("InlineArray.span property (String)")
}
}

suite.test("InlineArray.span property (empty, aligned)")
.require(.stdlib_6_2).code {
guard #available(SwiftStdlib 6.2, *) else { return }

let empty: InlineArray<0, Padded> = []
let span = empty.span
expectTrue(span.isEmpty)
expectEqual(span.count, 0)
}

suite.test("InlineArray.mutableSpan property")
.require(.stdlib_6_2).code
{
Expand Down Expand Up @@ -146,3 +156,14 @@ suite.test("InlineArray.mutableSpan property (String)")
let s = span[3]
expectTrue(s._isIdentical(to: v[3]))
}

suite.test("InlineArray.mutableSpan property (empty, aligned)")
.require(.stdlib_6_2).code
{
guard #available(SwiftStdlib 6.2, *) else { return }

var empty: InlineArray<0, Padded> = []
var span = empty.mutableSpan
expectTrue(span.isEmpty)
expectEqual(span.count, 0)
}