Skip to content

Commit 435251f

Browse files
committed
chore: only use swift 6
1 parent cde9510 commit 435251f

File tree

2 files changed

+5
-200
lines changed

2 files changed

+5
-200
lines changed

Package.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version: 5.9
1+
// swift-tools-version: 6.0
22

33
import PackageDescription
44

@@ -13,16 +13,15 @@ let package = Package(
1313
],
1414
targets: [
1515
.target(
16-
name: "Deadline",
17-
swiftSettings: [.enableExperimentalFeature("StrictConcurrency")]
16+
name: "Deadline"
1817
),
1918
.testTarget(
2019
name: "DeadlineTests",
2120
dependencies: [
2221
"Deadline",
2322
.product(name: "Clocks", package: "swift-clocks")
24-
],
25-
swiftSettings: [.enableExperimentalFeature("StrictConcurrency")]
23+
]
2624
)
27-
]
25+
],
26+
swiftLanguageModes: [.v6]
2827
)

Sources/Deadline/Deadline.swift

Lines changed: 0 additions & 194 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ enum DeadlineState<T>: Sendable where T: Sendable {
66
/// An error indicating that the deadline has passed and the operation did not complete.
77
public struct DeadlineExceededError: Error { }
88

9-
#if swift(>=6.0)
109
/// Race the given operation against a deadline.
1110
///
1211
/// This function provides a mechanism for enforcing timeouts on asynchronous operations that lack native deadline support. It creates a `TaskGroup` with two concurrent tasks: the provided operation and a sleep task.
@@ -203,196 +202,3 @@ public func deadline<R>(
203202
) async throws -> R where R: Sendable {
204203
try await deadline(until: instant, tolerance: tolerance, clock: ContinuousClock(), isolation: isolation, operation: operation)
205204
}
206-
#else
207-
/// Race the given operation against a deadline.
208-
///
209-
/// This function provides a mechanism for enforcing timeouts on asynchronous operations that lack native deadline support. It creates a `TaskGroup` with two concurrent tasks: the provided operation and a sleep task.
210-
///
211-
/// - Parameters:
212-
/// - instant: The absolute deadline for the operation to complete.
213-
/// - tolerance: The allowed tolerance for the deadline.
214-
/// - clock: The clock used for timing the operation.
215-
/// - operation: The asynchronous operation to be executed.
216-
///
217-
/// - Returns: The result of the operation if it completes before the deadline.
218-
/// - Throws: `DeadlineExceededError`, if the operation fails to complete before the deadline and errors thrown by the operation or clock.
219-
///
220-
/// ## Examples
221-
/// To fully understand this, let's illustrate the 3 outcomes of this function:
222-
///
223-
/// ### Outcome 1
224-
/// The operation finishes in time:
225-
/// ```swift
226-
/// let result = try await deadline(until: .now + .seconds(5)) {
227-
/// // Simulate long running task
228-
/// try await Task.sleep(for: .seconds(1))
229-
/// return "success"
230-
/// }
231-
/// ```
232-
/// As you'd expect, result will be "success". The same applies when your operation fails in time:
233-
/// ```swift
234-
/// let result = try await deadline(until: .now + .seconds(5)) {
235-
/// // Simulate long running task
236-
/// try await Task.sleep(for: .seconds(1))
237-
/// throw CustomError()
238-
/// }
239-
/// ```
240-
/// This will throw `CustomError`.
241-
///
242-
/// ## Outcome 2
243-
/// The operation does not finish in time:
244-
/// ```swift
245-
/// let result = try await deadline(until: .now + .seconds(1)) {
246-
/// // Simulate even longer running task
247-
/// try await Task.sleep(for: .seconds(5))
248-
/// return "success"
249-
/// }
250-
/// ```
251-
/// This will throw `DeadlineExceededError` because the operation will not finish in time.
252-
///
253-
/// ## Outcome 3
254-
/// The parent task was cancelled:
255-
/// ```swift
256-
/// let task = Task {
257-
/// do {
258-
/// try await deadline(until: .now + .seconds(5)) {
259-
/// try await URLSession.shared.data(from: url)
260-
/// }
261-
/// } catch {
262-
/// print(error)
263-
/// }
264-
/// }
265-
///
266-
/// task.cancel()
267-
/// ```
268-
/// The print is guaranteed to print `URLError(.cancelled)`.
269-
/// - Important: The operation closure must support cooperative cancellation. Otherwise, the deadline will not be respected.
270-
public func deadline<C, R>(
271-
until instant: C.Instant,
272-
tolerance: C.Instant.Duration? = nil,
273-
clock: C,
274-
operation: @Sendable () async throws -> R
275-
) async throws -> R where C: Clock, R: Sendable {
276-
277-
// NB: This is safe to use, because the closure will not escape the context of this function.
278-
let result = await withoutActuallyEscaping(operation) { operation in
279-
await withTaskGroup(
280-
of: DeadlineState<R>.self,
281-
returning: Result<R, any Error>.self
282-
) { taskGroup in
283-
284-
taskGroup.addTask {
285-
do {
286-
let result = try await operation()
287-
return .operationResult(.success(result))
288-
} catch {
289-
return .operationResult(.failure(error))
290-
}
291-
}
292-
293-
taskGroup.addTask {
294-
do {
295-
try await Task.sleep(until: instant, tolerance: tolerance, clock: clock)
296-
return .sleepResult(.success(false))
297-
} catch where Task.isCancelled {
298-
return .sleepResult(.success(true))
299-
} catch {
300-
return .sleepResult(.failure(error))
301-
}
302-
}
303-
304-
defer {
305-
taskGroup.cancelAll()
306-
}
307-
308-
for await next in taskGroup {
309-
switch next {
310-
case .operationResult(let result):
311-
return result
312-
case .sleepResult(.success(false)):
313-
return .failure(DeadlineExceededError())
314-
case .sleepResult(.success(true)):
315-
continue
316-
case .sleepResult(.failure(let error)):
317-
return .failure(error)
318-
}
319-
}
320-
321-
preconditionFailure("Invalid state")
322-
}
323-
}
324-
325-
return try result.get()
326-
}
327-
328-
/// Race the given operation against a deadline.
329-
///
330-
/// This function provides a mechanism for enforcing timeouts on asynchronous operations that lack native deadline support. It creates a `TaskGroup` with two concurrent tasks: the provided operation and a sleep task.
331-
/// `ContinuousClock` will be used as the default clock.
332-
///
333-
/// - Parameters:
334-
/// - instant: The absolute deadline for the operation to complete.
335-
/// - tolerance: The allowed tolerance for the deadline.
336-
/// - operation: The asynchronous operation to be executed.
337-
///
338-
/// - Returns: The result of the operation if it completes before the deadline.
339-
/// - Throws: `DeadlineExceededError`, if the operation fails to complete before the deadline and errors thrown by the operation or clock.
340-
///
341-
/// ## Examples
342-
/// To fully understand this, let's illustrate the 3 outcomes of this function:
343-
///
344-
/// ### Outcome 1
345-
/// The operation finishes in time:
346-
/// ```swift
347-
/// let result = try await deadline(until: .now + .seconds(5)) {
348-
/// // Simulate long running task
349-
/// try await Task.sleep(for: .seconds(1))
350-
/// return "success"
351-
/// }
352-
/// ```
353-
/// As you'd expect, result will be "success". The same applies when your operation fails in time:
354-
/// ```swift
355-
/// let result = try await deadline(until: .now + .seconds(5)) {
356-
/// // Simulate long running task
357-
/// try await Task.sleep(for: .seconds(1))
358-
/// throw CustomError()
359-
/// }
360-
/// ```
361-
/// This will throw `CustomError`.
362-
///
363-
/// ## Outcome 2
364-
/// The operation does not finish in time:
365-
/// ```swift
366-
/// let result = try await deadline(until: .now + .seconds(1)) {
367-
/// // Simulate even longer running task
368-
/// try await Task.sleep(for: .seconds(5))
369-
/// return "success"
370-
/// }
371-
/// ```
372-
/// This will throw `DeadlineExceededError` because the operation will not finish in time.
373-
///
374-
/// ## Outcome 3
375-
/// The parent task was cancelled:
376-
/// ```swift
377-
/// let task = Task {
378-
/// do {
379-
/// try await deadline(until: .now + .seconds(5)) {
380-
/// try await URLSession.shared.data(from: url)
381-
/// }
382-
/// } catch {
383-
/// print(error)
384-
/// }
385-
/// }
386-
///
387-
/// task.cancel()
388-
/// ```
389-
/// The print is guaranteed to print `URLError(.cancelled)`.
390-
/// - Important: The operation closure must support cooperative cancellation. Otherwise, the deadline will not be respected.
391-
public func deadline<R>(
392-
until instant: ContinuousClock.Instant,
393-
tolerance: ContinuousClock.Instant.Duration? = nil,
394-
operation: @Sendable () async throws -> R
395-
) async throws -> R where R: Sendable {
396-
try await deadline(until: instant, tolerance: tolerance, clock: ContinuousClock(), operation: operation)
397-
}
398-
#endif

0 commit comments

Comments
 (0)