Skip to content

Commit

Permalink
support any Clock in Swift 5
Browse files Browse the repository at this point in the history
  • Loading branch information
swhitty committed Sep 13, 2024
1 parent 313e46f commit 28e90fc
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion Sources/Timeout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,26 @@ public func withThrowingTimeout<T>(
after instant: ContinuousClock.Instant,
tolerance: ContinuousClock.Instant.Duration? = nil,
body: () async throws -> T
) async throws -> T {
try await withThrowingTimeout(
after: instant,
tolerance: tolerance,
clock: ContinuousClock(),
body: body
)
}

@available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *)
public func withThrowingTimeout<T, C: Clock>(
after instant: C.Instant,
tolerance: C.Instant.Duration? = nil,
clock: C,
body: () async throws -> T
) async throws -> T {
let transferringBody = { try await Transferring(body()) }
return try await withoutActuallyEscaping(transferringBody) {
try await _withThrowingTimeout(body: $0) {
try await Task.sleep(until: instant, tolerance: tolerance, clock: ContinuousClock())
try await Task.sleep(until: instant, tolerance: tolerance, clock: clock)
throw TimeoutError("Task timed out before completion. Deadline: \(instant).")
}
}.value
Expand Down

0 comments on commit 28e90fc

Please sign in to comment.