utils/result: add "unwrap" convenience function

This commit is contained in:
Dirk-Jan C. Binnema 2023-07-09 12:40:54 +03:00
parent 31f0c40893
commit 904f64aa03
1 changed files with 16 additions and 5 deletions

View File

@ -85,30 +85,41 @@ Err(const Result<T>& res)
* convenience
*/
template <typename ...T>
inline tl::unexpected<Error>
tl::unexpected<Error>
Err(Error::Code code, fmt::format_string<T...> frm, T&&... args)
{
return Err(Error{code, frm, std::forward<T>(args)...});
}
template <typename ...T>
inline tl::unexpected<Error>
tl::unexpected<Error>
Err(Error::Code code, GError **err, fmt::format_string<T...> frm, T&&... args)
{
return Err(Error{code, err, frm, std::forward<T>(args)...});
}
template<typename T>
T unwrap(Result<T>&& res)
{
if (!!res)
return std::move(res.value());
else
throw res.error();
}
/**
* Assert that some result has a value (for unit tests)
*
* @param R some result
*/
#define assert_valid_result(R) do { \
if(!R) { \
auto&& res__ = R; \
if(!res__) { \
mu_printerrln("{}:{}: error-result: {}", \
__FILE__, __LINE__, \
(R).error().what()); \
g_assert_true(!!R); \
(res__).error().what()); \
g_assert_true(!!res__); \
} \
} while(0)