Skip to content

struct Timeout -> TimeoutController #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ let val = try await withThrowingTimeout(seconds: 2.0) {

> Note: When the timeout expires the task executing the closure is cancelled and `TimeoutError` is thrown.

An overload includes a `Timeout` object allowing the body to cancel or move the expiration where required:
An overload includes a `TimeoutController` object allowing the body to cancel or move the expiration where required:

```swift
try await withThrowingTimeout(seconds: 1.0) { timeout in
Expand Down
13 changes: 8 additions & 5 deletions Sources/Timeout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@
#if compiler(>=6.0)
import Foundation

public struct Timeout: Sendable {
@available(*, unavailable, renamed: "TimeoutController")
public typealias Timeout = TimeoutController

public struct TimeoutController: Sendable {
fileprivate var canary: @Sendable () -> Void
fileprivate let shared: SharedState

Expand Down Expand Up @@ -74,7 +77,7 @@ public struct Timeout: Sendable {
}

@available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *)
public extension Timeout {
public extension TimeoutController {

@discardableResult
func expire<C: Clock>(
Expand All @@ -97,7 +100,7 @@ public extension Timeout {
}
}

extension Timeout {
extension TimeoutController {

init(
canary: @escaping @Sendable () -> Void,
Expand Down Expand Up @@ -156,13 +159,13 @@ extension Timeout {
func withNonEscapingTimeout<T>(
_ timeout: @escaping @Sendable () async throws -> Never,
isolation: isolated (any Actor)? = #isolation,
body: (Timeout) async throws -> sending T
body: (TimeoutController) async throws -> sending T
) async throws -> sending T {
// canary ensuring Timeout does not escape at runtime.
// Swift 6.2 and later enforce at compile time with ~Escapable
try await withoutActuallyEscaping({ @Sendable in }) { escaping in
_ = isolation
let timeout = Timeout(canary: escaping, pending: timeout)
let timeout = TimeoutController(canary: escaping, pending: timeout)
return try await Transferring(body(timeout))
}.value
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/withThrowingTimeout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public func withThrowingTimeout<T>(
public func withThrowingTimeout<T>(
isolation: isolated (any Actor)? = #isolation,
seconds: TimeInterval,
body: (Timeout) async throws -> sending T
body: (TimeoutController) async throws -> sending T
) async throws -> sending T {
try await _withThrowingTimeout(isolation: isolation, body: body) {
try await Task.sleep(nanoseconds: UInt64(seconds * 1_000_000_000))
Expand Down Expand Up @@ -91,7 +91,7 @@ public func withThrowingTimeout<T>(

private func _withThrowingTimeout<T>(
isolation: isolated (any Actor)? = #isolation,
body: (Timeout) async throws -> sending T,
body: (TimeoutController) async throws -> sending T,
timeout closure: @Sendable @escaping () async throws -> Never
) async throws -> Transferring<T> {
try await withoutActuallyEscaping(body) { escapingBody in
Expand Down
Loading