Skip to content

Commit

Permalink
Further minor tweaks to result<>'s failure type
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Parpart <christian@parpart.family>
  • Loading branch information
christianparpart committed Mar 29, 2024
1 parent 881f765 commit f18103b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/crispy/result.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class failure
constexpr failure(const failure&) = default;
constexpr failure(failure&&) noexcept = default;

template <typename Err = E>
template <typename Err = E, typename = std::enable_if_t<std::is_constructible_v<E, Err&&>>>
constexpr explicit failure(Err&& e): _value { std::forward<Err>(e) }
{
}
Expand Down
14 changes: 14 additions & 0 deletions src/crispy/result_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,20 @@ TEST_CASE("result.or_else")
// test const lvalue reference
auto const c = b.or_else([](error_code e) -> result { return failure { error_code(int(e) + 1) }; });
REQUIRE(c.error() == error_code::E3);

// ensure chaining works over rvalues
auto const b2 = result { failure { error_code::E1 } } // E1
.or_else([](error_code) { return result { 12 }; })
.or_else([](error_code) { return result { 13 }; });
REQUIRE(b2.value() == 12);

// ensure chaining works over lvalues
auto const someError = result { failure { error_code::E1 } };
auto const c2 = someError // E1
.or_else([](error_code) { return result { 14 }; })
.or_else([](error_code) { return result { 15 }; })
.or_else([](error_code) { return result { 16 }; });
REQUIRE(c2.value() == 14);
}

TEST_CASE("result.emplace")
Expand Down

0 comments on commit f18103b

Please sign in to comment.