1
1
enum DeadlineState < T> : Sendable where T: Sendable {
2
- case result( Result < T , any Error > )
3
- case sleepWasCancelled
4
- case deadlineExceeded
2
+ case operationResult( Result < T , Error > )
3
+ case sleepResult( Result < Void , Error > )
5
4
}
6
5
7
6
/// An error indicating that the deadline has passed and the operation did not complete.
@@ -18,7 +17,7 @@ public struct DeadlineExceededError: Error { }
18
17
/// - operation: The asynchronous operation to be executed.
19
18
///
20
19
/// - Returns: The result of the operation if it completes before the deadline.
21
- /// - Throws: `DeadlineExceededError`, if the operation fails to complete before the deadline and errors thrown by the operation itself .
20
+ /// - Throws: `DeadlineExceededError`, if the operation fails to complete before the deadline and errors thrown by the operation or clock .
22
21
///
23
22
/// ## Examples
24
23
/// To fully understand this, let's illustrate the 3 outcomes of this function:
@@ -86,18 +85,19 @@ public func withDeadline<C, R>(
86
85
87
86
taskGroup. addTask {
88
87
do {
89
- return try await . result( . success( operation ( ) ) )
88
+ let result = try await operation ( )
89
+ return . operationResult( . success( result) )
90
90
} catch {
91
- return . result ( . failure( error) )
91
+ return . operationResult ( . failure( error) )
92
92
}
93
93
}
94
94
95
95
taskGroup. addTask {
96
96
do {
97
97
try await Task . sleep ( until: instant, tolerance: tolerance, clock: clock)
98
- return . deadlineExceeded
98
+ return . sleepResult ( . success ( ( ) ) )
99
99
} catch {
100
- return . sleepWasCancelled
100
+ return . sleepResult ( . failure ( error ) )
101
101
}
102
102
}
103
103
@@ -107,12 +107,14 @@ public func withDeadline<C, R>(
107
107
108
108
for await next in taskGroup {
109
109
switch next {
110
- case let . result ( result) :
110
+ case . operationResult ( let result) :
111
111
return result
112
- case . deadlineExceeded :
112
+ case . sleepResult ( . success ) :
113
113
return . failure( DeadlineExceededError ( ) )
114
- case . sleepWasCancelled :
114
+ case . sleepResult ( . failure ( let error ) ) where error is CancellationError :
115
115
continue
116
+ case . sleepResult( . failure( let error) ) :
117
+ return . failure( error)
116
118
}
117
119
}
118
120
@@ -134,7 +136,7 @@ public func withDeadline<C, R>(
134
136
/// - operation: The asynchronous operation to be executed.
135
137
///
136
138
/// - Returns: The result of the operation if it completes before the deadline.
137
- /// - Throws: `DeadlineExceededError`, if the operation fails to complete before the deadline and errors thrown by the operation itself .
139
+ /// - Throws: `DeadlineExceededError`, if the operation fails to complete before the deadline and errors thrown by the operation or clock .
138
140
///
139
141
/// ## Examples
140
142
/// To fully understand this, let's illustrate the 3 outcomes of this function:
0 commit comments