From 712c0242600b224cdf46eb273e4b44e641d2f6c4 Mon Sep 17 00:00:00 2001 From: Zachary Waldowski Date: Mon, 14 Jan 2019 16:04:49 -0500 Subject: [PATCH] Align SuccessValue/FailureValue with Success/Failure in Swift 5 --- Sources/Task/ExistentialTask.swift | 29 +++++++++++++++++------------ Sources/Task/Task.swift | 24 ++++++++++++------------ Sources/Task/TaskAndThen.swift | 8 ++++---- Sources/Task/TaskAsync.swift | 4 ++-- Sources/Task/TaskChain.swift | 4 ++-- Sources/Task/TaskCollections.swift | 20 ++++++++++---------- Sources/Task/TaskFallback.swift | 24 ++++++++++++------------ Sources/Task/TaskIgnore.swift | 2 +- Sources/Task/TaskMap.swift | 12 ++++++------ Sources/Task/TaskPromise.swift | 6 +++--- Sources/Task/TaskRecovery.swift | 12 ++++++------ Sources/Task/TaskResult.swift | 22 +++++++++++----------- Sources/Task/TaskUpon.swift | 8 ++++---- 13 files changed, 90 insertions(+), 85 deletions(-) diff --git a/Sources/Task/ExistentialTask.swift b/Sources/Task/ExistentialTask.swift index 125eb06b..1f922891 100755 --- a/Sources/Task/ExistentialTask.swift +++ b/Sources/Task/ExistentialTask.swift @@ -188,14 +188,19 @@ import Deferred.Atomics /// /// - seealso: `TaskProtocol` /// - seealso: `Future` -public final class Task { +public final class Task { + @available(*, unavailable, renamed: "Success", message: "Renamed 'Success' to better align with SE-0235, the Swift 5 Result type.") + public typealias SuccessValue = Success + /// A type that represents either a wrapped value or an error, representing the /// possible return values of a throwing function. public enum Result { + /// Any error. + public typealias Failure = Error /// The success value, stored as `Value`. - case success(SuccessValue) + case success(Success) /// The failure value, stored as any error. - case failure(Error) + case failure(Failure) } private let future: Future @@ -215,7 +220,7 @@ public final class Task { } /// Creates a task whose `upon(_:execute:)` methods use those of `base`. - public init(_ wrapped: Wrapped, progress: Progress) where Wrapped.SuccessValue == SuccessValue { + public init(_ wrapped: Wrapped, progress: Progress) where Wrapped.Success == Success { self.future = Future(resultFrom: wrapped) self.progress = TaskChain(startingWith: wrapped, using: progress).effectiveProgress } @@ -241,7 +246,7 @@ public final class Task { /// /// `cancellation` will be called asynchronously, but not on any specific /// queue. If you must do work on a specific queue, schedule work on it. - public init(_ wrapped: Wrapped, uponCancel cancellation: (() -> Void)? = nil) where Wrapped.SuccessValue == SuccessValue { + public init(_ wrapped: Wrapped, uponCancel cancellation: (() -> Void)? = nil) where Wrapped.Success == Success { self.future = Future(resultFrom: wrapped) #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) self.progress = TaskChain(startingWith: wrapped, uponCancel: cancellation).effectiveProgress @@ -304,7 +309,7 @@ extension Task { #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) /// Creates a task whose `upon(_:execute:)` methods use those of `base`. - public convenience init(succeedsFrom wrapped: Wrapped, progress: Progress) where Wrapped.Value == SuccessValue { + public convenience init(succeedsFrom wrapped: Wrapped, progress: Progress) where Wrapped.Value == Success { let future = Future(succeedsFrom: wrapped) self.init(future, progress: progress) } @@ -314,25 +319,25 @@ extension Task { /// /// `cancellation` will be called asynchronously, but not on any specific /// queue. If you must do work on a specific queue, schedule work on it. - public convenience init(succeedsFrom wrapped: Wrapped, uponCancel cancellation: (() -> Void)? = nil) where Wrapped.Value == SuccessValue { + public convenience init(succeedsFrom wrapped: Wrapped, uponCancel cancellation: (() -> Void)? = nil) where Wrapped.Value == Success { let future = Future(succeedsFrom: wrapped) self.init(future, uponCancel: cancellation) } /// Creates an operation that has already completed with `value`. - public convenience init(success value: @autoclosure() throws -> SuccessValue) { + public convenience init(success value: @autoclosure() throws -> Success) { let future = Future(success: value) self.init(future) } /// Creates an operation that has already failed with `error`. - public convenience init(failure error: Error) { + public convenience init(failure error: Failure) { let future = Future(failure: error) self.init(future) } /// Creates a task having the same underlying operation as the `other` task. - public convenience init(_ task: Task) { + public convenience init(_ task: Task) { #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) self.init(task.future, progress: task.progress) #else @@ -344,7 +349,7 @@ extension Task { } /// Create a task that will never complete. - public static var never: Task { + public static var never: Task { return Task(Future.never) } } @@ -367,7 +372,7 @@ extension Task { } @available(*, unavailable, renamed: "init(succeedsFrom:progress:)", message: "Replace with 'init(succeedsFrom:progress:)' to disambiguate from a completed Task.") - public convenience init(success wrapped: Wrapped, progress: Progress) where Wrapped.Value == SuccessValue { + public convenience init(success wrapped: Wrapped, progress: Progress) where Wrapped.Value == Success { fatalError("unavailable initializer cannot be called") } #endif diff --git a/Sources/Task/Task.swift b/Sources/Task/Task.swift index d3d2ca3a..a33c3d02 100755 --- a/Sources/Task/Task.swift +++ b/Sources/Task/Task.swift @@ -21,10 +21,10 @@ import Deferred /// - seealso: `FutureProtocol` public protocol TaskProtocol: FutureProtocol where Value: Either { /// A type that represents the success of some asynchronous work. - associatedtype SuccessValue where SuccessValue == Value.Right + associatedtype Success where Success == Value.Right /// A type that represents the failure of some asynchronous work. - typealias FailureValue = Error + typealias Failure = Error /// Call some `body` closure if the task successfully completes. /// @@ -32,7 +32,7 @@ public protocol TaskProtocol: FutureProtocol where Value: Either { /// - parameter body: A closure to be invoked when the result is determined. /// * parameter value: The determined success value. /// - seealso: `FutureProtocol.upon(_:execute:)` - func uponSuccess(on executor: Executor, execute body: @escaping(_ value: SuccessValue) -> Void) + func uponSuccess(on executor: Executor, execute body: @escaping(_ value: Success) -> Void) /// Call some `body` closure if the task fails. /// @@ -40,7 +40,7 @@ public protocol TaskProtocol: FutureProtocol where Value: Either { /// - parameter body: A closure to be invoked when the result is determined. /// * parameter error: The determined failure value. /// - seealso: `FutureProtocol.upon(_:execute:)` - func uponFailure(on executor: Executor, execute body: @escaping(_ error: FailureValue) -> Void) + func uponFailure(on executor: Executor, execute body: @escaping(_ error: Failure) -> Void) /// Tests whether the underlying work has been cancelled. /// @@ -80,43 +80,43 @@ extension TaskProtocol { // MARK: - Conditional conformances extension Future: TaskProtocol where Value: Either { - public typealias SuccessValue = Value.Right + public typealias Success = Value.Right /// Create a future having the same underlying task as `other`. - public init(resultFrom wrapped: Wrapped) where Wrapped.SuccessValue == SuccessValue { + public init(resultFrom wrapped: Wrapped) where Wrapped.Success == Success { self = wrapped as? Future ?? wrapped.every { (result) -> Value in Value(catching: result.get) } } /// Create a future having the same underlying task as `other`. - public init(succeedsFrom wrapped: Wrapped) where Wrapped.Value == SuccessValue { + public init(succeedsFrom wrapped: Wrapped) where Wrapped.Value == Success { self = wrapped.every(per: Value.init(right:)) } /// Creates an future having already filled successfully with `value`. - public init(success value: @autoclosure() throws -> SuccessValue) { + public init(success value: @autoclosure() throws -> Success) { self.init(value: Value(catching: value)) } /// Creates an future having already failed with `error`. - public init(failure error: FailureValue) { + public init(failure error: Failure) { self.init(value: Value(left: error)) } } extension Future where Value: Either { @available(*, unavailable, renamed: "init(resultFrom:)") - public init(task wrapped: Wrapped) where Wrapped.SuccessValue == SuccessValue { + public init(task wrapped: Wrapped) where Wrapped.Success == Success { fatalError("unavailable initializer cannot be called") } @available(*, unavailable, renamed: "init(succeedsFrom:)") - public init(success wrapped: Wrapped) where Wrapped.Value == SuccessValue { + public init(success wrapped: Wrapped) where Wrapped.Value == Success { fatalError("unavailable initializer cannot be called") } } extension Deferred: TaskProtocol where Value: Either { - public typealias SuccessValue = Value.Right + public typealias Success = Value.Right } diff --git a/Sources/Task/TaskAndThen.swift b/Sources/Task/TaskAndThen.swift index 07cfdc5f..0c39e6bb 100644 --- a/Sources/Task/TaskAndThen.swift +++ b/Sources/Task/TaskAndThen.swift @@ -22,7 +22,7 @@ extension TaskProtocol { /// /// Cancelling the resulting task will attempt to cancel both the receiving /// task and the created task. - public func andThen(upon executor: PreferredExecutor, start startNextTask: @escaping(SuccessValue) throws -> NewTask) -> Task { + public func andThen(upon executor: PreferredExecutor, start startNextTask: @escaping(Success) throws -> NewTask) -> Task { return andThen(upon: executor as Executor, start: startNextTask) } @@ -41,7 +41,7 @@ extension TaskProtocol { /// `startNextTask` closure. `andThen` submits `startNextTask` to `executor` /// once the task completes successfully. /// - see: FutureProtocol.andThen(upon:start:) - public func andThen(upon executor: Executor, start startNextTask: @escaping(SuccessValue) throws -> NewTask) -> Task { + public func andThen(upon executor: Executor, start startNextTask: @escaping(Success) throws -> NewTask) -> Task { #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) let chain = TaskChain(continuingWith: self) #else @@ -71,9 +71,9 @@ extension TaskProtocol { } #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) - return Task(future, progress: chain.effectiveProgress) + return Task(future, progress: chain.effectiveProgress) #else - return Task(future) { + return Task(future) { cancellationToken.fill(with: ()) } #endif diff --git a/Sources/Task/TaskAsync.swift b/Sources/Task/TaskAsync.swift index ba36bafe..71466dab 100755 --- a/Sources/Task/TaskAsync.swift +++ b/Sources/Task/TaskAsync.swift @@ -24,7 +24,7 @@ extension Task { /// preemptively fail the task. /// - parameter body: A function body that either calculates and returns the /// success value for the task or throws to indicate failure. - public static func async(upon queue: DispatchQueue = .any(), flags: DispatchWorkItemFlags = [], onCancel makeError: @autoclosure @escaping() -> Error, execute work: @escaping() throws -> SuccessValue) -> Task { + public static func async(upon queue: DispatchQueue = .any(), flags: DispatchWorkItemFlags = [], onCancel makeError: @autoclosure @escaping() -> Failure, execute work: @escaping() throws -> Success) -> Task { let deferred = Deferred() let semaphore = DispatchSemaphore(value: 1) @@ -44,7 +44,7 @@ extension Task { } @available(*, unavailable, message: "Replace with 'Task.async(upon:flags:onCancel:)' for clarity.") - public convenience init(upon queue: DispatchQueue = .any(), flags: DispatchWorkItemFlags = [], onCancel produceError: @autoclosure @escaping() -> Error, execute body: @escaping() throws -> SuccessValue) { + public convenience init(upon queue: DispatchQueue = .any(), flags: DispatchWorkItemFlags = [], onCancel produceError: @autoclosure @escaping() -> Error, execute body: @escaping() throws -> Success) { fatalError() } } diff --git a/Sources/Task/TaskChain.swift b/Sources/Task/TaskChain.swift index 05feda42..15e5abaa 100644 --- a/Sources/Task/TaskChain.swift +++ b/Sources/Task/TaskChain.swift @@ -108,7 +108,7 @@ struct TaskChain { /// Locates or creates the root of a task chain, then increments its /// total units in preparation for a follow-up operation to be performed. init(continuingWith wrapped: Wrapped) { - if let task = wrapped as? Task, let root = task.progress as? Root { + if let task = wrapped as? Task, let root = task.progress as? Root { // If `wrapped` is a Task created normally, reuse the progress root; // this `map` or `andThen` builds on that progress. self.root = root @@ -153,7 +153,7 @@ struct TaskChain { /// See `beginAndThen`. func commitAndThen(with wrapped: Wrapped) { - if let task = wrapped as? Task, !(task.progress is Root) { + if let task = wrapped as? Task, !(task.progress is Root) { let pendingUnitCount = task.progress.wasGeneratedByTask ? TaskChain.singleUnit : TaskChain.explicitChildUnitCount root.totalUnitCount += pendingUnitCount - TaskChain.singleUnit root.adoptChild(task.progress, withPendingUnitCount: pendingUnitCount) diff --git a/Sources/Task/TaskCollections.swift b/Sources/Task/TaskCollections.swift index 9e195181..76e266d7 100644 --- a/Sources/Task/TaskCollections.swift +++ b/Sources/Task/TaskCollections.swift @@ -14,16 +14,16 @@ import Dispatch import Foundation #endif -private struct AllFilled: TaskProtocol { +private struct AllFilled: TaskProtocol { let group = DispatchGroup() - let combined = Deferred.Result>() + let combined = Deferred.Result>() #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) let progress = Progress() #else let cancellations: [() -> Void] #endif - init(_ base: Base, mappingBy transform: @escaping([Base.Element]) -> SuccessValue) where Base.Element: TaskProtocol { + init(_ base: Base, mappingBy transform: @escaping([Base.Element]) -> Success) where Base.Element: TaskProtocol { let array = Array(base) let queue = DispatchQueue.global(qos: .utility) @@ -35,7 +35,7 @@ private struct AllFilled: TaskProtocol { for future in array { #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) - if let task = future as? Task { + if let task = future as? Task { progress.monitorChild(task.progress, withPendingUnitCount: 1) } else { progress.monitorCompletion(of: future, withPendingUnitCount: 1) @@ -59,15 +59,15 @@ private struct AllFilled: TaskProtocol { } } - func upon(_ executor: Executor, execute body: @escaping(Task.Result) -> Void) { + func upon(_ executor: Executor, execute body: @escaping(Task.Result) -> Void) { combined.upon(executor, execute: body) } - func peek() -> Task.Result? { + func peek() -> Task.Result? { return combined.peek() } - func wait(until time: DispatchTime) -> Task.Result? { + func wait(until time: DispatchTime) -> Task.Result? { return combined.wait(until: time) } @@ -86,12 +86,12 @@ extension Collection where Element: TaskProtocol { /// If any of the contained tasks fail, the returned task will be determined /// with that failure. Otherwise, once all operations succeed, the returned /// task will be fulfilled by combining the values. - public func allSucceeded() -> Task<[Element.SuccessValue]> { + public func allSucceeded() -> Task<[Element.Success]> { guard !isEmpty else { return Task(success: []) } - let wrapper = AllFilled(self) { (array) -> [Element.SuccessValue] in + let wrapper = AllFilled(self) { (array) -> [Element.Success] in // Expect each to be filled but not successful right now. // swiftlint:disable:next force_unwrapping return array.compactMap { try? $0.peek()!.get() } @@ -105,7 +105,7 @@ extension Collection where Element: TaskProtocol { } } -extension Collection where Element: TaskProtocol, Element.SuccessValue == Void { +extension Collection where Element: TaskProtocol, Element.Success == Void { /// Compose a number of tasks into a single array. /// /// If any of the contained tasks fail, the returned task will be determined diff --git a/Sources/Task/TaskFallback.swift b/Sources/Task/TaskFallback.swift index 442084ca..1b3649d7 100644 --- a/Sources/Task/TaskFallback.swift +++ b/Sources/Task/TaskFallback.swift @@ -21,7 +21,7 @@ extension TaskProtocol { /// /// Cancelling the resulting task will attempt to cancel both the receiving /// task and the created task. - public func fallback(upon executor: PreferredExecutor, to restartTask: @escaping(Error) throws -> NewTask) -> Task where NewTask.SuccessValue == SuccessValue { + public func fallback(upon executor: PreferredExecutor, to restartTask: @escaping(Failure) throws -> NewTask) -> Task where NewTask.Success == Success { return fallback(upon: executor as Executor, to: restartTask) } @@ -40,7 +40,7 @@ extension TaskProtocol { /// `restartTask` closure. `fallback` submits `restartTask` to `executor` /// once the task fails. /// - see: FutureProtocol.andThen(upon:start:) - public func fallback(upon executor: Executor, to restartTask: @escaping(Error) throws -> NewTask) -> Task where NewTask.SuccessValue == SuccessValue { + public func fallback(upon executor: Executor, to restartTask: @escaping(Failure) throws -> NewTask) -> Task where NewTask.Success == Success { #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) let chain = TaskChain(continuingWith: self) #else @@ -77,9 +77,9 @@ extension TaskProtocol { } #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) - return Task(future, progress: chain.effectiveProgress) + return Task(future, progress: chain.effectiveProgress) #else - return Task(future) { + return Task(future) { cancellationToken.fill(with: ()) } #endif @@ -90,9 +90,9 @@ extension TaskProtocol { public static func `repeat`( upon preferredExecutor: PreferredExecutor, count numberOfAttempts: Int = 3, - continuingIf shouldRetry: @escaping(Error) -> Bool = { _ in return true }, - to startTask: @escaping() throws -> Task - ) -> Task { + continuingIf shouldRetry: @escaping(Failure) -> Bool = { _ in return true }, + to startTask: @escaping() throws -> Task + ) -> Task { return self.repeat(upon: preferredExecutor as Executor, count: numberOfAttempts, continuingIf: shouldRetry, to: startTask) } @@ -112,10 +112,10 @@ extension TaskProtocol { public static func `repeat`( upon executor: Executor, count numberOfAttempts: Int = 3, - continuingIf shouldRetry: @escaping(Error) -> Bool = { _ in return true }, - to startTask: @escaping() throws -> Task - ) -> Task { - var lastFailedTask: Task + continuingIf shouldRetry: @escaping(Failure) -> Bool = { _ in return true }, + to startTask: @escaping() throws -> Task + ) -> Task { + var lastFailedTask: Task do { lastFailedTask = try startTask() } catch { @@ -123,7 +123,7 @@ extension TaskProtocol { } for _ in 0 ..< max(numberOfAttempts, 0) { - lastFailedTask = lastFailedTask.fallback(upon: executor) { (error) -> Task in + lastFailedTask = lastFailedTask.fallback(upon: executor) { (error) -> Task in guard shouldRetry(error) else { throw error } diff --git a/Sources/Task/TaskIgnore.swift b/Sources/Task/TaskIgnore.swift index e8bb8ff1..7616c7da 100644 --- a/Sources/Task/TaskIgnore.swift +++ b/Sources/Task/TaskIgnore.swift @@ -33,7 +33,7 @@ extension TaskProtocol { } #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) - if let progress = (self as? Task)?.progress { + if let progress = (self as? Task)?.progress { return Task(future, progress: progress) } #endif diff --git a/Sources/Task/TaskMap.swift b/Sources/Task/TaskMap.swift index 0e6e2c3b..e0f09a45 100644 --- a/Sources/Task/TaskMap.swift +++ b/Sources/Task/TaskMap.swift @@ -20,7 +20,7 @@ extension TaskProtocol { /// using the current parent will also contribute to the chain's progress. /// /// The resulting task is cancellable in the same way the receiving task is. - public func map(upon queue: PreferredExecutor, transform: @escaping(SuccessValue) throws -> NewSuccessValue) -> Task { + public func map(upon queue: PreferredExecutor, transform: @escaping(Success) throws -> NewSuccess) -> Task { return map(upon: queue as Executor, transform: transform) } @@ -37,26 +37,26 @@ extension TaskProtocol { /// The resulting task is cancellable in the same way the receiving task is. /// /// - see: FutureProtocol.map(upon:transform:) - public func map(upon executor: Executor, transform: @escaping(SuccessValue) throws -> NewSuccessValue) -> Task { + public func map(upon executor: Executor, transform: @escaping(Success) throws -> NewSuccess) -> Task { #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) let chain = TaskChain(continuingWith: self) #endif - let future: Future = map(upon: executor) { (result) -> Task.Result in + let future: Future = map(upon: executor) { (result) -> Task.Result in #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) chain.beginMap() defer { chain.commitMap() } #endif - return Task.Result { + return Task.Result { try transform(result.get()) } } #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) - return Task(future, progress: chain.effectiveProgress) + return Task(future, progress: chain.effectiveProgress) #else - return Task(future, uponCancel: cancel) + return Task(future, uponCancel: cancel) #endif } } diff --git a/Sources/Task/TaskPromise.swift b/Sources/Task/TaskPromise.swift index 813a56d1..bb9b7bf4 100644 --- a/Sources/Task/TaskPromise.swift +++ b/Sources/Task/TaskPromise.swift @@ -20,7 +20,7 @@ extension TaskProtocol where Self: PromiseProtocol { /// /// - seealso: `PromiseProtocol.fill(with:)` @discardableResult - public func succeed(with value: @autoclosure() throws -> SuccessValue) -> Bool { + public func succeed(with value: @autoclosure() throws -> Success) -> Bool { return fill(with: Value(catching: value)) } @@ -28,12 +28,12 @@ extension TaskProtocol where Self: PromiseProtocol { /// /// - see: fill(with:) @discardableResult - public func fail(with error: FailureValue) -> Bool { + public func fail(with error: Failure) -> Bool { return fill(with: Value(left: error)) } } -extension TaskProtocol where Self: PromiseProtocol, SuccessValue == Void { +extension TaskProtocol where Self: PromiseProtocol, Success == Void { /// Completes the task with a success. /// /// Fulfilling this deferred value should usually be attempted only once. diff --git a/Sources/Task/TaskRecovery.swift b/Sources/Task/TaskRecovery.swift index 80d91260..485dd878 100644 --- a/Sources/Task/TaskRecovery.swift +++ b/Sources/Task/TaskRecovery.swift @@ -20,7 +20,7 @@ extension TaskProtocol { /// using the current parent will also contribute to the chain's progress. /// /// The resulting task is cancellable in the same way the receiving task is. - public func recover(upon executor: PreferredExecutor, substituting substitution: @escaping(Error) throws -> SuccessValue) -> Task { + public func recover(upon executor: PreferredExecutor, substituting substitution: @escaping(Failure) throws -> Success) -> Task { return recover(upon: executor as Executor, substituting: substitution) } @@ -38,18 +38,18 @@ extension TaskProtocol { /// The resulting task is cancellable in the same way the receiving task is. /// /// - see: FutureProtocol.map(upon:transform:) - public func recover(upon executor: Executor, substituting substitution: @escaping(Error) throws -> SuccessValue) -> Task { + public func recover(upon executor: Executor, substituting substitution: @escaping(Failure) throws -> Success) -> Task { #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) let chain = TaskChain(continuingWith: self) #endif - let future: Future = map(upon: executor) { (result) -> Task.Result in + let future: Future = map(upon: executor) { (result) -> Task.Result in #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) chain.beginMap() defer { chain.commitMap() } #endif - return Task.Result { + return Task.Result { do { return try result.get() } catch { @@ -59,9 +59,9 @@ extension TaskProtocol { } #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) - return Task(future, progress: chain.effectiveProgress) + return Task(future, progress: chain.effectiveProgress) #else - return Task(future, uponCancel: cancel) + return Task(future, uponCancel: cancel) #endif } } diff --git a/Sources/Task/TaskResult.swift b/Sources/Task/TaskResult.swift index 39c6fe5d..2a94c69b 100644 --- a/Sources/Task/TaskResult.swift +++ b/Sources/Task/TaskResult.swift @@ -10,7 +10,7 @@ extension Task.Result { @_inlineable - public func get() throws -> SuccessValue { + public func get() throws -> Success { switch self { case let .success(success): return success @@ -27,7 +27,7 @@ extension Task.Result { /// value as the parameter, to derive a new value. /// /// Use the `map` method with a closure that produces a new value. - public func map(_ transform: (SuccessValue) -> NewValue) -> Task.Result { + public func map(_ transform: (Success) -> NewSuccess) -> Task.Result { switch self { case .success(let value): return .success(transform(value)) @@ -40,7 +40,7 @@ extension Task.Result { /// error as the parameter, to derive a new value. /// /// Use the `mapError` method with a closure that produces a new value. - public func mapError(_ transform: (Error) -> Error) -> Task.Result { + public func mapError(_ transform: (Failure) -> Error) -> Task.Result { switch self { case .success(let success): return .success(success) @@ -53,7 +53,7 @@ extension Task.Result { /// value as the parameter, to derive a new result. /// /// Use `flatMap` with a closure that itself returns a result. - public func flatMap(_ transform: (SuccessValue) -> Task.Result) -> Task.Result { + public func flatMap(_ transform: (Success) -> Task.Result) -> Task.Result { switch self { case .success(let value): return transform(value) @@ -66,7 +66,7 @@ extension Task.Result { /// error as the parameter, to derive a new result. /// /// Use the `flatMapError` with a closure that itself returns a result. - public func flatMapError(_ transform: (Error) -> Task.Result) -> Task.Result { + public func flatMapError(_ transform: (Failure) -> Task.Result) -> Task.Result { switch self { case let .success(success): return .success(success) @@ -81,19 +81,19 @@ extension Task.Result { extension Task.Result { /// Creates an instance storing a successful `value`. @_inlineable - public init(success value: @autoclosure() throws -> SuccessValue) { + public init(success value: @autoclosure() throws -> Success) { self.init(catching: value) } /// Creates an instance storing an `error` describing the failure. @_inlineable - public init(failure error: Error) { + public init(failure error: Failure) { self = .failure(error) } /// Create an exclusive success/failure state derived from two optionals, /// in the style of Cocoa completion handlers. - public init(value: SuccessValue?, error: Error?) { + public init(value: Success?, error: Failure?) { switch (value, error) { case (let value?, _): // Ignore error if value is non-nil @@ -110,7 +110,7 @@ private enum TaskResultInitializerError: Error { case invalidInput } -extension Task.Result where SuccessValue == Void { +extension Task.Result where Success == Void { /// Creates the success value. @_inlineable public init() { @@ -122,12 +122,12 @@ extension Task.Result where SuccessValue == Void { extension Task.Result: Either { @_inlineable - public init(left error: Error) { + public init(left error: Failure) { self = .failure(error) } @_inlineable - public init(right value: SuccessValue) { + public init(right value: Success) { self = .success(value) } } diff --git a/Sources/Task/TaskUpon.swift b/Sources/Task/TaskUpon.swift index 342a03fb..de93a871 100644 --- a/Sources/Task/TaskUpon.swift +++ b/Sources/Task/TaskUpon.swift @@ -15,11 +15,11 @@ extension TaskProtocol { /// /// - seealso: `TaskProtocol.uponSuccess(on:execute:)` /// - see: `FutureProtocol.upon(_:execute:)` - public func uponSuccess(on executor: PreferredExecutor = Self.defaultUponExecutor, execute body: @escaping(_ value: SuccessValue) -> Void) { + public func uponSuccess(on executor: PreferredExecutor = Self.defaultUponExecutor, execute body: @escaping(_ value: Success) -> Void) { uponSuccess(on: executor as Executor, execute: body) } - public func uponSuccess(on executor: Executor, execute body: @escaping(_ value: SuccessValue) -> Void) { + public func uponSuccess(on executor: Executor, execute body: @escaping(_ value: Success) -> Void) { upon(executor) { (result) in do { try body(result.get()) @@ -31,11 +31,11 @@ extension TaskProtocol { /// /// - seealso: `TaskProtocol.uponFailure(on:execute:)` /// - seealso: `FutureProtocol.upon(_:execute:)` - public func uponFailure(on executor: PreferredExecutor = Self.defaultUponExecutor, execute body: @escaping(_ error: FailureValue) -> Void) { + public func uponFailure(on executor: PreferredExecutor = Self.defaultUponExecutor, execute body: @escaping(_ error: Failure) -> Void) { uponFailure(on: executor as Executor, execute: body) } - public func uponFailure(on executor: Executor, execute body: @escaping(_ error: FailureValue) -> Void) { + public func uponFailure(on executor: Executor, execute body: @escaping(_ error: Failure) -> Void) { upon(executor) { result in do { _ = try result.get()