From 8363d66ec14ca6960a03680bf998a12237d7da73 Mon Sep 17 00:00:00 2001 From: altalk23 <45172705+altalk23@users.noreply.github.com> Date: Fri, 6 Dec 2024 21:50:18 +0300 Subject: [PATCH] change or else behavior --- CMakeLists.txt | 2 +- README.md | 4 ++-- include/Geode/Result.hpp | 12 +++++++----- test/Misc.cpp | 6 +++++- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 078bf47..1d3de1e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/README.md b/README.md index c73edea..677f708 100644 --- a/README.md +++ b/README.md @@ -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); diff --git a/include/Geode/Result.hpp b/include/Geode/Result.hpp index f3c4ad3..9c84486 100644 --- a/include/Geode/Result.hpp +++ b/include/Geode/Result.hpp @@ -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 diff --git a/test/Misc.cpp b/test/Misc.cpp index 8abaec5..af39b54 100644 --- a/test/Misc.cpp +++ b/test/Misc.cpp @@ -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") {