From c088c7ad6d81878af94a440ce6338fef5453341b Mon Sep 17 00:00:00 2001 From: matcool <26722564+matcool@users.noreply.github.com> Date: Tue, 12 Nov 2024 13:30:58 -0300 Subject: [PATCH] add operator* for unwrapping --- CMakeLists.txt | 2 +- include/Geode/Result.hpp | 30 ++++++++++++++++++++++++++++++ test/Misc.cpp | 8 +++++++- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b73a683..8099805 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.1.1 LANGUAGES C CXX) +project(GeodeResult VERSION 1.2.1 LANGUAGES C CXX) add_library(GeodeResult INTERFACE) diff --git a/include/Geode/Result.hpp b/include/Geode/Result.hpp index 832a55b..994426a 100644 --- a/include/Geode/Result.hpp +++ b/include/Geode/Result.hpp @@ -1212,6 +1212,36 @@ namespace geode { return !(*this == other); } + /// @brief Unwraps the Ok value from the Result + /// @throw UnwrapException if the Result is Err + /// @return the Ok value + /// @note Same behavior as Result::unwrap() + constexpr decltype(auto) operator*() && noexcept + requires(!std::same_as) + { + return std::move(*this).unwrap(); + } + + /// @brief Unwraps the Ok value from the Result + /// @throw UnwrapException if the Result is Err + /// @return the Ok value + /// @note Same behavior as Result::unwrap() + constexpr decltype(auto) operator*() & noexcept + requires(!std::same_as) + { + return this->unwrap(); + } + + /// @brief Unwraps the Ok value from the Result + /// @throw UnwrapException if the Result is Err + /// @return the Ok value + /// @note Same behavior as Result::unwrap() + constexpr decltype(auto) operator*() const& noexcept + requires(!std::same_as) + { + return this->unwrap(); + } + /// @brief Returns true if the Result is Ok and the Ok value satisfies the predicate /// @param predicate the predicate to check the Ok value against /// @return true if the Result is Ok and the Ok value satisfies the predicate diff --git a/test/Misc.cpp b/test/Misc.cpp index 2546509..5198f87 100644 --- a/test/Misc.cpp +++ b/test/Misc.cpp @@ -177,4 +177,10 @@ TEST_CASE("Misc") { } } } -} \ No newline at end of file + + SECTION("Operator*") { + auto res = divideConstRefErrRef(32, 2); + REQUIRE(res.isOk()); + REQUIRE(*res == 16); + } +}