Files
swift-mirror/stdlib/public/Concurrency/SourceCompatibilityShims.swift
Doug Gregor eda5b4daad [Concurrency] Alternative Task API.
The `Task` type has oscillated somewhat from being purely a namespace,
to having instances that are used (albeit rarely), back to purely
being a namespace that isn't used for all that many names. Many of the
names that used to be on Task have already been moved out, e.g., for
creating new detached tasks, creating new task groups, adding
cancellation handlers, etc.

Collapse `Task.Handle<Success, Failure>` into `Task<Success, Failure>`.
`Task.Handle` is the type that is most frequently referenced in the
concurrency library, so giving it the short name `Task` is most
appropriate. Replace the top-level async/detach functions with a
`Task` initializer and `Task.detached`, respectively.

The `Task` type can still act as a namespace for static operations
such as, e.g., `Task.isCancelled`. Do this with an extension of the
form:

    extension Task where Success == Never, Failure == Never { ... }

We've been accruing a number of compatibility shims. Move them all
into their own source file, deprecate them, and make them
always-emit-into-client so they don't have any ABI impact.
2021-05-18 14:36:21 -07:00

210 lines
7.6 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// This file provides source compatibility shims to help migrate code
// using earlier versions of the concurrency library to the latest syntax.
//===----------------------------------------------------------------------===//
import Swift
@_implementationOnly import _SwiftConcurrencyShims
@available(SwiftStdlib 5.5, *)
extension Task where Success == Never, Failure == Never {
@available(*, deprecated, message: "use TaskPriority")
public typealias Priority = TaskPriority
@available(*, deprecated, message: "use Task")
public typealias Handle = _Concurrency.Task
}
@available(SwiftStdlib 5.5, *)
extension TaskPriority {
@available(*, deprecated, message: "unspecified priority will be removed; use nil")
public static let unspecified: TaskPriority = .init(rawValue: 0x00)
}
@available(SwiftStdlib 5.5, *)
extension Task where Success == Never, Failure == Never {
@available(*, deprecated, message: "`Task.withCancellationHandler` has been replaced by `withTaskCancellationHandler` and will be removed shortly.")
@_alwaysEmitIntoClient
public static func withCancellationHandler<T>(
handler: @Sendable () -> (),
operation: () async throws -> T
) async rethrows -> T {
try await withTaskCancellationHandler(handler: handler, operation: operation)
}
}
@available(SwiftStdlib 5.5, *)
extension Task where Failure == Error {
@discardableResult
@_alwaysEmitIntoClient
@available(*, deprecated, message: "`Task.runDetached` was replaced by `Task.detached` and will be removed shortly.")
public static func runDetached(
priority: TaskPriority = .unspecified,
operation: __owned @Sendable @escaping () async throws -> Success
) -> Task<Success, Failure> {
detached(priority: priority, operation: operation)
}
}
@discardableResult
@available(SwiftStdlib 5.5, *)
@available(*, deprecated, message: "`detach` was replaced by `Task.detached` and will be removed shortly.")
@_alwaysEmitIntoClient
public func detach<T>(
priority: TaskPriority? = nil,
operation: __owned @Sendable @escaping () async -> T
) -> Task<T, Never> {
Task.detached(priority: priority, operation: operation)
}
@discardableResult
@available(SwiftStdlib 5.5, *)
@available(*, deprecated, message: "`detach` was replaced by `Task.detached` and will be removed shortly.")
@_alwaysEmitIntoClient
public func detach<T>(
priority: TaskPriority? = nil,
operation: __owned @Sendable @escaping () async throws -> T
) -> Task<T, Error> {
Task.detached(priority: priority, operation: operation)
}
@discardableResult
@available(SwiftStdlib 5.5, *)
@available(*, deprecated, message: "`asyncDetached` was replaced by `Task.detached` and will be removed shortly.")
@_alwaysEmitIntoClient
public func asyncDetached<T>(
priority: TaskPriority? = nil,
@_implicitSelfCapture operation: __owned @Sendable @escaping () async -> T
) -> Task<T, Never> {
return Task.detached(priority: priority, operation: operation)
}
@discardableResult
@available(SwiftStdlib 5.5, *)
@available(*, deprecated, message: "`asyncDetached` was replaced by `Task.detached` and will be removed shortly.")
@_alwaysEmitIntoClient
public func asyncDetached<T>(
priority: TaskPriority? = nil,
@_implicitSelfCapture operation: __owned @Sendable @escaping () async throws -> T
) -> Task<T, Error> {
return Task.detached(priority: priority, operation: operation)
}
@available(SwiftStdlib 5.5, *)
@available(*, deprecated, message: "`async` was replaced by `Task.init` and will be removed shortly.")
@discardableResult
@_alwaysEmitIntoClient
public func async<T>(
priority: TaskPriority? = nil,
@_inheritActorContext @_implicitSelfCapture operation: __owned @Sendable @escaping () async -> T
) -> Task<T, Never> {
.init(priority: priority, operation: operation)
}
@available(SwiftStdlib 5.5, *)
@available(*, deprecated, message: "`async` was replaced by `Task.init` and will be removed shortly.")
@discardableResult
@_alwaysEmitIntoClient
public func async<T>(
priority: TaskPriority? = nil,
@_inheritActorContext @_implicitSelfCapture operation: __owned @Sendable @escaping () async throws -> T
) -> Task<T, Error> {
.init(priority: priority, operation: operation)
}
@available(SwiftStdlib 5.5, *)
extension Task where Success == Never, Failure == Never {
@available(*, deprecated, message: "`Task.Group` was replaced by `ThrowingTaskGroup` and `TaskGroup` and will be removed shortly.")
public typealias Group<TaskResult> = ThrowingTaskGroup<TaskResult, Error>
@available(*, deprecated, message: "`Task.withGroup` was replaced by `withThrowingTaskGroup` and `withTaskGroup` and will be removed shortly.")
@_alwaysEmitIntoClient
public static func withGroup<TaskResult, BodyResult>(
resultType: TaskResult.Type,
returning returnType: BodyResult.Type = BodyResult.self,
body: (inout Task.Group<TaskResult>) async throws -> BodyResult
) async rethrows -> BodyResult {
try await withThrowingTaskGroup(of: resultType) { group in
try await body(&group)
}
}
}
@available(SwiftStdlib 5.5, *)
extension TaskGroup {
@available(*, deprecated, renamed: "async(priority:operation:)")
@_alwaysEmitIntoClient
public mutating func add(
priority: TaskPriority? = nil,
operation: __owned @Sendable @escaping () async -> ChildTaskResult
) async -> Bool {
return self.asyncUnlessCancelled(priority: priority) {
await operation()
}
}
@available(*, deprecated, renamed: "async(priority:operation:)")
@_alwaysEmitIntoClient
public mutating func spawn(
priority: TaskPriority? = nil,
operation: __owned @Sendable @escaping () async -> ChildTaskResult
) {
async(priority: priority, operation: operation)
}
@available(*, deprecated, renamed: "asyncUnlessCancelled(priority:operation:)")
@_alwaysEmitIntoClient
public mutating func spawnUnlessCancelled(
priority: TaskPriority? = nil,
operation: __owned @Sendable @escaping () async -> ChildTaskResult
) -> Bool {
asyncUnlessCancelled(priority: priority, operation: operation)
}
}
@available(SwiftStdlib 5.5, *)
extension ThrowingTaskGroup {
@available(*, deprecated, renamed: "async(priority:operation:)")
@_alwaysEmitIntoClient
public mutating func add(
priority: TaskPriority? = nil,
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
) async -> Bool {
return self.asyncUnlessCancelled(priority: priority) {
try await operation()
}
}
@available(*, deprecated, renamed: "async(priority:operation:)")
@_alwaysEmitIntoClient
public mutating func spawn(
priority: TaskPriority? = nil,
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
) {
async(priority: priority, operation: operation)
}
@available(*, deprecated, renamed: "asyncUnlessCancelled(priority:operation:)")
@_alwaysEmitIntoClient
public mutating func spawnUnlessCancelled(
priority: TaskPriority? = nil,
operation: __owned @Sendable @escaping () async throws -> ChildTaskResult
) -> Bool {
asyncUnlessCancelled(priority: priority, operation: operation)
}
}
@available(SwiftStdlib 5.5, *)
@available(*, deprecated, message: "please use UnsafeContination<..., Error>")
public typealias UnsafeThrowingContinuation<T> = UnsafeContinuation<T, Error>