@@ -22,7 +22,7 @@ public struct PartialAsyncTask {
2222}
2323
2424@frozen
25- public struct UnsafeContinuation < T, E: Error > : _Continuation {
25+ public struct UnsafeContinuation < T, E: Error > {
2626 @usableFromInline internal var context : Builtin . RawUnsafeContinuation
2727
2828 @_alwaysEmitIntoClient
@@ -34,6 +34,18 @@ public struct UnsafeContinuation<T, E: Error>: _Continuation {
3434 @_silgen_name ( " swift_continuation_resume " )
3535 internal func _resume( returning value: __owned T)
3636
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.
3749 @_alwaysEmitIntoClient
3850 public func resume( returning value: __owned T) where E == Never {
3951 self . _resume ( returning: value)
@@ -43,6 +55,18 @@ public struct UnsafeContinuation<T, E: Error>: _Continuation {
4355 @_silgen_name ( " swift_continuation_throwingResume " )
4456 internal func _resume( returningToThrowingFunction: __owned T)
4557
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.
4670 @_alwaysEmitIntoClient
4771 public func resume( returning value: __owned T) {
4872 self . _resume ( returningToThrowingFunction: value)
@@ -52,12 +76,89 @@ public struct UnsafeContinuation<T, E: Error>: _Continuation {
5276 @_silgen_name ( " swift_continuation_throwingResumeWithError " )
5377 internal func _resume( throwing: __owned Error)
5478
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.
5591 @_alwaysEmitIntoClient
5692 public func resume( throwing error: __owned E) {
5793 self . _resume ( throwing: error)
5894 }
5995}
6096
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+
61162#if _runtime(_ObjC)
62163
63164// Intrinsics used by SILGen to resume or fail continuations.
0 commit comments