@@ -28,7 +28,7 @@ internal func _arrayDownCastIndirect<SourceValue, TargetValue>(
28
28
///
29
29
/// - Note: When SourceElement and TargetElement are both bridged verbatim, type
30
30
/// checking is deferred until elements are actually accessed.
31
- @inlinable // FIXME(sil-serialize-all)
31
+ @inlinable //for performance reasons
32
32
public func _arrayForceCast< SourceElement, TargetElement> (
33
33
_ source: Array < SourceElement >
34
34
) -> Array < TargetElement > {
@@ -52,21 +52,6 @@ public func _arrayForceCast<SourceElement, TargetElement>(
52
52
return source. map { $0 as! TargetElement }
53
53
}
54
54
55
- @_fixed_layout
56
- @usableFromInline
57
- internal struct _UnwrappingFailed : Error {
58
- @inlinable
59
- internal init ( ) { }
60
- }
61
-
62
- extension Optional {
63
- @inlinable // FIXME(sil-serialize-all)
64
- internal func unwrappedOrError( ) throws -> Wrapped {
65
- if let x = self { return x }
66
- throw _UnwrappingFailed ( )
67
- }
68
- }
69
-
70
55
/// Called by the casting machinery.
71
56
@_silgen_name ( " _swift_arrayDownCastConditionalIndirect " )
72
57
internal func _arrayDownCastConditionalIndirect< SourceValue, TargetValue> (
@@ -85,9 +70,18 @@ internal func _arrayDownCastConditionalIndirect<SourceValue, TargetValue>(
85
70
/// return `nil` if any element fails to convert.
86
71
///
87
72
/// - Complexity: O(n), because each element must be checked.
88
- @inlinable // FIXME(sil-serialize-all)
73
+ @inlinable //for performance reasons
89
74
public func _arrayConditionalCast< SourceElement, TargetElement> (
90
75
_ source: [ SourceElement ]
91
76
) -> [ TargetElement ] ? {
92
- return try ? source. map { try ( $0 as? TargetElement ) . unwrappedOrError ( ) }
77
+ var successfulCasts = ContiguousArray < TargetElement > ( )
78
+ successfulCasts. reserveCapacity ( source. count)
79
+ for element in source {
80
+ if let casted = element as? TargetElement {
81
+ successfulCasts. append ( casted)
82
+ } else {
83
+ return nil
84
+ }
85
+ }
86
+ return Array ( successfulCasts)
93
87
}
0 commit comments