@@ -100,13 +100,25 @@ public struct Mirror {
100100 /// `superclassMirror` property is `nil`.
101101 case suppressed
102102 }
103-
104103
105- /// The static type of the subject being reflected .
104+ /// Creates a mirror that reflects on the given instance .
106105 ///
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+ }
110122
111123 /// An element of the reflected instance's structure.
112124 ///
@@ -130,26 +142,6 @@ public struct Mirror {
130142 /// }
131143 public typealias Children = AnyCollection < Child >
132144
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-
153145 /// A suggestion of how a mirror's subject is to be interpreted.
154146 ///
155147 /// Playgrounds and the debugger will show a representation similar
@@ -160,25 +152,6 @@ public struct Mirror {
160152 case dictionary, `set`
161153 }
162154
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-
182155 internal static func _noSuperclassMirror( ) -> Mirror ? { return nil }
183156
184157 @_semantics ( " optimize.sil.specialize.generic.never " )
@@ -250,7 +223,7 @@ public struct Mirror {
250223 self . _makeSuperclassMirror = Mirror . _superclassIterator (
251224 subject, ancestorRepresentation)
252225
253- self . _children = _Children ( children)
226+ self . children = Children ( children)
254227 self . displayStyle = displayStyle
255228 self . _defaultDescendantRepresentation
256229 = subject is CustomLeafReflectable ? . suppressed : . generated
@@ -295,7 +268,7 @@ public struct Mirror {
295268
296269 let lazyChildren =
297270 unlabeledChildren. lazy. map { Child ( label: nil , value: $0) }
298- self . _children = _Children ( lazyChildren)
271+ self . children = Children ( lazyChildren)
299272
300273 self . displayStyle = displayStyle
301274 self . _defaultDescendantRepresentation
@@ -342,12 +315,33 @@ public struct Mirror {
342315 subject, ancestorRepresentation)
343316
344317 let lazyChildren = children. lazy. map { Child ( label: $0. 0 , value: $0. 1 ) }
345- self . _children = _Children ( lazyChildren)
318+ self . children = Children ( lazyChildren)
346319
347320 self . displayStyle = displayStyle
348321 self . _defaultDescendantRepresentation
349322 = subject is CustomLeafReflectable ? . suppressed : . generated
350323 }
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
351345}
352346
353347/// A type that explicitly supplies its own mirror.
@@ -384,6 +378,14 @@ extension Int: MirrorPath {}
384378extension String : MirrorPath { }
385379
386380extension 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+
387389 /// Returns a specific descendant of the reflected subject, or `nil` if no
388390 /// such descendant exists.
389391 ///
@@ -431,28 +433,24 @@ extension Mirror {
431433 public func descendant(
432434 _ first: MirrorPath , _ rest: MirrorPath ...
433435 ) -> 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 {
441444 position = children. index ( children. startIndex,
442445 offsetBy: offset,
443- limitedBy: children. endIndex)
444- default :
446+ limitedBy: children. endIndex) ?? children. endIndex
447+ }
448+ else {
445449 _preconditionFailure (
446450 " Someone added a conformance to MirrorPath; that privilege is reserved to the standard library " )
447451 }
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
456454 }
457455 return result
458456 }
0 commit comments