@@ -22,7 +22,7 @@ public struct PartialAsyncTask {
22
22
}
23
23
24
24
@frozen
25
- public struct UnsafeContinuation < T, E: Error > : _Continuation {
25
+ public struct UnsafeContinuation < T, E: Error > {
26
26
@usableFromInline internal var context : Builtin . RawUnsafeContinuation
27
27
28
28
@_alwaysEmitIntoClient
@@ -34,6 +34,18 @@ public struct UnsafeContinuation<T, E: Error>: _Continuation {
34
34
@_silgen_name ( " swift_continuation_resume " )
35
35
internal func _resume( returning value: __owned T)
36
36
37
+ /// Resume the task awaiting the continuation by having it return normally
38
+ /// from its suspension point.
39
+ ///
40
+ /// - Parameter value: The value to return from the continuation.
41
+ ///
42
+ /// A continuation must be resumed exactly once. If the continuation has
43
+ /// already been resumed through this object, then the attempt to resume
44
+ /// the continuation again will result in undefined behavior.
45
+ ///
46
+ /// After `resume` enqueues the task, control is immediately returned to
47
+ /// the caller. The task will continue executing when its executor is
48
+ /// able to reschedule it.
37
49
@_alwaysEmitIntoClient
38
50
public func resume( returning value: __owned T) where E == Never {
39
51
self . _resume ( returning: value)
@@ -43,6 +55,18 @@ public struct UnsafeContinuation<T, E: Error>: _Continuation {
43
55
@_silgen_name ( " swift_continuation_throwingResume " )
44
56
internal func _resume( returningToThrowingFunction: __owned T)
45
57
58
+ /// Resume the task awaiting the continuation by having it return normally
59
+ /// from its suspension point.
60
+ ///
61
+ /// - Parameter value: The value to return from the continuation.
62
+ ///
63
+ /// A continuation must be resumed exactly once. If the continuation has
64
+ /// already been resumed through this object, then the attempt to resume
65
+ /// the continuation again will result in undefined behavior.
66
+ ///
67
+ /// After `resume` enqueues the task, control is immediately returned to
68
+ /// the caller. The task will continue executing when its executor is
69
+ /// able to reschedule it.
46
70
@_alwaysEmitIntoClient
47
71
public func resume( returning value: __owned T) {
48
72
self . _resume ( returningToThrowingFunction: value)
@@ -52,12 +76,89 @@ public struct UnsafeContinuation<T, E: Error>: _Continuation {
52
76
@_silgen_name ( " swift_continuation_throwingResumeWithError " )
53
77
internal func _resume( throwing: __owned Error)
54
78
79
+ /// Resume the task awaiting the continuation by having it throw an error
80
+ /// from its suspension point.
81
+ ///
82
+ /// - Parameter error: The error to throw from the continuation.
83
+ ///
84
+ /// A continuation must be resumed exactly once. If the continuation has
85
+ /// already been resumed through this object, then the attempt to resume
86
+ /// the continuation again will result in undefined behavior.
87
+ ///
88
+ /// After `resume` enqueues the task, control is immediately returned to
89
+ /// the caller. The task will continue executing when its executor is
90
+ /// able to reschedule it.
55
91
@_alwaysEmitIntoClient
56
92
public func resume( throwing error: __owned E) {
57
93
self . _resume ( throwing: error)
58
94
}
59
95
}
60
96
97
+ extension UnsafeContinuation {
98
+ /// Resume the task awaiting the continuation by having it either
99
+ /// return normally or throw an error based on the state of the given
100
+ /// `Result` value.
101
+ ///
102
+ /// - Parameter result: A value to either return or throw from the
103
+ /// continuation.
104
+ ///
105
+ /// A continuation must be resumed exactly once. If the continuation has
106
+ /// already been resumed through this object, then the attempt to resume
107
+ /// the continuation again will trap.
108
+ ///
109
+ /// After `resume` enqueues the task, control is immediately returned to
110
+ /// the caller. The task will continue executing when its executor is
111
+ /// able to reschedule it.
112
+ @_alwaysEmitIntoClient
113
+ public func resume< Er: Error > ( with result: Result < T , Er > ) where E == Error {
114
+ switch result {
115
+ case . success( let val) :
116
+ self . resume ( returning: val)
117
+ case . failure( let err) :
118
+ self . resume ( throwing: err)
119
+ }
120
+ }
121
+
122
+ /// Resume the task awaiting the continuation by having it either
123
+ /// return normally or throw an error based on the state of the given
124
+ /// `Result` value.
125
+ ///
126
+ /// - Parameter result: A value to either return or throw from the
127
+ /// continuation.
128
+ ///
129
+ /// A continuation must be resumed exactly once. If the continuation has
130
+ /// already been resumed through this object, then the attempt to resume
131
+ /// the continuation again will trap.
132
+ ///
133
+ /// After `resume` enqueues the task, control is immediately returned to
134
+ /// the caller. The task will continue executing when its executor is
135
+ /// able to reschedule it.
136
+ @_alwaysEmitIntoClient
137
+ public func resume( with result: Result < T , E > ) {
138
+ switch result {
139
+ case . success( let val) :
140
+ self . resume ( returning: val)
141
+ case . failure( let err) :
142
+ self . resume ( throwing: err)
143
+ }
144
+ }
145
+
146
+ /// Resume the task awaiting the continuation by having it return normally
147
+ /// from its suspension point.
148
+ ///
149
+ /// A continuation must be resumed exactly once. If the continuation has
150
+ /// already been resumed through this object, then the attempt to resume
151
+ /// the continuation again will trap.
152
+ ///
153
+ /// After `resume` enqueues the task, control is immediately returned to
154
+ /// the caller. The task will continue executing when its executor is
155
+ /// able to reschedule it.
156
+ @_alwaysEmitIntoClient
157
+ public func resume( ) where T == Void {
158
+ self . resume ( returning: ( ) )
159
+ }
160
+ }
161
+
61
162
#if _runtime(_ObjC)
62
163
63
164
// Intrinsics used by SILGen to resume or fail continuations.
0 commit comments