From 904f64aa034be820f8c12b18cba57862e78faf42 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Sun, 9 Jul 2023 12:40:54 +0300 Subject: [PATCH] utils/result: add "unwrap" convenience function --- lib/utils/mu-result.hh | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/lib/utils/mu-result.hh b/lib/utils/mu-result.hh index 02ca332f..f33a2edc 100644 --- a/lib/utils/mu-result.hh +++ b/lib/utils/mu-result.hh @@ -85,30 +85,41 @@ Err(const Result& res) * convenience */ template -inline tl::unexpected +tl::unexpected Err(Error::Code code, fmt::format_string frm, T&&... args) { return Err(Error{code, frm, std::forward(args)...}); } template -inline tl::unexpected +tl::unexpected Err(Error::Code code, GError **err, fmt::format_string frm, T&&... args) { return Err(Error{code, err, frm, std::forward(args)...}); } + +template +T unwrap(Result&& 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)