|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2024 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#if compiler(>=6.0) |
| 14 | +extension AsyncSequence { |
| 15 | + |
| 16 | + /// Converts any failure into a new error. |
| 17 | + /// |
| 18 | + /// - Parameter transform: A closure that takes the failure as a parameter and returns a new error. |
| 19 | + /// - Returns: An asynchronous sequence that maps the error thrown into the one produced by the transform closure. |
| 20 | + /// |
| 21 | + /// Use the ``mapError(_:)`` operator when you need to replace one error type with another. |
| 22 | + @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) |
| 23 | + public func mapError<MappedError: Error>(_ transform: @Sendable @escaping (Self.Failure) -> MappedError) -> some AsyncSequence<Self.Element, MappedError> { |
| 24 | + AsyncMapErrorSequence(base: self, transform: transform) |
| 25 | + } |
| 26 | + |
| 27 | + /// Converts any failure into a new error. |
| 28 | + /// |
| 29 | + /// - Parameter transform: A closure that takes the failure as a parameter and returns a new error. |
| 30 | + /// - Returns: An asynchronous sequence that maps the error thrown into the one produced by the transform closure. |
| 31 | + /// |
| 32 | + /// Use the ``mapError(_:)`` operator when you need to replace one error type with another. |
| 33 | + @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) |
| 34 | + public func mapError<MappedError: Error>(_ transform: @Sendable @escaping (Self.Failure) -> MappedError) -> (some AsyncSequence<Self.Element, MappedError> & Sendable) where Self: Sendable, Self.Element: Sendable { |
| 35 | + AsyncMapErrorSequence(base: self, transform: transform) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +/// An asynchronous sequence that converts any failure into a new error. |
| 40 | +@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) |
| 41 | +fileprivate struct AsyncMapErrorSequence<Base: AsyncSequence, MappedError: Error>: AsyncSequence { |
| 42 | + |
| 43 | + typealias AsyncIterator = Iterator |
| 44 | + typealias Element = Base.Element |
| 45 | + typealias Failure = Base.Failure |
| 46 | + |
| 47 | + private let base: Base |
| 48 | + private let transform: @Sendable (Failure) -> MappedError |
| 49 | + |
| 50 | + init( |
| 51 | + base: Base, |
| 52 | + transform: @Sendable @escaping (Failure) -> MappedError |
| 53 | + ) { |
| 54 | + self.base = base |
| 55 | + self.transform = transform |
| 56 | + } |
| 57 | + |
| 58 | + func makeAsyncIterator() -> Iterator { |
| 59 | + Iterator( |
| 60 | + base: base.makeAsyncIterator(), |
| 61 | + transform: transform |
| 62 | + ) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) |
| 67 | +extension AsyncMapErrorSequence { |
| 68 | + |
| 69 | + /// The iterator that produces elements of the map sequence. |
| 70 | + fileprivate struct Iterator: AsyncIteratorProtocol { |
| 71 | + |
| 72 | + typealias Element = Base.Element |
| 73 | + |
| 74 | + private var base: Base.AsyncIterator |
| 75 | + |
| 76 | + private let transform: @Sendable (Failure) -> MappedError |
| 77 | + |
| 78 | + init( |
| 79 | + base: Base.AsyncIterator, |
| 80 | + transform: @Sendable @escaping (Failure) -> MappedError |
| 81 | + ) { |
| 82 | + self.base = base |
| 83 | + self.transform = transform |
| 84 | + } |
| 85 | + |
| 86 | + mutating func next() async throws(MappedError) -> Element? { |
| 87 | + try await self.next(isolation: nil) |
| 88 | + } |
| 89 | + |
| 90 | + mutating func next(isolation actor: isolated (any Actor)?) async throws(MappedError) -> Element? { |
| 91 | + do { |
| 92 | + return try await base.next(isolation: actor) |
| 93 | + } catch { |
| 94 | + throw transform(error) |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) |
| 101 | +extension AsyncMapErrorSequence: Sendable where Base: Sendable, Base.Element: Sendable {} |
| 102 | + |
| 103 | +@available(*, unavailable) |
| 104 | +extension AsyncMapErrorSequence.Iterator: Sendable {} |
| 105 | +#endif |
0 commit comments