Skip to content

Commit feae6d0

Browse files
committed
[stdlib] add span property to InlineArray
1 parent 4411e39 commit feae6d0

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

stdlib/public/core/InlineArray.swift

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2024 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2024 - 2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -13,6 +13,7 @@
1313
/// A fixed-size array.
1414
@available(SwiftStdlib 6.2, *)
1515
@frozen
16+
@_addressableForDependencies
1617
public struct InlineArray<let count: Int, Element: ~Copyable>: ~Copyable {
1718
@usableFromInline
1819
internal let _storage: Builtin.FixedArray<count, Element>
@@ -395,6 +396,26 @@ extension InlineArray where Element: ~Copyable {
395396
}
396397
}
397398

399+
//===----------------------------------------------------------------------===//
400+
// Span
401+
//===----------------------------------------------------------------------===//
402+
403+
@available(SwiftStdlib 6.2, *)
404+
extension InlineArray where Element: ~Copyable {
405+
406+
@available(SwiftStdlib 6.2, *)
407+
public var span: Span<Element> {
408+
@lifetime(borrow self)
409+
@_addressableSelf
410+
@_alwaysEmitIntoClient
411+
borrowing get {
412+
let pointer = _address
413+
let span = Span(_unsafeStart: pointer, count: count)
414+
return _overrideLifetime(span, borrowing: self)
415+
}
416+
}
417+
}
418+
398419
//===----------------------------------------------------------------------===//
399420
// Unsafe APIs
400421
//===----------------------------------------------------------------------===//

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)