Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@CassiusPacheco
Last active May 17, 2020 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CassiusPacheco/4f74c4e881cfb467c1aa8db08004c1f0 to your computer and use it in GitHub Desktop.
Save CassiusPacheco/4f74c4e881cfb467c1aa8db08004c1f0 to your computer and use it in GitHub Desktop.
Wraps this publisher with a type eraser that updates the expected `Failure` from `Never` to the given type.
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
extension Publisher where Self.Failure == Never {
/// Wraps this publisher with a type eraser that updates the expected `Failure` from `Never` to the given type.
///
/// Use `eraseToAnyPublisherFailure(to:)` to expose an instance of AnyPublisher to the downstream subscriber, rather than this publisher’s actual type.
/// Under the hood it updates the failure type by calling `setFailureType(to:)` applying the given type.
public func eraseToAnyPublisherFailure<E>(to failureType: E.Type) -> AnyPublisher<Self.Output, E> where E: Error {
return self
.setFailureType(to: failureType)
.eraseToAnyPublisher()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment