@@ -100,13 +100,25 @@ public struct Mirror {
100
100
/// `superclassMirror` property is `nil`.
101
101
case suppressed
102
102
}
103
-
104
103
105
- /// The static type of the subject being reflected .
104
+ /// Creates a mirror that reflects on the given instance .
106
105
///
107
- /// This type may differ from the subject's dynamic type when this mirror
108
- /// is the `superclassMirror` of another mirror.
109
- public let subjectType : Any . Type
106
+ /// If the dynamic type of `subject` conforms to `CustomReflectable`, the
107
+ /// resulting mirror is determined by its `customMirror` property.
108
+ /// Otherwise, the result is generated by the language.
109
+ ///
110
+ /// If the dynamic type of `subject` has value semantics, subsequent
111
+ /// mutations of `subject` will not observable in `Mirror`. In general,
112
+ /// though, the observability of mutations is unspecified.
113
+ ///
114
+ /// - Parameter subject: The instance for which to create a mirror.
115
+ public init ( reflecting subject: Any ) {
116
+ if case let customized as CustomReflectable = subject {
117
+ self = customized. customMirror
118
+ } else {
119
+ self = Mirror ( internalReflecting: subject)
120
+ }
121
+ }
110
122
111
123
/// An element of the reflected instance's structure.
112
124
///
@@ -130,26 +142,6 @@ public struct Mirror {
130
142
/// }
131
143
public typealias Children = AnyCollection < Child >
132
144
133
- internal typealias _Children
134
- = _EitherCollection < ReflectedChildren , AnyCollection < Child > >
135
-
136
- internal var _children : _Children
137
-
138
- /// A collection of `Child` elements describing the structure of the
139
- /// reflected subject.
140
- public var children : Children { AnyCollection ( _children) }
141
-
142
- /// A suggested display style for the reflected subject.
143
- public let displayStyle : DisplayStyle ?
144
-
145
- /// A mirror of the subject's superclass, if one exists.
146
- public var superclassMirror : Mirror ? {
147
- return _makeSuperclassMirror ( )
148
- }
149
-
150
- internal let _makeSuperclassMirror : ( ) -> Mirror ?
151
- internal let _defaultDescendantRepresentation : _DefaultDescendantRepresentation
152
-
153
145
/// A suggestion of how a mirror's subject is to be interpreted.
154
146
///
155
147
/// Playgrounds and the debugger will show a representation similar
@@ -160,25 +152,6 @@ public struct Mirror {
160
152
case dictionary, `set`
161
153
}
162
154
163
- /// Creates a mirror that reflects on the given instance.
164
- ///
165
- /// If the dynamic type of `subject` conforms to `CustomReflectable`, the
166
- /// resulting mirror is determined by its `customMirror` property.
167
- /// Otherwise, the result is generated by the language.
168
- ///
169
- /// If the dynamic type of `subject` has value semantics, subsequent
170
- /// mutations of `subject` will not observable in `Mirror`. In general,
171
- /// though, the observability of mutations is unspecified.
172
- ///
173
- /// - Parameter subject: The instance for which to create a mirror.
174
- public init ( reflecting subject: Any ) {
175
- if case let customized as CustomReflectable = subject {
176
- self = customized. customMirror
177
- } else {
178
- self = Mirror ( internalReflecting: subject)
179
- }
180
- }
181
-
182
155
internal static func _noSuperclassMirror( ) -> Mirror ? { return nil }
183
156
184
157
@_semantics ( " optimize.sil.specialize.generic.never " )
@@ -250,7 +223,7 @@ public struct Mirror {
250
223
self . _makeSuperclassMirror = Mirror . _superclassIterator (
251
224
subject, ancestorRepresentation)
252
225
253
- self . _children = _Children ( children)
226
+ self . children = Children ( children)
254
227
self . displayStyle = displayStyle
255
228
self . _defaultDescendantRepresentation
256
229
= subject is CustomLeafReflectable ? . suppressed : . generated
@@ -295,7 +268,7 @@ public struct Mirror {
295
268
296
269
let lazyChildren =
297
270
unlabeledChildren. lazy. map { Child ( label: nil , value: $0) }
298
- self . _children = _Children ( lazyChildren)
271
+ self . children = Children ( lazyChildren)
299
272
300
273
self . displayStyle = displayStyle
301
274
self . _defaultDescendantRepresentation
@@ -342,12 +315,33 @@ public struct Mirror {
342
315
subject, ancestorRepresentation)
343
316
344
317
let lazyChildren = children. lazy. map { Child ( label: $0. 0 , value: $0. 1 ) }
345
- self . _children = _Children ( lazyChildren)
318
+ self . children = Children ( lazyChildren)
346
319
347
320
self . displayStyle = displayStyle
348
321
self . _defaultDescendantRepresentation
349
322
= subject is CustomLeafReflectable ? . suppressed : . generated
350
323
}
324
+
325
+ /// The static type of the subject being reflected.
326
+ ///
327
+ /// This type may differ from the subject's dynamic type when this mirror
328
+ /// is the `superclassMirror` of another mirror.
329
+ public let subjectType : Any . Type
330
+
331
+ /// A collection of `Child` elements describing the structure of the
332
+ /// reflected subject.
333
+ public let children : Children
334
+
335
+ /// A suggested display style for the reflected subject.
336
+ public let displayStyle : DisplayStyle ?
337
+
338
+ /// A mirror of the subject's superclass, if one exists.
339
+ public var superclassMirror : Mirror ? {
340
+ return _makeSuperclassMirror ( )
341
+ }
342
+
343
+ internal let _makeSuperclassMirror : ( ) -> Mirror ?
344
+ internal let _defaultDescendantRepresentation : _DefaultDescendantRepresentation
351
345
}
352
346
353
347
/// A type that explicitly supplies its own mirror.
@@ -384,6 +378,14 @@ extension Int: MirrorPath {}
384
378
extension String : MirrorPath { }
385
379
386
380
extension Mirror {
381
+ internal struct _Dummy : CustomReflectable {
382
+ internal init ( mirror: Mirror ) {
383
+ self . mirror = mirror
384
+ }
385
+ internal var mirror : Mirror
386
+ internal var customMirror : Mirror { return mirror }
387
+ }
388
+
387
389
/// Returns a specific descendant of the reflected subject, or `nil` if no
388
390
/// such descendant exists.
389
391
///
@@ -431,28 +433,24 @@ extension Mirror {
431
433
public func descendant(
432
434
_ first: MirrorPath , _ rest: MirrorPath ...
433
435
) -> Any ? {
434
-
435
- func fetch( _ path: MirrorPath , in children: _Children ) -> Any ? {
436
- let position : _Children . Index ?
437
- switch path {
438
- case let label as String :
439
- position = children. firstIndex { $0. label == label }
440
- case let offset as Int :
436
+ var result : Any = _Dummy ( mirror: self )
437
+ for e in [ first] + rest {
438
+ let children = Mirror ( reflecting: result) . children
439
+ let position : Children . Index
440
+ if case let label as String = e {
441
+ position = children. firstIndex { $0. label == label } ?? children. endIndex
442
+ }
443
+ else if let offset = e as? Int {
441
444
position = children. index ( children. startIndex,
442
445
offsetBy: offset,
443
- limitedBy: children. endIndex)
444
- default :
446
+ limitedBy: children. endIndex) ?? children. endIndex
447
+ }
448
+ else {
445
449
_preconditionFailure (
446
450
" Someone added a conformance to MirrorPath; that privilege is reserved to the standard library " )
447
451
}
448
- return position. map { children [ $0] . value }
449
- }
450
-
451
- guard var result = fetch ( first, in: _children) else { return nil }
452
- for path in rest {
453
- guard let next = fetch ( path, in: Mirror ( reflecting: result) . _children)
454
- else { return nil }
455
- result = next
452
+ if position == children. endIndex { return nil }
453
+ result = children [ position] . value
456
454
}
457
455
return result
458
456
}
0 commit comments