Skip to content

Commit d0d5285

Browse files
committed
[stdlib] add span property to InlineArray
1 parent 264747a commit d0d5285

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

stdlib/public/core/InlineArray.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
@available(SwiftStdlib 6.2, *)
4444
@frozen
4545
@safe
46+
@_addressableForDependencies
4647
public struct InlineArray<let count: Int, Element: ~Copyable>: ~Copyable {
4748
@usableFromInline
4849
internal let _storage: Builtin.FixedArray<count, Element>
@@ -441,6 +442,26 @@ extension InlineArray where Element: ~Copyable {
441442
}
442443
}
443444

445+
//===----------------------------------------------------------------------===//
446+
// MARK: Span
447+
//===----------------------------------------------------------------------===//
448+
449+
@available(SwiftStdlib 6.2, *)
450+
extension InlineArray where Element: ~Copyable {
451+
452+
@available(SwiftStdlib 6.2, *)
453+
public var span: Span<Element> {
454+
@lifetime(borrow self)
455+
@_addressableSelf
456+
@_alwaysEmitIntoClient
457+
borrowing get {
458+
let pointer = _address
459+
let span = unsafe Span(_unsafeStart: pointer, count: count)
460+
return unsafe _overrideLifetime(span, borrowing: self)
461+
}
462+
}
463+
}
464+
444465
//===----------------------------------------------------------------------===//
445466
// MARK: - Unsafe APIs
446467
//===----------------------------------------------------------------------===//

test/stdlib/Span/InlineStorage.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
// RUN: %target-run-stdlib-swift
13+
// RUN: %target-run-stdlib-swift(-enable-experimental-feature ValueGenerics)
1414

1515
// REQUIRES: executable_test
16+
// REQUIRES: swift_feature_ValueGenerics
1617

1718
import StdlibUnittest
1819

@@ -68,3 +69,20 @@ suite.test("CollectionOfOne.span stride test")
6869
let bytes = span.bytes
6970
expectEqual(bytes.byteCount, MemoryLayout.size(ofValue: c))
7071
}
72+
73+
suite.test("InlineArray.span property")
74+
.skip(.custom(
75+
{ if #available(SwiftStdlib 6.2, *) { false } else { true } },
76+
reason: "Requires Swift 6.2's standard library"
77+
))
78+
.code {
79+
guard #available(SwiftStdlib 6.2, *) else { return }
80+
81+
var s = InlineArray<5, Int>(repeating: 0)
82+
s[3] = .random(in: 0..<1000)
83+
let span = s.span
84+
expectEqual(span.count, s.count)
85+
for i in s.indices {
86+
expectEqual(span[i], s[i])
87+
}
88+
}

0 commit comments

Comments
 (0)