Skip to content

Commit d9398af

Browse files
committed
feat: Clock is not guaranteed to only throw CancellationError, it is not ignored anymore
1 parent 276952a commit d9398af

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

Sources/Deadline/Deadline.swift

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
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>)
54
}
65

76
/// An error indicating that the deadline has passed and the operation did not complete.
@@ -18,7 +17,7 @@ public struct DeadlineExceededError: Error { }
1817
/// - operation: The asynchronous operation to be executed.
1918
///
2019
/// - 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.
2221
///
2322
/// ## Examples
2423
/// To fully understand this, let's illustrate the 3 outcomes of this function:
@@ -86,18 +85,19 @@ public func withDeadline<C, R>(
8685

8786
taskGroup.addTask {
8887
do {
89-
return try await .result(.success(operation()))
88+
let result = try await operation()
89+
return .operationResult(.success(result))
9090
} catch {
91-
return .result(.failure(error))
91+
return .operationResult(.failure(error))
9292
}
9393
}
9494

9595
taskGroup.addTask {
9696
do {
9797
try await Task.sleep(until: instant, tolerance: tolerance, clock: clock)
98-
return .deadlineExceeded
98+
return .sleepResult(.success(()))
9999
} catch {
100-
return .sleepWasCancelled
100+
return .sleepResult(.failure(error))
101101
}
102102
}
103103

@@ -107,12 +107,14 @@ public func withDeadline<C, R>(
107107

108108
for await next in taskGroup {
109109
switch next {
110-
case let .result(result):
110+
case .operationResult(let result):
111111
return result
112-
case .deadlineExceeded:
112+
case .sleepResult(.success):
113113
return .failure(DeadlineExceededError())
114-
case .sleepWasCancelled:
114+
case .sleepResult(.failure(let error)) where error is CancellationError:
115115
continue
116+
case .sleepResult(.failure(let error)):
117+
return .failure(error)
116118
}
117119
}
118120

@@ -134,7 +136,7 @@ public func withDeadline<C, R>(
134136
/// - operation: The asynchronous operation to be executed.
135137
///
136138
/// - 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.
138140
///
139141
/// ## Examples
140142
/// To fully understand this, let's illustrate the 3 outcomes of this function:

0 commit comments

Comments
 (0)