|
11 | 11 | //===----------------------------------------------------------------------===// |
12 | 12 |
|
13 | 13 | #if compiler(>=6.0) |
| 14 | +@available(AsyncAlgorithms 1.1, *) |
14 | 15 | extension AsyncSequence { |
15 | 16 |
|
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 | | - } |
| 17 | + /// Converts any failure into a new error. |
| 18 | + /// |
| 19 | + /// - Parameter transform: A closure that takes the failure as a parameter and returns a new error. |
| 20 | + /// - Returns: An asynchronous sequence that maps the error thrown into the one produced by the transform closure. |
| 21 | + /// |
| 22 | + /// Use the ``mapError(_:)`` operator when you need to replace one error type with another. |
| 23 | + @available(AsyncAlgorithms 1.1, *) |
| 24 | + public func mapError<MappedError: Error>(_ transform: @Sendable @escaping (Failure) async -> MappedError) -> AsyncMapErrorSequence<Self, MappedError> { |
| 25 | + AsyncMapErrorSequence(base: self, transform: transform) |
| 26 | + } |
37 | 27 | } |
38 | 28 |
|
39 | 29 | /// 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 { |
| 30 | +@available(AsyncAlgorithms 1.1, *) |
| 31 | +public struct AsyncMapErrorSequence<Base: AsyncSequence, MappedError: Error> { |
| 32 | + public typealias Element = Base.Element |
| 33 | + public typealias Failure = MappedError |
| 34 | + |
| 35 | + private let base: Base |
| 36 | + private let transform: @Sendable (Base.Failure) async -> MappedError |
| 37 | + |
| 38 | + init( |
| 39 | + base: Base, |
| 40 | + transform: @Sendable @escaping (Base.Failure) async -> MappedError |
| 41 | + ) { |
| 42 | + self.base = base |
| 43 | + self.transform = transform |
| 44 | + } |
| 45 | +} |
42 | 46 |
|
43 | | - typealias AsyncIterator = Iterator |
44 | | - typealias Element = Base.Element |
45 | | - typealias Failure = Base.Failure |
| 47 | +@available(AsyncAlgorithms 1.1, *) |
| 48 | +extension AsyncMapErrorSequence: AsyncSequence { |
46 | 49 |
|
47 | | - private let base: Base |
48 | | - private let transform: @Sendable (Failure) -> MappedError |
| 50 | + /// The iterator that produces elements of the map sequence. |
| 51 | + public struct Iterator: AsyncIteratorProtocol { |
| 52 | + public typealias Element = Base.Element |
| 53 | + |
| 54 | + private var base: Base.AsyncIterator |
| 55 | + |
| 56 | + private let transform: @Sendable (Base.Failure) async -> MappedError |
49 | 57 |
|
50 | 58 | init( |
51 | | - base: Base, |
52 | | - transform: @Sendable @escaping (Failure) -> MappedError |
| 59 | + base: Base.AsyncIterator, |
| 60 | + transform: @Sendable @escaping (Base.Failure) async -> MappedError |
53 | 61 | ) { |
54 | | - self.base = base |
55 | | - self.transform = transform |
| 62 | + self.base = base |
| 63 | + self.transform = transform |
56 | 64 | } |
57 | 65 |
|
58 | | - func makeAsyncIterator() -> Iterator { |
59 | | - Iterator( |
60 | | - base: base.makeAsyncIterator(), |
61 | | - transform: transform |
62 | | - ) |
| 66 | + public mutating func next() async throws(MappedError) -> Element? { |
| 67 | + try await self.next(isolation: nil) |
63 | 68 | } |
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 | 69 |
|
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 | | - } |
| 70 | + public mutating func next(isolation actor: isolated (any Actor)?) async throws(MappedError) -> Element? { |
| 71 | + do { |
| 72 | + return try await base.next(isolation: actor) |
| 73 | + } catch { |
| 74 | + throw await transform(error) |
| 75 | + } |
97 | 76 | } |
| 77 | + } |
| 78 | + |
| 79 | + public func makeAsyncIterator() -> Iterator { |
| 80 | + Iterator( |
| 81 | + base: base.makeAsyncIterator(), |
| 82 | + transform: transform |
| 83 | + ) |
| 84 | + } |
98 | 85 | } |
99 | 86 |
|
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 {} |
| 87 | +@available(AsyncAlgorithms 1.1, *) |
| 88 | +extension AsyncMapErrorSequence: Sendable where Base: Sendable { } |
102 | 89 |
|
103 | 90 | @available(*, unavailable) |
104 | | -extension AsyncMapErrorSequence.Iterator: Sendable {} |
| 91 | +extension AsyncMapErrorSequence.Iterator: Sendable { } |
105 | 92 | #endif |
0 commit comments