converted *Result types to new Swift.Result

This commit is contained in:
John Estropia
2019-03-30 23:58:26 +09:00
parent bf8a1062e0
commit 0ab52d2f43
9 changed files with 100 additions and 316 deletions

View File

@@ -31,21 +31,8 @@ import CoreData
/**
The `SetupResult` indicates the result of an asynchronous initialization of a persistent store.
The `SetupResult` can be treated as a boolean:
```
try! CoreStore.addStorage(
SQLiteStore(),
completion: { (result: SetupResult) -> Void in
if result {
// succeeded
}
else {
// failed
}
}
)
```
or as an `enum`, where the resulting associated object can also be inspected:
`SetupResult.success` indicates that the storage setup succeeded. The associated object for this `enum` value is the related `StorageInterface` instance.
`SetupResult.failure` indicates that the storage setup failed. The associated object for this value is the related `CoreStoreError` enum value.
```
try! CoreStore.addStorage(
SQLiteStore(),
@@ -60,81 +47,4 @@ import CoreData
)
```
*/
public enum SetupResult<T: StorageInterface>: Hashable {
/**
`SetupResult.success` indicates that the storage setup succeeded. The associated object for this `enum` value is the related `StorageInterface` instance.
*/
case success(T)
/**
`SetupResult.failure` indicates that the storage setup failed. The associated object for this value is the related `CoreStoreError` enum value.
*/
case failure(CoreStoreError)
/**
Returns `true` if the result indicates `.success`, `false` if the result is `.failure`.
*/
public var isSuccess: Bool {
switch self {
case .success: return true
case .failure: return false
}
}
// MARK: Equatable
public static func == <T, U>(lhs: SetupResult<T>, rhs: SetupResult<U>) -> Bool {
switch (lhs, rhs) {
case (.success(let storage1), .success(let storage2)):
return storage1 === storage2
case (.failure(let error1), .failure(let error2)):
return error1 == error2
default:
return false
}
}
// MARK: Hashable
public func hash(into hasher: inout Hasher) {
switch self {
case .success(let storage):
hasher.combine(true)
hasher.combine(ObjectIdentifier(storage))
case .failure(let error):
hasher.combine(false)
hasher.combine(error)
}
}
// MARK: Internal
internal init(_ storage: T) {
self = .success(storage)
}
internal init(_ error: CoreStoreError) {
self = .failure(error)
}
internal init(_ error: Error) {
self = .failure(CoreStoreError(error))
}
}
public typealias SetupResult<StorageInterfaceType> = Swift.Result<StorageInterfaceType, CoreStoreError> where StorageInterfaceType: StorageInterface