-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fallible<T> initialization mini-rework (#144).
* Rework some Fallible<T> iniitializers (#125) (#128). * Write section about new Fallible<T> initializers. * Some documentation comments. * Cleanup of README.md.
- Loading branch information
Showing
9 changed files
with
241 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
//=----------------------------------------------------------------------------= | ||
// This source file is part of the Ultimathnum open source project. | ||
// | ||
// Copyright (c) 2023 Oscar Byström Ericsson | ||
// Licensed under Apache License, Version 2.0 | ||
// | ||
// See http://www.apache.org/licenses/LICENSE-2.0 for license information. | ||
//=----------------------------------------------------------------------------= | ||
|
||
//*============================================================================* | ||
// MARK: * Fallible x Setup | ||
//*============================================================================* | ||
|
||
extension Fallible { | ||
|
||
//=------------------------------------------------------------------------= | ||
// MARK: Initializers | ||
//=------------------------------------------------------------------------= | ||
|
||
/// Creates a new instance by combining the mutable `value` and `error` | ||
/// indicator passed to the `setup` function by consuming them when the | ||
/// the `setup` function returns. | ||
/// | ||
/// ```swift | ||
/// let x = Fallible(U8.zero) { value, error in | ||
/// value = value.decremented().sink(&error) | ||
/// } // value: 255, error: true | ||
/// ``` | ||
/// | ||
/// - Note: The default `error` indicator is `false`. | ||
/// | ||
@inlinable public init<Error>( | ||
_ | ||
value: consuming Value, | ||
error: consuming Bool = false, | ||
setup: (inout Value, inout Bool) throws(Error) -> Void | ||
) throws(Error) { | ||
|
||
try setup(&value, &error) | ||
self.init(value, error: error) | ||
} | ||
|
||
//=------------------------------------------------------------------------= | ||
// MARK: Initializers x Error | ||
//=------------------------------------------------------------------------= | ||
|
||
/// Creates a new instance by combining the mutable `error` indicator passed | ||
/// to the `setup` function and the `value` that the `setup` function returns. | ||
/// | ||
/// ```swift | ||
/// let x = Fallible.error { error in | ||
/// U8.zero.decremented().sink(&error) | ||
/// } // value: 255, error: true | ||
/// ``` | ||
/// | ||
/// - Note: The default `error` indicator is `false`. | ||
/// | ||
@inlinable public static func error<Error>( | ||
_ setup: (inout Bool) throws(Error) -> Value | ||
) throws(Error) -> Self { | ||
|
||
try Self.error(false, setup: setup) | ||
} | ||
|
||
/// Creates a new instance by combining the mutable `error` indicator passed | ||
/// to the `setup` function and the `value` that the `setup` function returns. | ||
/// | ||
/// ```swift | ||
/// let x = Fallible.error { error in | ||
/// U8.zero.decremented().sink(&error) | ||
/// } // value: 255, error: true | ||
/// ``` | ||
/// | ||
/// - Note: The default `error` indicator is `false`. | ||
/// | ||
@inlinable public static func error<Error>( | ||
_ error: consuming Bool, setup: (inout Bool) throws(Error) -> Value | ||
) throws(Error) -> Self { | ||
|
||
let value = try setup(&error) | ||
return Self(value, error: error) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
//=----------------------------------------------------------------------------= | ||
// This source file is part of the Ultimathnum open source project. | ||
// | ||
// Copyright (c) 2023 Oscar Byström Ericsson | ||
// Licensed under Apache License, Version 2.0 | ||
// | ||
// See http://www.apache.org/licenses/LICENSE-2.0 for license information. | ||
//=----------------------------------------------------------------------------= | ||
|
||
import CoreKit | ||
import TestKit | ||
|
||
//*============================================================================* | ||
// MARK: * Fallible x Setup | ||
//*============================================================================* | ||
|
||
@Suite struct FallibleTestsOnSetup { | ||
|
||
//=------------------------------------------------------------------------= | ||
// MARK: Tests | ||
//=------------------------------------------------------------------------= | ||
|
||
@Test( | ||
"Fallible/setup: Self.init(_:setup:)", | ||
Tag.List.tags(.exhaustive), | ||
arguments: Bit.all, Bool.all | ||
) func fromInoutValueAndInoutError(value: Bit, error: Bool) throws { | ||
#expect(Fallible(value, setup: { _, _ in }) == Fallible(value, error: false)) | ||
#expect(Fallible(value, setup: { $0 = Bit.zero; $1 = false }) == Fallible(Bit.zero, error: false)) | ||
#expect(Fallible(value, setup: { $0 = Bit.zero; $1 = true }) == Fallible(Bit.zero, error: true )) | ||
#expect(Fallible(value, setup: { $0 = Bit.one; $1 = false }) == Fallible(Bit.one, error: false)) | ||
#expect(Fallible(value, setup: { $0 = Bit.one; $1 = true }) == Fallible(Bit.one, error: true )) | ||
} | ||
|
||
@Test( | ||
"Fallible/setup: Self.init(_:error:setup:)", | ||
Tag.List.tags(.exhaustive), | ||
arguments: Bit.all, Bool.all | ||
) func fromInoutValueAndInoutErrorWithCustomInitialError(value: Bit, error: Bool) throws { | ||
#expect(Fallible(value, error: error, setup: { _, _ in }) == Fallible(value, error: error)) | ||
#expect(Fallible(value, error: error, setup: { $0 = Bit.zero; $1 = false }) == Fallible(Bit.zero, error: false)) | ||
#expect(Fallible(value, error: error, setup: { $0 = Bit.zero; $1 = true }) == Fallible(Bit.zero, error: true )) | ||
#expect(Fallible(value, error: error, setup: { $0 = Bit.one; $1 = false }) == Fallible(Bit.one, error: false)) | ||
#expect(Fallible(value, error: error, setup: { $0 = Bit.one; $1 = true }) == Fallible(Bit.one, error: true )) | ||
} | ||
|
||
//=------------------------------------------------------------------------= | ||
// MARK: Tests x Error | ||
//=------------------------------------------------------------------------= | ||
|
||
@Test( | ||
"Fallible/setup: Self.error(_:)", | ||
Tag.List.tags(.exhaustive), | ||
arguments: Bit.all | ||
) func fromInoutErrorReturningValue(value: Bit) throws { | ||
#expect(Fallible.error({ _ in return value }) == Fallible(value, error: false)) | ||
#expect(Fallible.error({ $0 = false; return value }) == Fallible(value, error: false)) | ||
#expect(Fallible.error({ $0 = true; return value }) == Fallible(value, error: true )) | ||
} | ||
|
||
@Test( | ||
"Fallible/setup: Self.error(_:setup:)", | ||
Tag.List.tags(.exhaustive), | ||
arguments: Bit.all, Bool.all | ||
) func fromInoutErrorReturningValueWithCustomInitialError(value: Bit, error: Bool) throws { | ||
#expect(Fallible.error(error, setup: { _ in return value }) == Fallible(value, error: error)) | ||
#expect(Fallible.error(error, setup: { $0 = false; return value }) == Fallible(value, error: false)) | ||
#expect(Fallible.error(error, setup: { $0 = true; return value }) == Fallible(value, error: true )) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.