Skip to content

Commit

Permalink
change or else behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
altalk23 committed Dec 6, 2024
1 parent b006578 commit 8363d66
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.21 FATAL_ERROR)
cmake_policy(SET CMP0097 NEW)

project(GeodeResult VERSION 1.3.1 LANGUAGES C CXX)
project(GeodeResult VERSION 1.3.2 LANGUAGES C CXX)

add_library(GeodeResult INTERFACE)

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ Here are the convenience utils for setting a value inline with manually handling
int main() {
// Enters the trailing block if the result is an error,
// otherwise sets the value into the variable
GEODE_UNWRAP_OR_ELSE(p4, integerDivision(3, 2)) {
GEODE_UNWRAP_OR_ELSE(p4, err, integerDivision(3, 2)) {
return -1;
}
assert(p4 == 1);

GEODE_UNWRAP_OR_ELSE(p5, integerDivision(3, 0)) {
GEODE_UNWRAP_OR_ELSE(p5, err, integerDivision(3, 0)) {
p5 = -1;
}
assert(p5 == -1);
Expand Down
12 changes: 7 additions & 5 deletions include/Geode/Result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@
#endif

#if !defined(GEODE_UNWRAP_OR_ELSE)
#define GEODE_UNWRAP_OR_ELSE(variable, ...) \
geode::impl::ResultOkType<std::remove_cvref_t<decltype(__VA_ARGS__)>> variable; \
auto GEODE_CONCAT(res, __LINE__) = __VA_ARGS__; \
if (GEODE_CONCAT(res, __LINE__).isOk()) \
variable = std::move(GEODE_CONCAT(res, __LINE__)).unwrap(); \
#define GEODE_UNWRAP_OR_ELSE(okVariable, errVariable, ...) \
geode::impl::ResultOkType<std::remove_cvref_t<decltype(__VA_ARGS__)>> okVariable; \
auto GEODE_CONCAT(res, __LINE__) = __VA_ARGS__; \
if (geode::impl::ResultErrType<std::remove_cvref_t<decltype(__VA_ARGS__)>> errVariable; \
GEODE_CONCAT(res, __LINE__).isOk() || \
(errVariable = std::move(GEODE_CONCAT(res, __LINE__)).unwrapErr(), false)) \
okVariable = std::move(GEODE_CONCAT(res, __LINE__)).unwrap(); \
else
#endif

Expand Down
6 changes: 5 additions & 1 deletion test/Misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,14 @@ TEST_CASE("Misc") {
}

SECTION("Unwrap Into Else") {
GEODE_UNWRAP_OR_ELSE(value, divideConstexpr(32, 2)) {
GEODE_UNWRAP_OR_ELSE(value, err, divideConstexpr(32, 2)) {
FAIL("Expected the block to not be executed");
}
REQUIRE(value == 16);

GEODE_UNWRAP_OR_ELSE(value2, err, divideConstexpr(32, 0)) {
REQUIRE(err == -1);
}
}

SECTION("Unwrap Either") {
Expand Down

0 comments on commit 8363d66

Please sign in to comment.