|
| 1 | +import Foundation |
| 2 | + |
| 3 | +@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) |
| 4 | +extension AsyncSequence { |
| 5 | + |
| 6 | + /// Creates an asynchronous sequence that iterates over the events of the original sequence. |
| 7 | + /// |
| 8 | + /// ``` |
| 9 | + /// for await event in (1...3).async.materialize() { |
| 10 | + /// print("\(event)") |
| 11 | + /// } |
| 12 | + /// |
| 13 | + /// // .value(1) |
| 14 | + /// // .value(2) |
| 15 | + /// // .value(3) |
| 16 | + /// // .completed(.finished) |
| 17 | + /// ``` |
| 18 | + /// |
| 19 | + /// - Returns: An `AsyncSequence` where the element is an event of the original `AsyncSequence`. |
| 20 | + @inlinable |
| 21 | + public func materialize() -> AsyncMaterializedSequence<Self> { |
| 22 | + return AsyncMaterializedSequence(self) |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +/// An `AsyncSequence` that iterates over the events of the original `AsyncSequence`. |
| 27 | +@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) |
| 28 | +public struct AsyncMaterializedSequence<Base: AsyncSequence> { |
| 29 | + |
| 30 | + @usableFromInline |
| 31 | + let base: Base |
| 32 | + |
| 33 | + @inlinable |
| 34 | + init(_ base: Base) { |
| 35 | + self.base = base |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) |
| 40 | +extension AsyncMaterializedSequence: AsyncSequence { |
| 41 | + |
| 42 | + public enum Completion { |
| 43 | + case failure(Base.Failure) |
| 44 | + case finished |
| 45 | + } |
| 46 | + |
| 47 | + public enum Event { |
| 48 | + case value(Base.Element) |
| 49 | + case completed(Completion) |
| 50 | + } |
| 51 | + |
| 52 | + public struct Iterator: AsyncIteratorProtocol { |
| 53 | + |
| 54 | + @usableFromInline |
| 55 | + var base: Base.AsyncIterator |
| 56 | + |
| 57 | + @usableFromInline |
| 58 | + private(set) var baseIsCompleted: Bool = false |
| 59 | + |
| 60 | + @inlinable |
| 61 | + init(_ base: Base.AsyncIterator) { |
| 62 | + self.base = base |
| 63 | + } |
| 64 | + |
| 65 | + @inlinable |
| 66 | + public mutating func next() async -> Event? { |
| 67 | + guard !baseIsCompleted else { |
| 68 | + return nil |
| 69 | + } |
| 70 | + |
| 71 | + do { |
| 72 | + if let element = try await base.next() { |
| 73 | + return .value(element) |
| 74 | + } else { |
| 75 | + baseIsCompleted = true |
| 76 | + return .completed(.finished) |
| 77 | + } |
| 78 | + } catch let error as Base.Failure { |
| 79 | + baseIsCompleted = true |
| 80 | + return .completed(.failure(error)) |
| 81 | + } catch { |
| 82 | + preconditionFailure("Unexpected error: \(error)") |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @inlinable |
| 88 | + public func makeAsyncIterator() -> Iterator { |
| 89 | + Iterator(base.makeAsyncIterator()) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) |
| 94 | +extension AsyncMaterializedSequence: Sendable where Base: Sendable, Base.Element: Sendable {} |
| 95 | + |
| 96 | +@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) |
| 97 | +extension AsyncMaterializedSequence.Completion: Sendable {} |
| 98 | + |
| 99 | +@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) |
| 100 | +extension AsyncMaterializedSequence.Event: Sendable where Base.Element: Sendable {} |
| 101 | + |
| 102 | +@available(*, unavailable) |
| 103 | +extension AsyncMaterializedSequence.Iterator: Sendable {} |
| 104 | + |
| 105 | +@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) |
| 106 | +extension AsyncMaterializedSequence.Completion: Equatable where Base.Failure: Equatable {} |
| 107 | + |
| 108 | +@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) |
| 109 | +extension AsyncMaterializedSequence.Event: Equatable where Base.Element: Equatable, Base.Failure: Equatable {} |
| 110 | + |
| 111 | +@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) |
| 112 | +extension AsyncMaterializedSequence.Completion: CustomDebugStringConvertible { |
| 113 | + |
| 114 | + public var debugDescription: String { |
| 115 | + switch self { |
| 116 | + case .failure(let error): |
| 117 | + ".failure(\(error.localizedDescription))" |
| 118 | + case .finished: |
| 119 | + ".finished" |
| 120 | + } |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) |
| 125 | +extension AsyncMaterializedSequence.Event: CustomDebugStringConvertible { |
| 126 | + |
| 127 | + public var debugDescription: String { |
| 128 | + switch self { |
| 129 | + case .value(let element): |
| 130 | + ".value(\(element))" |
| 131 | + case .completed(let completion): |
| 132 | + ".completed(\(completion))" |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments