Skip to content

Commit 9655fe4

Browse files
committed
Move collection diffing type-erasure to an init
1 parent e8a288a commit 9655fe4

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

Sources/Testing/Expectations/ExpectationContext.swift

+2-22
Original file line numberDiff line numberDiff line change
@@ -222,26 +222,6 @@ extension __ExpectationContext {
222222
// MARK: - Collection comparison and diffing
223223

224224
extension __ExpectationContext {
225-
/// Convert an instance of `CollectionDifference` to one that is type-erased
226-
/// over elements of type `Any`.
227-
///
228-
/// - Parameters:
229-
/// - difference: The difference to convert.
230-
///
231-
/// - Returns: A type-erased copy of `difference`.
232-
private static func _typeEraseCollectionDifference(_ difference: CollectionDifference<some Any>) -> CollectionDifference<Any> {
233-
CollectionDifference<Any>(
234-
difference.lazy.map { change in
235-
switch change {
236-
case let .insert(offset, element, associatedWith):
237-
return .insert(offset: offset, element: element as Any, associatedWith: associatedWith)
238-
case let .remove(offset, element, associatedWith):
239-
return .remove(offset: offset, element: element as Any, associatedWith: associatedWith)
240-
}
241-
}
242-
)!
243-
}
244-
245225
/// Generate a description of a previously-computed collection difference.
246226
///
247227
/// - Parameters:
@@ -317,7 +297,7 @@ extension __ExpectationContext {
317297

318298
if !result {
319299
differences[opID] = { [lhs, rhs] in
320-
Self._typeEraseCollectionDifference(lhs.difference(from: rhs))
300+
CollectionDifference<Any>(lhs.difference(from: rhs))
321301
}
322302
}
323303

@@ -380,7 +360,7 @@ extension __ExpectationContext {
380360
return nil
381361
}
382362

383-
return Self._typeEraseCollectionDifference(diff)
363+
return CollectionDifference<Any>(diff)
384364
}
385365
}
386366

Sources/Testing/Support/Additions/CollectionDifferenceAdditions.swift

+22
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,28 @@
88
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
99
//
1010

11+
extension CollectionDifference<Any> {
12+
/// Convert an instance of `CollectionDifference` to one that is type-erased
13+
/// over elements of type `Any`.
14+
///
15+
/// - Parameters:
16+
/// - difference: The difference to convert.
17+
init(_ difference: CollectionDifference<some Any>) {
18+
self.init(
19+
difference.lazy.map { change in
20+
switch change {
21+
case let .insert(offset, element, associatedWith):
22+
return .insert(offset: offset, element: element as Any, associatedWith: associatedWith)
23+
case let .remove(offset, element, associatedWith):
24+
return .remove(offset: offset, element: element as Any, associatedWith: associatedWith)
25+
}
26+
}
27+
)!
28+
}
29+
}
30+
31+
// MARK: -
32+
1133
extension CollectionDifference.Change {
1234
/// The element that was changed.
1335
var element: ChangeElement {

0 commit comments

Comments
 (0)