2
2
//
3
3
// This source file is part of the Swift.org open source project
4
4
//
5
- // Copyright (c) 2018 Apple Inc. and the Swift project authors
5
+ // Copyright (c) 2018 - 2024 Apple Inc. and the Swift project authors
6
6
// Licensed under Apache License v2.0 with Runtime Library Exception
7
7
//
8
8
// See https://swift.org/LICENSE.txt for license information
13
13
/// A value that represents either a success or a failure, including an
14
14
/// associated value in each case.
15
15
@frozen
16
- public enum Result < Success, Failure: Error > {
16
+ public enum Result < Success: ~ Copyable , Failure: Error > {
17
17
/// A success, storing a `Success` value.
18
18
case success( Success )
19
-
19
+
20
20
/// A failure, storing a `Failure` value.
21
21
case failure( Failure )
22
-
22
+ }
23
+
24
+ extension Result : Copyable where Success: Copyable { }
25
+
26
+ extension Result : Sendable where Success: Sendable & ~ Copyable { }
27
+
28
+ extension Result : Equatable where Success: Equatable , Failure: Equatable { }
29
+
30
+ extension Result : Hashable where Success: Hashable , Failure: Hashable { }
31
+
32
+ extension Result {
23
33
/// Returns a new result, mapping any success value using the given
24
34
/// transformation.
25
35
///
@@ -39,7 +49,8 @@ public enum Result<Success, Failure: Error> {
39
49
/// - Returns: A `Result` instance with the result of evaluating `transform`
40
50
/// as the new success value if this instance represents a success.
41
51
@inlinable
42
- public func map< NewSuccess> (
52
+ @_preInverseGenerics
53
+ public func map< NewSuccess: ~ Copyable> (
43
54
_ transform: ( Success ) -> NewSuccess
44
55
) -> Result < NewSuccess , Failure > {
45
56
switch self {
@@ -49,7 +60,37 @@ public enum Result<Success, Failure: Error> {
49
60
return . failure( failure)
50
61
}
51
62
}
52
-
63
+ }
64
+
65
+ extension Result where Success: ~ Copyable {
66
+ @_alwaysEmitIntoClient
67
+ public consuming func consumingMap< NewSuccess : ~ Copyable> (
68
+ _ transform: ( consuming Success ) -> NewSuccess
69
+ ) -> Result < NewSuccess , Failure > {
70
+ switch consume self {
71
+ case let . success ( success) :
72
+ return . success ( transform ( consume success) )
73
+ case let . failure ( failure) :
74
+ return . failure ( consume failure)
75
+ }
76
+ }
77
+
78
+ #if $BorrowingSwitch
79
+ @_alwaysEmitIntoClient
80
+ public borrowing func borrowingMap< NewSuccess: ~ Copyable> (
81
+ _ transform: ( borrowing Success ) -> NewSuccess
82
+ ) -> Result < NewSuccess , Failure > {
83
+ switch self {
84
+ case . success( _borrowing success) :
85
+ return . success( transform ( success) )
86
+ case let . failure( failure) :
87
+ return . failure( failure)
88
+ }
89
+ }
90
+ #endif
91
+ }
92
+
93
+ extension Result where Success: ~ Copyable {
53
94
/// Returns a new result, mapping any failure value using the given
54
95
/// transformation.
55
96
///
@@ -76,8 +117,24 @@ public enum Result<Success, Failure: Error> {
76
117
/// instance.
77
118
/// - Returns: A `Result` instance with the result of evaluating `transform`
78
119
/// as the new failure value if this instance represents a failure.
79
- @inlinable
80
- public func mapError< NewFailure> (
120
+ @_alwaysEmitIntoClient
121
+ public consuming func mapError< NewFailure> (
122
+ _ transform: ( Failure ) -> NewFailure
123
+ ) -> Result < Success , NewFailure > {
124
+ switch consume self {
125
+ case let . success( success) :
126
+ return . success( consume success)
127
+ case let . failure( failure) :
128
+ return . failure( transform ( failure) )
129
+ }
130
+ }
131
+ }
132
+
133
+ extension Result {
134
+ // TODO: Merge this back into the noncopyable variant once we have @_preInverseGenerics
135
+ @_spi ( SwiftStdlibLegacyABI) @available ( swift, obsoleted: 2 )
136
+ @usableFromInline
137
+ internal func mapError< NewFailure> (
81
138
_ transform: ( Failure ) -> NewFailure
82
139
) -> Result < Success , NewFailure > {
83
140
switch self {
@@ -87,7 +144,9 @@ public enum Result<Success, Failure: Error> {
87
144
return . failure( transform ( failure) )
88
145
}
89
146
}
90
-
147
+ }
148
+
149
+ extension Result {
91
150
/// Returns a new result, mapping any success value using the given
92
151
/// transformation and unwrapping the produced result.
93
152
///
@@ -115,7 +174,8 @@ public enum Result<Success, Failure: Error> {
115
174
/// - Returns: A `Result` instance, either from the closure or the previous
116
175
/// `.failure`.
117
176
@inlinable
118
- public func flatMap< NewSuccess> (
177
+ @_preInverseGenerics
178
+ public func flatMap< NewSuccess: ~ Copyable> (
119
179
_ transform: ( Success ) -> Result < NewSuccess , Failure >
120
180
) -> Result < NewSuccess , Failure > {
121
181
switch self {
@@ -125,13 +185,43 @@ public enum Result<Success, Failure: Error> {
125
185
return . failure( failure)
126
186
}
127
187
}
128
-
188
+ }
189
+
190
+ extension Result where Success: ~ Copyable {
191
+ @_alwaysEmitIntoClient
192
+ public consuming func consumingFlatMap< NewSuccess : ~ Copyable> (
193
+ _ transform: ( consuming Success ) -> Result < NewSuccess , Failure >
194
+ ) -> Result < NewSuccess , Failure > {
195
+ switch consume self {
196
+ case let . success ( success) :
197
+ return transform( consume success)
198
+ case let . failure ( failure) :
199
+ return . failure ( failure )
200
+ }
201
+ }
202
+
203
+ #if $BorrowingSwitch
204
+ @_alwaysEmitIntoClient
205
+ public borrowing func borrowingFlatMap< NewSuccess: ~ Copyable> (
206
+ _ transform: ( borrowing Success ) -> Result < NewSuccess , Failure >
207
+ ) -> Result < NewSuccess , Failure > {
208
+ switch self {
209
+ case . success( _borrowing success) :
210
+ return transform ( success)
211
+ case let . failure( failure) :
212
+ return . failure( failure)
213
+ }
214
+ }
215
+ #endif
216
+ }
217
+
218
+ extension Result {
129
219
/// Returns a new result, mapping any failure value using the given
130
220
/// transformation and unwrapping the produced result.
131
221
///
132
222
/// - Parameter transform: A closure that takes the failure value of the
133
223
/// instance.
134
- /// - Returns: A `Result` instance, either from the closure or the previous
224
+ /// - Returns: A `Result` instance, either from the closure or the previous
135
225
/// `.success`.
136
226
@inlinable
137
227
public func flatMapError< NewFailure> (
@@ -144,7 +234,9 @@ public enum Result<Success, Failure: Error> {
144
234
return transform ( failure)
145
235
}
146
236
}
147
-
237
+ }
238
+
239
+ extension Result where Success: ~ Copyable {
148
240
/// Returns the success value as a throwing expression.
149
241
///
150
242
/// Use this method to retrieve the value of this result if it represents a
@@ -162,9 +254,8 @@ public enum Result<Success, Failure: Error> {
162
254
/// - Returns: The success value, if the instance represents a success.
163
255
/// - Throws: The failure value, if the instance represents a failure.
164
256
@_alwaysEmitIntoClient
165
- @inlinable
166
- public func get( ) throws ( Failure) -> Success {
167
- switch self {
257
+ public consuming func get( ) throws ( Failure) -> Success {
258
+ switch consume self {
168
259
case let . success( success) :
169
260
return success
170
261
case let . failure( failure) :
@@ -173,13 +264,12 @@ public enum Result<Success, Failure: Error> {
173
264
}
174
265
}
175
266
176
- extension Result {
267
+ extension Result where Success : ~ Copyable {
177
268
/// Creates a new result by evaluating a throwing closure, capturing the
178
269
/// returned value as a success, or any thrown error as a failure.
179
270
///
180
271
/// - Parameter body: A potentially throwing closure to evaluate.
181
272
@_alwaysEmitIntoClient
182
- @inlinable
183
273
public init ( catching body: ( ) throws ( Failure ) -> Success ) {
184
274
do {
185
275
self = . success( try body ( ) )
@@ -191,6 +281,7 @@ extension Result {
191
281
192
282
extension Result {
193
283
/// ABI: Historical get() throws
284
+ @_spi ( SwiftStdlibLegacyABI) @available ( swift, obsoleted: 2 )
194
285
@_silgen_name ( " $ss6ResultO3getxyKF " )
195
286
@usableFromInline
196
287
func __abi_get( ) throws -> Success {
@@ -206,6 +297,7 @@ extension Result {
206
297
207
298
extension Result where Failure == Swift . Error {
208
299
/// ABI: Historical init(catching:)
300
+ @_spi ( SwiftStdlibLegacyABI) @available ( swift, obsoleted: 2 )
209
301
@_silgen_name ( " $ss6ResultOss5Error_pRs_rlE8catchingAByxsAC_pGxyKXE_tcfC " )
210
302
@usableFromInline
211
303
init ( __abi_catching body: ( ) throws ( Failure ) -> Success ) {
@@ -216,9 +308,3 @@ extension Result where Failure == Swift.Error {
216
308
}
217
309
}
218
310
}
219
-
220
- extension Result : Equatable where Success: Equatable , Failure: Equatable { }
221
-
222
- extension Result : Hashable where Success: Hashable , Failure: Hashable { }
223
-
224
- extension Result : Sendable where Success: Sendable { }
0 commit comments