Skip to content

Commit 2a78e81

Browse files
committed
[stdlib] add a storage property to Slab
1 parent 3639926 commit 2a78e81

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

stdlib/public/core/Span/Properties/InlineTypeExtensions.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,19 @@ extension SIMD64 {
121121
}
122122
}
123123
}
124+
125+
@available(SwiftStdlib 6.2, *)
126+
extension Slab where Element: ~Copyable {
127+
128+
@available(SwiftStdlib 6.2, *)
129+
public var storage: Span<Element> {
130+
@_alwaysEmitIntoClient
131+
@_addressableSelf
132+
@lifetime(borrow self)
133+
borrowing get {
134+
let pointer = _address
135+
let span = Span(_unsafeStart: pointer, count: count)
136+
return _overrideLifetime(span, borrowing: self)
137+
}
138+
}
139+
}

test/stdlib/Span/SlabSpan.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//===--- SlabSpan.swift ---------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2025 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
// RUN: %target-run-stdlib-swift(-enable-experimental-feature LifetimeDependence -enable-experimental-feature Span -enable-experimental-feature AddressableTypes -enable-experimental-feature ValueGenerics)
14+
15+
// REQUIRES: executable_test
16+
// REQUIRES: swift_feature_ValueGenerics
17+
// REQUIRES: swift_feature_AddressableTypes
18+
// REQUIRES: swift_feature_Span
19+
// REQUIRES: swift_feature_LifetimeDependence
20+
21+
import StdlibUnittest
22+
23+
var suite = TestSuite("SlabStorageProperty")
24+
defer { runAllTests() }
25+
26+
suite.test("Slab.storage property")
27+
.skip(.custom(
28+
{ if #available(SwiftStdlib 6.1, *) { false } else { true } },
29+
reason: "Requires Swift 6.1's standard library"
30+
))
31+
.code {
32+
guard #available(SwiftStdlib 6.1, *) else { return }
33+
34+
var s = Slab<5, Int>(repeating: 0)
35+
s[3] = .random(in: 0..<1000)
36+
let storage = s.storage
37+
expectEqual(storage.count, s.count)
38+
for i in 0..<s.count {
39+
expectEqual(storage[i], s[i])
40+
}
41+
}

0 commit comments

Comments
 (0)