From 3a781f670482d735ecb9a5c6c4b3e10362a6b56c Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Thu, 14 Dec 2023 22:41:05 -0600 Subject: [PATCH] [ox/std] Improve r/l-value correctness of Result::to, make unwrap nodiscard --- deps/ox/src/ox/std/error.hpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/deps/ox/src/ox/std/error.hpp b/deps/ox/src/ox/std/error.hpp index ecfbec13..a3de8952 100644 --- a/deps/ox/src/ox/std/error.hpp +++ b/deps/ox/src/ox/std/error.hpp @@ -200,6 +200,7 @@ struct [[nodiscard]] Result { return error; } + [[nodiscard]] constexpr T &unwrap() & noexcept { if (error) { oxPanic(error, "Failed unwrap"); @@ -207,6 +208,7 @@ struct [[nodiscard]] Result { return value; } + [[nodiscard]] constexpr T &&unwrap() && noexcept { if (error) { oxPanic(error, "Failed unwrap"); @@ -214,6 +216,7 @@ struct [[nodiscard]] Result { return std::move(value); } + [[nodiscard]] constexpr T const&unwrap() const & noexcept { if (error) [[unlikely]] { oxPanic(error, "Failed unwrap"); @@ -221,6 +224,7 @@ struct [[nodiscard]] Result { return value; } + [[nodiscard]] constexpr T &unwrapThrow() & { if (error) { throw ox::Exception(error); @@ -228,6 +232,7 @@ struct [[nodiscard]] Result { return value; } + [[nodiscard]] constexpr T &&unwrapThrow() && { if (error) { throw ox::Exception(error); @@ -235,6 +240,7 @@ struct [[nodiscard]] Result { return std::move(value); } + [[nodiscard]] constexpr T const&unwrapThrow() const & { if (error) { throw ox::Exception(error); @@ -243,13 +249,21 @@ struct [[nodiscard]] Result { } template - constexpr ox::Result to(const auto &f) noexcept { + constexpr ox::Result to(auto const&f) & noexcept { if (error) [[unlikely]] { - return OxError(1); + return error; } return f(value); } + template + constexpr ox::Result to(auto const&f) && noexcept { + if (error) [[unlikely]] { + return error; + } + return f(std::move(value)); + } + }; namespace detail {