Skip to content

Commit

Permalink
Move collection diffing type-erasure to an init
Browse files Browse the repository at this point in the history
  • Loading branch information
grynspan committed Dec 17, 2024
1 parent e8a288a commit 9655fe4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
24 changes: 2 additions & 22 deletions Sources/Testing/Expectations/ExpectationContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -222,26 +222,6 @@ extension __ExpectationContext {
// MARK: - Collection comparison and diffing

extension __ExpectationContext {
/// Convert an instance of `CollectionDifference` to one that is type-erased
/// over elements of type `Any`.
///
/// - Parameters:
/// - difference: The difference to convert.
///
/// - Returns: A type-erased copy of `difference`.
private static func _typeEraseCollectionDifference(_ difference: CollectionDifference<some Any>) -> CollectionDifference<Any> {
CollectionDifference<Any>(
difference.lazy.map { change in
switch change {
case let .insert(offset, element, associatedWith):
return .insert(offset: offset, element: element as Any, associatedWith: associatedWith)
case let .remove(offset, element, associatedWith):
return .remove(offset: offset, element: element as Any, associatedWith: associatedWith)
}
}
)!
}

/// Generate a description of a previously-computed collection difference.
///
/// - Parameters:
Expand Down Expand Up @@ -317,7 +297,7 @@ extension __ExpectationContext {

if !result {
differences[opID] = { [lhs, rhs] in
Self._typeEraseCollectionDifference(lhs.difference(from: rhs))
CollectionDifference<Any>(lhs.difference(from: rhs))
}
}

Expand Down Expand Up @@ -380,7 +360,7 @@ extension __ExpectationContext {
return nil
}

return Self._typeEraseCollectionDifference(diff)
return CollectionDifference<Any>(diff)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,28 @@
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
//

extension CollectionDifference<Any> {
/// Convert an instance of `CollectionDifference` to one that is type-erased
/// over elements of type `Any`.
///
/// - Parameters:
/// - difference: The difference to convert.
init(_ difference: CollectionDifference<some Any>) {
self.init(
difference.lazy.map { change in
switch change {
case let .insert(offset, element, associatedWith):
return .insert(offset: offset, element: element as Any, associatedWith: associatedWith)
case let .remove(offset, element, associatedWith):
return .remove(offset: offset, element: element as Any, associatedWith: associatedWith)
}
}
)!
}
}

// MARK: -

extension CollectionDifference.Change {
/// The element that was changed.
var element: ChangeElement {
Expand Down

0 comments on commit 9655fe4

Please sign in to comment.