11/*
22 This source file is part of the Swift.org open source project
33
4- Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
4+ Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
55 Licensed under Apache License v2.0 with Runtime Library Exception
66
77 See http://swift.org/LICENSE.txt for license information
88 See http://swift.org/CONTRIBUTORS.txt for Swift project authors
99*/
1010
11- /// An simple enum which is either a value or an error.
12- /// It can be used for error handling in situations where try catch is
13- /// problematic to use, for eg: asynchronous APIs.
14- public enum Result < Value, ErrorType: Swift . Error > {
15- /// Indicates success with value in the associated object.
16- case success( Value )
17-
18- /// Indicates failure with error inside the associated object.
19- case failure( ErrorType )
20-
21- /// Initialiser for value.
22- public init ( _ value: Value ) {
23- self = . success( value)
24- }
25-
26- /// Initialiser for error.
27- public init ( _ error: ErrorType ) {
28- self = . failure( error)
29- }
30-
31- /// Initialise with something that can throw ErrorType.
32- public init ( _ body: ( ) throws -> Value ) throws {
33- do {
34- self = . success( try body ( ) )
35- } catch let error as ErrorType {
36- self = . failure( error)
37- }
38- }
39-
40- /// Get the value if success else throw the saved error.
41- public func dematerialize( ) throws -> Value {
42- switch self {
43- case . success( let value) :
44- return value
45- case . failure( let error) :
46- throw error
47- }
48- }
49-
50- /// Evaluates the given closure when this Result instance has a value.
51- public func map< U> ( _ transform: ( Value ) throws -> U ) rethrows -> Result < U , ErrorType > {
52- switch self {
53- case . success( let value) :
54- return Result < U , ErrorType > ( try transform ( value) )
55- case . failure( let error) :
56- return Result < U , ErrorType > ( error)
57- }
58- }
59-
60- /// Evaluates the given closure when this Result instance has a value, passing the unwrapped value as a parameter.
61- ///
62- /// The closure returns a Result instance itself which can have value or not.
63- public func flatMap< U> ( _ transform: ( Value ) -> Result < U , ErrorType > ) -> Result < U , ErrorType > {
64- switch self {
65- case . success( let value) :
66- return transform ( value)
67- case . failure( let error) :
68- return Result < U , ErrorType > ( error)
69- }
70- }
71-
72- }
73-
74- extension Result : CustomStringConvertible {
75- public var description : String {
76- switch self {
77- case . success( let value) :
78- return " Result( \( value) ) "
79- case . failure( let error) :
80- return " Result( \( error) ) "
81- }
82- }
83- }
84-
8511/// A type erased error enum.
8612public struct AnyError : Swift . Error , CustomStringConvertible {
8713 /// The underlying error.
@@ -114,9 +40,9 @@ public struct StringError: Equatable, Codable, CustomStringConvertible, Error {
11440}
11541
11642// AnyError specific helpers.
117- extension Result where ErrorType == AnyError {
43+ extension Result where Failure == AnyError {
11844 /// Initialise with something that throws AnyError.
119- public init ( anyError body: ( ) throws -> Value ) {
45+ public init ( anyError body: ( ) throws -> Success ) {
12046 do {
12147 self = . success( try body ( ) )
12248 } catch {
@@ -132,11 +58,11 @@ extension Result where ErrorType == AnyError {
13258 /// Evaluates the given throwing closure when this Result instance has a value.
13359 ///
13460 /// The final result will either be the transformed value or any error thrown by the closure.
135- public func mapAny< U> ( _ transform: ( Value ) throws -> U ) -> Result < U , AnyError > {
61+ public func mapAny< U> ( _ transform: ( Success ) throws -> U ) -> Result < U , AnyError > {
13662 switch self {
13763 case . success( let value) :
13864 do {
139- return Result < U , AnyError > ( try transform ( value) )
65+ return Result < U , AnyError > . success ( try transform ( value) )
14066 } catch {
14167 return Result < U , AnyError > ( error)
14268 }
@@ -146,11 +72,11 @@ extension Result where ErrorType == AnyError {
14672 }
14773}
14874
149- extension Result where ErrorType == StringError {
75+ extension Result where Failure == StringError {
15076 /// Create an instance of Result<Value, StringError>.
15177 ///
15278 /// Errors will be encoded as StringError using their description.
153- public init ( string body: ( ) throws -> Value ) {
79+ public init ( string body: ( ) throws -> Success ) {
15480 do {
15581 self = . success( try body ( ) )
15682 } catch let error as StringError {
@@ -160,40 +86,3 @@ extension Result where ErrorType == StringError {
16086 }
16187 }
16288}
163-
164- extension Result : Equatable where Value: Equatable , ErrorType: Equatable { }
165-
166- extension Result : Codable where Value: Codable , ErrorType: Codable {
167- private enum CodingKeys : String , CodingKey {
168- case success, failure
169- }
170-
171- public func encode( to encoder: Encoder ) throws {
172- var container = encoder. container ( keyedBy: CodingKeys . self)
173- switch self {
174- case . success( let value) :
175- var unkeyedContainer = container. nestedUnkeyedContainer ( forKey: . success)
176- try unkeyedContainer. encode ( value)
177- case . failure( let error) :
178- var unkeyedContainer = container. nestedUnkeyedContainer ( forKey: . failure)
179- try unkeyedContainer. encode ( error)
180- }
181- }
182-
183- public init ( from decoder: Decoder ) throws {
184- let values = try decoder. container ( keyedBy: CodingKeys . self)
185- guard let key = values. allKeys. first ( where: values. contains) else {
186- throw DecodingError . dataCorrupted ( . init( codingPath: decoder. codingPath, debugDescription: " Did not find a matching key " ) )
187- }
188- switch key {
189- case . success:
190- var unkeyedValues = try values. nestedUnkeyedContainer ( forKey: key)
191- let value = try unkeyedValues. decode ( Value . self)
192- self = . success( value)
193- case . failure:
194- var unkeyedValues = try values. nestedUnkeyedContainer ( forKey: key)
195- let error = try unkeyedValues. decode ( ErrorType . self)
196- self = . failure( error)
197- }
198- }
199- }
0 commit comments