From 80cbf7c75bc0989681dbf85a59d4b5986fac98e6 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Tue, 15 Feb 2022 22:24:28 +0200 Subject: [PATCH] utils: Improve Mu::Result Ensure the Ok() and Err() actually work, and add support for Result --- lib/utils/mu-result.hh | 46 +++++++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/lib/utils/mu-result.hh b/lib/utils/mu-result.hh index 290ccfb5..8c6754a0 100644 --- a/lib/utils/mu-result.hh +++ b/lib/utils/mu-result.hh @@ -25,24 +25,56 @@ namespace Mu { /** - * A Result is _either_ some value of type T, _or_ an error. + * A little Rust-envy...a Result is _either_ some value of type T, _or_ a Mu::Error */ template using Result = tl::expected; +/** + * Ok() is not typically strictly needed (unlike Err), but imitates Rust's Ok + * and it helps the reader. + * + * @param t the value to return + * + * @return a success Result + */ template -typename Result::expected_type +typename Result::expected Ok(T&& t) { - return Result::expected(std::move(t)); + return std::move(t); } -template -typename Result::unexpected_type +/** + * Implementation of Ok() for void results. + * + * @return a success Result + */ +static inline Result +Ok() +{ + return {}; +} + +/** + * Return an error + * + * @param err the error + * + * @return error + */ +static inline tl::unexpected Err(Error&& err) { - return Result::unexpected(std::move(err)); + return tl::unexpected(std::move(err)); +} + +static inline tl::unexpected +Err(const Error& err) +{ + return tl::unexpected(err); } } // namespace Mu -#endif /* MU_ERROR_HH__ */ + +#endif /* MU_RESULT_HH__ */