[ox/std] Improve r/l-value correctness of Result::to, make unwrap nodiscard

This commit is contained in:
Gary Talent 2023-12-14 22:41:05 -06:00
parent 30909f85a3
commit 3a781f6704

View File

@ -200,6 +200,7 @@ struct [[nodiscard]] Result {
return error; return error;
} }
[[nodiscard]]
constexpr T &unwrap() & noexcept { constexpr T &unwrap() & noexcept {
if (error) { if (error) {
oxPanic(error, "Failed unwrap"); oxPanic(error, "Failed unwrap");
@ -207,6 +208,7 @@ struct [[nodiscard]] Result {
return value; return value;
} }
[[nodiscard]]
constexpr T &&unwrap() && noexcept { constexpr T &&unwrap() && noexcept {
if (error) { if (error) {
oxPanic(error, "Failed unwrap"); oxPanic(error, "Failed unwrap");
@ -214,6 +216,7 @@ struct [[nodiscard]] Result {
return std::move(value); return std::move(value);
} }
[[nodiscard]]
constexpr T const&unwrap() const & noexcept { constexpr T const&unwrap() const & noexcept {
if (error) [[unlikely]] { if (error) [[unlikely]] {
oxPanic(error, "Failed unwrap"); oxPanic(error, "Failed unwrap");
@ -221,6 +224,7 @@ struct [[nodiscard]] Result {
return value; return value;
} }
[[nodiscard]]
constexpr T &unwrapThrow() & { constexpr T &unwrapThrow() & {
if (error) { if (error) {
throw ox::Exception(error); throw ox::Exception(error);
@ -228,6 +232,7 @@ struct [[nodiscard]] Result {
return value; return value;
} }
[[nodiscard]]
constexpr T &&unwrapThrow() && { constexpr T &&unwrapThrow() && {
if (error) { if (error) {
throw ox::Exception(error); throw ox::Exception(error);
@ -235,6 +240,7 @@ struct [[nodiscard]] Result {
return std::move(value); return std::move(value);
} }
[[nodiscard]]
constexpr T const&unwrapThrow() const & { constexpr T const&unwrapThrow() const & {
if (error) { if (error) {
throw ox::Exception(error); throw ox::Exception(error);
@ -243,13 +249,21 @@ struct [[nodiscard]] Result {
} }
template<typename U = T> template<typename U = T>
constexpr ox::Result<U> to(const auto &f) noexcept { constexpr ox::Result<U> to(auto const&f) & noexcept {
if (error) [[unlikely]] { if (error) [[unlikely]] {
return OxError(1); return error;
} }
return f(value); return f(value);
} }
template<typename U = T>
constexpr ox::Result<U> to(auto const&f) && noexcept {
if (error) [[unlikely]] {
return error;
}
return f(std::move(value));
}
}; };
namespace detail { namespace detail {