Skip to content

Commit 28e90fc

Browse files
committed
support any Clock in Swift 5
1 parent 313e46f commit 28e90fc

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

Sources/Timeout.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,26 @@ public func withThrowingTimeout<T>(
134134
after instant: ContinuousClock.Instant,
135135
tolerance: ContinuousClock.Instant.Duration? = nil,
136136
body: () async throws -> T
137+
) async throws -> T {
138+
try await withThrowingTimeout(
139+
after: instant,
140+
tolerance: tolerance,
141+
clock: ContinuousClock(),
142+
body: body
143+
)
144+
}
145+
146+
@available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *)
147+
public func withThrowingTimeout<T, C: Clock>(
148+
after instant: C.Instant,
149+
tolerance: C.Instant.Duration? = nil,
150+
clock: C,
151+
body: () async throws -> T
137152
) async throws -> T {
138153
let transferringBody = { try await Transferring(body()) }
139154
return try await withoutActuallyEscaping(transferringBody) {
140155
try await _withThrowingTimeout(body: $0) {
141-
try await Task.sleep(until: instant, tolerance: tolerance, clock: ContinuousClock())
156+
try await Task.sleep(until: instant, tolerance: tolerance, clock: clock)
142157
throw TimeoutError("Task timed out before completion. Deadline: \(instant).")
143158
}
144159
}.value

0 commit comments

Comments
 (0)