[ox/std] Fix Result move constructor, add & and && variants of unwrap

This commit is contained in:
Gary Talent 2023-12-01 22:50:25 -06:00
parent 9904399724
commit 72c130d8a9

View File

@ -138,7 +138,7 @@ struct [[nodiscard]] Result {
} }
template<typename U> template<typename U>
constexpr Result(const Result<U> &&other) noexcept: value(std::move(other.value)), error(std::move(other.error)) { constexpr Result(Result<U> &&other) noexcept: value(std::move(other.value)), error(std::move(other.error)) {
} }
constexpr Result(const Error &error) noexcept: value(), error(error) { constexpr Result(const Error &error) noexcept: value(), error(error) {
@ -182,23 +182,44 @@ struct [[nodiscard]] Result {
return error; return error;
} }
constexpr auto &unwrap() noexcept { constexpr T &unwrap() & noexcept {
if (error) { if (error) {
oxPanic(error, "Failed unwrap"); oxPanic(error, "Failed unwrap");
} }
return value; return value;
} }
constexpr auto &unwrapThrow() { constexpr T &&unwrap() && noexcept {
if (error) {
oxPanic(error, "Failed unwrap");
}
return std::move(value);
}
constexpr T const&unwrap() const & noexcept {
if (error) [[unlikely]] {
oxPanic(error, "Failed unwrap");
}
return value;
}
constexpr T &unwrapThrow() & {
if (error) { if (error) {
throw ox::Exception(error); throw ox::Exception(error);
} }
return value; return value;
} }
constexpr const auto &unwrap() const noexcept { constexpr T &&unwrapThrow() && {
if (error) [[unlikely]] { if (error) {
oxPanic(error, "Failed unwrap"); throw ox::Exception(error);
}
return std::move(value);
}
constexpr T const&unwrapThrow() const & {
if (error) {
throw ox::Exception(error);
} }
return value; return value;
} }