mirror of https://github.com/djcb/mu.git
utils/result: add "unwrap" convenience function
This commit is contained in:
parent
31f0c40893
commit
904f64aa03
|
@ -85,30 +85,41 @@ Err(const Result<T>& res)
|
||||||
* convenience
|
* convenience
|
||||||
*/
|
*/
|
||||||
template <typename ...T>
|
template <typename ...T>
|
||||||
inline tl::unexpected<Error>
|
tl::unexpected<Error>
|
||||||
Err(Error::Code code, fmt::format_string<T...> frm, T&&... args)
|
Err(Error::Code code, fmt::format_string<T...> frm, T&&... args)
|
||||||
{
|
{
|
||||||
return Err(Error{code, frm, std::forward<T>(args)...});
|
return Err(Error{code, frm, std::forward<T>(args)...});
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename ...T>
|
template <typename ...T>
|
||||||
inline tl::unexpected<Error>
|
tl::unexpected<Error>
|
||||||
Err(Error::Code code, GError **err, fmt::format_string<T...> frm, T&&... args)
|
Err(Error::Code code, GError **err, fmt::format_string<T...> frm, T&&... args)
|
||||||
{
|
{
|
||||||
return Err(Error{code, err, frm, std::forward<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)
|
* Assert that some result has a value (for unit tests)
|
||||||
*
|
*
|
||||||
* @param R some result
|
* @param R some result
|
||||||
*/
|
*/
|
||||||
#define assert_valid_result(R) do { \
|
#define assert_valid_result(R) do { \
|
||||||
if(!R) { \
|
auto&& res__ = R; \
|
||||||
|
if(!res__) { \
|
||||||
mu_printerrln("{}:{}: error-result: {}", \
|
mu_printerrln("{}:{}: error-result: {}", \
|
||||||
__FILE__, __LINE__, \
|
__FILE__, __LINE__, \
|
||||||
(R).error().what()); \
|
(res__).error().what()); \
|
||||||
g_assert_true(!!R); \
|
g_assert_true(!!res__); \
|
||||||
} \
|
} \
|
||||||
} while(0)
|
} while(0)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue