-
Notifications
You must be signed in to change notification settings - Fork 161
/
Copy pathAsyncDeferredSequence.swift
101 lines (86 loc) · 3.08 KB
/
AsyncDeferredSequence.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Async Algorithms open source project
//
// Copyright (c) 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
/// Creates a ``AsyncDeferredSequence`` that uses the supplied closure to create a new `AsyncSequence`.
/// The closure is executed for each iterator on the first call to `next`.
/// This has the effect of postponing the initialization of an arbitrary `AsyncSequence` until the point of first demand.
@inlinable
public func deferred<Base>(
_ createSequence: @escaping @Sendable () async -> Base
) -> AsyncDeferredSequence<Base> where Base: AsyncSequence, Base: Sendable {
AsyncDeferredSequence(createSequence)
}
/// Creates a ``AsyncDeferredSequence`` that uses the supplied closure to create a new `AsyncSequence`.
/// The closure is executed for each iterator on the first call to `next`.
/// This has the effect of postponing the initialization of an arbitrary `AsyncSequence` until the point of first demand.
@inlinable
public func deferred<Base: AsyncSequence & Sendable>(
_ createSequence: @autoclosure @escaping @Sendable () -> Base
) -> AsyncDeferredSequence<Base> where Base: AsyncSequence, Base: Sendable {
AsyncDeferredSequence(createSequence)
}
@frozen
public struct AsyncDeferredSequence<Base> where Base: AsyncSequence, Base: Sendable {
@usableFromInline
let createSequence: @Sendable () async -> Base
@inlinable
init(_ createSequence: @escaping @Sendable () async -> Base) {
self.createSequence = createSequence
}
}
extension AsyncDeferredSequence: AsyncSequence {
public typealias Element = Base.Element
public struct Iterator: AsyncIteratorProtocol {
@usableFromInline
enum State {
case pending(@Sendable () async -> Base)
case active(Base.AsyncIterator)
case terminal
}
@usableFromInline
var state: State
@inlinable
init(_ createSequence: @escaping @Sendable () async -> Base) {
self.state = .pending(createSequence)
}
@inlinable
public mutating func next() async rethrows -> Element? {
switch state {
case .pending(let generator):
state = .active(await generator().makeAsyncIterator())
return try await next()
case .active(var base):
do {
if let value = try await base.next() {
state = .active(base)
return value
}
else {
state = .terminal
return nil
}
}
catch let error {
state = .terminal
throw error
}
case .terminal:
return nil
}
}
}
@inlinable
public func makeAsyncIterator() -> Iterator {
Iterator(createSequence)
}
}
extension AsyncDeferredSequence: Sendable { }
@available(*, unavailable)
extension AsyncDeferredSequence.Iterator: Sendable { }