Skip to content

Commit 7188d0b

Browse files
committed
[stdlib] add span property to KeyValuePairs
1 parent d6ab6d0 commit 7188d0b

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

stdlib/public/core/KeyValuePairs.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,19 @@ extension KeyValuePairs: RandomAccessCollection {
125125
}
126126
}
127127

128+
extension KeyValuePairs {
129+
130+
@available(SwiftStdlib 6.2, *)
131+
public var span: Span<(Key, Value)> {
132+
@lifetime(borrow self)
133+
@_alwaysEmitIntoClient
134+
get {
135+
let span = _elements.span
136+
return _overrideLifetime(span, borrowing: self)
137+
}
138+
}
139+
}
140+
128141
@_unavailableInEmbedded
129142
extension KeyValuePairs: CustomStringConvertible {
130143
/// A string that represents the contents of the dictionary.

test/stdlib/Span/ArrayStorage.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,21 @@ suite.test("ArraySlice.span property")
7272
let i2 = span1.withUnsafeBufferPointer { Int(bitPattern: $0.baseAddress) }
7373
expectEqual(i1, i2)
7474
}
75+
76+
suite.test("KeyValuePairs.span property")
77+
.skip(.custom(
78+
{ if #available(SwiftStdlib 6.2, *) { false } else { true } },
79+
reason: "Requires Swift 6.2's standard library"
80+
))
81+
.code {
82+
guard #available(SwiftStdlib 6.2, *) else { return }
83+
84+
let pairs = [(1, "a"), (2, "b"), (3, "c"), (4, "d")]
85+
let kvp: KeyValuePairs = [1: "a", 2: "b", 3: "c", 4: "d"]
86+
87+
let span = kvp.span
88+
expectEqual(span.count, kvp.count)
89+
for i in span.indices {
90+
expectEqual(span[i], pairs[i])
91+
}
92+
}

0 commit comments

Comments
 (0)