Skip to content

Commit 83de1b5

Browse files
committed
Preserve random-access Mirror.children inside AnyCollection
1 parent c64ca9a commit 83de1b5

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

stdlib/public/core/Mirror.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,12 @@ public struct Mirror {
137137

138138
/// A collection of `Child` elements describing the structure of the
139139
/// reflected subject.
140-
public var children: Children { AnyCollection(_children) }
140+
public var children: Children {
141+
switch _children {
142+
case let .left(l): return AnyCollection(l)
143+
case let .right(r): return AnyCollection(r)
144+
}
145+
}
141146

142147
/// A suggested display style for the reflected subject.
143148
public let displayStyle: DisplayStyle?

test/stdlib/Mirror.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,25 @@ mirrors.test("LabeledStructure") {
150150
expectEqual("[bark: 1, bite: Zee]", h.testDescription)
151151
}
152152

153+
mirrors.test("ErasedChildren") {
154+
let m1 = Mirror(reflecting: 1..<4)
155+
let c1 = AnyRandomAccessCollection(m1.children)
156+
expectNotNil(c1)
157+
expectNil(c1?.first?.label)
158+
expectTrue(c1?.first?.label as? Int == 1)
159+
expectNil(c1?.last?.label)
160+
expectTrue(c1?.last?.value as? Int == 3)
161+
162+
struct Foo { let i = 42; let s = "bar" }
163+
let m2 = Mirror(reflecting: Foo())
164+
let c2 = AnyRandomAccessCollection(m2.children)
165+
expectNotNil(c2)
166+
expectTrue(c2?.first?.label == "i")
167+
expectTrue(c2?.first?.label as? Int == 42)
168+
expectTrue(c2?.last?.label == "s")
169+
expectTrue(c2?.last?.value as? String == "bar")
170+
}
171+
153172
mirrors.test("Legacy") {
154173
let m = Mirror(reflecting: [1, 2, 3])
155174
expectTrue(m.subjectType == [Int].self)

0 commit comments

Comments
 (0)