Skip to content

Commit

Permalink
add operator* for unwrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
matcool committed Nov 12, 2024
1 parent 418b218 commit c088c7a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 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.1.1 LANGUAGES C CXX)
project(GeodeResult VERSION 1.2.1 LANGUAGES C CXX)

add_library(GeodeResult INTERFACE)

Expand Down
30 changes: 30 additions & 0 deletions include/Geode/Result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<OkType, void>)
{
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<OkType, void>)
{
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<OkType, void>)
{
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
Expand Down
8 changes: 7 additions & 1 deletion test/Misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,10 @@ TEST_CASE("Misc") {
}
}
}
}

SECTION("Operator*") {
auto res = divideConstRefErrRef(32, 2);
REQUIRE(res.isOk());
REQUIRE(*res == 16);
}
}

0 comments on commit c088c7a

Please sign in to comment.