@@ -147,9 +147,11 @@ internal func _arrayConditionalDownCastElements<SourceElement, TargetElement>(
147
147
/// - Precondition: SourceElement is a class type.
148
148
/// - Precondition: TargetElement is bridged non-verbatim to Objective-C.
149
149
/// O(n), because each element must be bridged separately.
150
- internal func _arrayConditionalBridgeElements< SourceElement, TargetElement> (
151
- _ source: Array < SourceElement >
152
- ) -> Array < TargetElement > ? {
150
+ internal func _arrayConditionalBridgeElements<
151
+ SourceElement,
152
+ TargetElement
153
+ > ( _ source: Array < SourceElement > ) -> Array < TargetElement > ? {
154
+
153
155
_sanityCheck ( _isBridgedVerbatimToObjectiveC ( SourceElement . self) )
154
156
_sanityCheck ( !_isBridgedVerbatimToObjectiveC( TargetElement . self) )
155
157
@@ -158,26 +160,18 @@ internal func _arrayConditionalBridgeElements<SourceElement, TargetElement>(
158
160
159
161
var p = buf. firstElementAddress
160
162
161
- ElementwiseBridging:
162
- repeat {
163
- for object : SourceElement in source {
164
- let value = Swift . _conditionallyBridgeFromObjectiveC (
165
- unsafeBitCast ( object, to: AnyObject . self) , TargetElement . self)
166
- if _slowPath ( value == nil ) {
167
- break ElementwiseBridging
168
- }
169
- p. initialize ( with: value!)
170
- p += 1
163
+ // Make sure the resulting array owns anything that is successfully stored
164
+ // there.
165
+ defer { buf. count = p - buf. firstElementAddress }
166
+
167
+ for object : SourceElement in source {
168
+ guard let value = object as? TargetElement else {
169
+ return nil
171
170
}
172
- return Array ( _buffer: _ArrayBuffer ( buf, shiftedToStartIndex: 0 ) )
171
+ p. initialize ( with: value)
172
+ p += 1
173
173
}
174
- while false
175
-
176
- // Don't destroy anything we never created.
177
- buf. count = p - buf. firstElementAddress
178
-
179
- // Report failure
180
- return nil
174
+ return Array ( _buffer: _ArrayBuffer ( buf, shiftedToStartIndex: 0 ) )
181
175
}
182
176
183
177
/// Implements `source as? [TargetElement]`: convert each element of
0 commit comments