Skip to content

Commit 840c0d9

Browse files
authored
Merge pull request #3545 from apple/array-bridge-cleanup
2 parents f50dcbf + 1680cc4 commit 840c0d9

File tree

4 files changed

+227
-322
lines changed

4 files changed

+227
-322
lines changed

stdlib/public/core/ArrayCast.swift

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,11 @@ internal func _arrayConditionalDownCastElements<SourceElement, TargetElement>(
147147
/// - Precondition: SourceElement is a class type.
148148
/// - Precondition: TargetElement is bridged non-verbatim to Objective-C.
149149
/// 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+
153155
_sanityCheck(_isBridgedVerbatimToObjectiveC(SourceElement.self))
154156
_sanityCheck(!_isBridgedVerbatimToObjectiveC(TargetElement.self))
155157

@@ -158,26 +160,18 @@ internal func _arrayConditionalBridgeElements<SourceElement, TargetElement>(
158160

159161
var p = buf.firstElementAddress
160162

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
171170
}
172-
return Array(_buffer: _ArrayBuffer(buf, shiftedToStartIndex: 0))
171+
p.initialize(with: value)
172+
p += 1
173173
}
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))
181175
}
182176

183177
/// Implements `source as? [TargetElement]`: convert each element of

0 commit comments

Comments
 (0)