diff --git a/Sources/Rx+Combine/Infallible+Combine.swift b/Sources/Rx+Combine/Infallible+Combine.swift new file mode 100644 index 0000000..7742627 --- /dev/null +++ b/Sources/Rx+Combine/Infallible+Combine.swift @@ -0,0 +1,30 @@ +// +// Infallible+Combine.swift +// RxCombine + +#if canImport(Combine) +import Combine +import RxSwift + +@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) +public extension Infallible { + /// An `AnyPublisher` of the underlying Observable's Element type + /// so the Infallible pushes events to the Publisher. + var publisher: AnyPublisher { + asObservable() + .asPublisher() + .assertNoFailure("Infallible should not fail") + .eraseToAnyPublisher() + } + + /// Returns a `AnyPublisher` of the underlying Observable's Element type + /// so the Infallible pushes events to the Publisher. + /// + /// - returns: AnyPublisher of the underlying Observable's Element type. + /// - note: This is an alias for the `publisher` property. + func asPublisher() -> AnyPublisher { + publisher + } +} + +#endif diff --git a/Tests/ObservableAsPublisherTests.swift b/Tests/ObservableAsPublisherTests.swift index 838ec7b..be694b0 100644 --- a/Tests/ObservableAsPublisherTests.swift +++ b/Tests/ObservableAsPublisherTests.swift @@ -86,6 +86,21 @@ class ObservableAsPublisherTests: XCTestCase { XCTAssertEqual(values, Array(1...10)) XCTAssertTrue(completed) } + + func testStringInfallible() { + let input = "Hello world I'm a RxSwift Infallible".components(separatedBy: " ") + let source = Infallible.from(input) + var values = [String]() + var completed = false + + subscription = source + .asPublisher() + .handleEvents(receiveCompletion: { _ in completed = true }) + .sink(receiveValue: { values.append($0) }) // this syntax is only allowed when Failure == Never + + XCTAssertEqual(values, input) + XCTAssertTrue(completed) + } } enum FakeError: Swift.Error {