Skip to content

Commit c088c7a

Browse files
committed
add operator* for unwrapping
1 parent 418b218 commit c088c7a

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cmake_minimum_required(VERSION 3.21 FATAL_ERROR)
22
cmake_policy(SET CMP0097 NEW)
33

4-
project(GeodeResult VERSION 1.1.1 LANGUAGES C CXX)
4+
project(GeodeResult VERSION 1.2.1 LANGUAGES C CXX)
55

66
add_library(GeodeResult INTERFACE)
77

include/Geode/Result.hpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,36 @@ namespace geode {
12121212
return !(*this == other);
12131213
}
12141214

1215+
/// @brief Unwraps the Ok value from the Result
1216+
/// @throw UnwrapException if the Result is Err
1217+
/// @return the Ok value
1218+
/// @note Same behavior as Result::unwrap()
1219+
constexpr decltype(auto) operator*() && noexcept
1220+
requires(!std::same_as<OkType, void>)
1221+
{
1222+
return std::move(*this).unwrap();
1223+
}
1224+
1225+
/// @brief Unwraps the Ok value from the Result
1226+
/// @throw UnwrapException if the Result is Err
1227+
/// @return the Ok value
1228+
/// @note Same behavior as Result::unwrap()
1229+
constexpr decltype(auto) operator*() & noexcept
1230+
requires(!std::same_as<OkType, void>)
1231+
{
1232+
return this->unwrap();
1233+
}
1234+
1235+
/// @brief Unwraps the Ok value from the Result
1236+
/// @throw UnwrapException if the Result is Err
1237+
/// @return the Ok value
1238+
/// @note Same behavior as Result::unwrap()
1239+
constexpr decltype(auto) operator*() const& noexcept
1240+
requires(!std::same_as<OkType, void>)
1241+
{
1242+
return this->unwrap();
1243+
}
1244+
12151245
/// @brief Returns true if the Result is Ok and the Ok value satisfies the predicate
12161246
/// @param predicate the predicate to check the Ok value against
12171247
/// @return true if the Result is Ok and the Ok value satisfies the predicate

test/Misc.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,10 @@ TEST_CASE("Misc") {
177177
}
178178
}
179179
}
180-
}
180+
181+
SECTION("Operator*") {
182+
auto res = divideConstRefErrRef(32, 2);
183+
REQUIRE(res.isOk());
184+
REQUIRE(*res == 16);
185+
}
186+
}

0 commit comments

Comments
 (0)