utils: Improve Mu::Result

Ensure the Ok() and Err() actually work, and add support for Result<void>
This commit is contained in:
Dirk-Jan C. Binnema 2022-02-15 22:24:28 +02:00
parent 6ff1831200
commit 80cbf7c75b
1 changed files with 39 additions and 7 deletions

View File

@ -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 <typename T> using Result = tl::expected<T, Error>;
/**
* 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<T>
*/
template <typename T>
typename Result<T>::expected_type
typename Result<T>::expected
Ok(T&& t)
{
return Result<T>::expected(std::move(t));
return std::move(t);
}
template <typename T>
typename Result<T>::unexpected_type
/**
* Implementation of Ok() for void results.
*
* @return a success Result<void>
*/
static inline Result<void>
Ok()
{
return {};
}
/**
* Return an error
*
* @param err the error
*
* @return error
*/
static inline tl::unexpected<Error>
Err(Error&& err)
{
return Result<T>::unexpected(std::move(err));
return tl::unexpected(std::move(err));
}
static inline tl::unexpected<Error>
Err(const Error& err)
{
return tl::unexpected(err);
}
} // namespace Mu
#endif /* MU_ERROR_HH__ */
#endif /* MU_RESULT_HH__ */