mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 08:57:34 +00:00
AK+Tests: Use less space in ErrorOr
This commit is contained in:
parent
c673b7220a
commit
208d85e707
2 changed files with 15 additions and 34 deletions
46
AK/Error.h
46
AK/Error.h
|
@ -9,6 +9,7 @@
|
||||||
#include <AK/Optional.h>
|
#include <AK/Optional.h>
|
||||||
#include <AK/StringView.h>
|
#include <AK/StringView.h>
|
||||||
#include <AK/Try.h>
|
#include <AK/Try.h>
|
||||||
|
#include <AK/Variant.h>
|
||||||
|
|
||||||
#if defined(__serenity__) && defined(KERNEL)
|
#if defined(__serenity__) && defined(KERNEL)
|
||||||
# include <LibC/errno_numbers.h>
|
# include <LibC/errno_numbers.h>
|
||||||
|
@ -55,58 +56,41 @@ private:
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T, typename ErrorType>
|
template<typename T, typename ErrorType>
|
||||||
class [[nodiscard]] ErrorOr {
|
class [[nodiscard]] ErrorOr final : public Variant<T, ErrorType> {
|
||||||
public:
|
public:
|
||||||
ErrorOr(T const& value)
|
using Variant<T, ErrorType>::Variant;
|
||||||
: m_value(value)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
ErrorOr(T&& value)
|
|
||||||
: m_value(move(value))
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename U>
|
template<typename U>
|
||||||
ALWAYS_INLINE ErrorOr(U&& value) requires(!IsSame<RemoveCVReference<U>, ErrorOr<T>>)
|
ALWAYS_INLINE ErrorOr(U&& value) requires(!IsSame<RemoveCVReference<U>, ErrorOr<T>>)
|
||||||
: m_value(forward<U>(value))
|
: Variant<T, ErrorType>(forward<U>(value))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef __serenity__
|
#ifdef __serenity__
|
||||||
ErrorOr(ErrnoCode code)
|
ErrorOr(ErrnoCode code)
|
||||||
: m_error(Error::from_errno(code))
|
: Variant<T, ErrorType>(Error::from_errno(code))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
ErrorOr(ErrorType&& error)
|
T& value()
|
||||||
: m_error(move(error))
|
|
||||||
{
|
{
|
||||||
|
return this->template get<T>();
|
||||||
}
|
}
|
||||||
|
T const& value() const { return this->template get<T>(); }
|
||||||
|
ErrorType& error() { return this->template get<ErrorType>(); }
|
||||||
|
ErrorType const& error() const { return this->template get<ErrorType>(); }
|
||||||
|
|
||||||
ErrorOr(ErrorOr&& other) = default;
|
bool is_error() const { return this->template has<ErrorType>(); }
|
||||||
ErrorOr(ErrorOr const& other) = default;
|
|
||||||
~ErrorOr() = default;
|
|
||||||
|
|
||||||
ErrorOr& operator=(ErrorOr&& other) = default;
|
T release_value() { return move(value()); }
|
||||||
ErrorOr& operator=(ErrorOr const& other) = default;
|
ErrorType release_error() { return move(error()); }
|
||||||
|
|
||||||
T& value() { return m_value.value(); }
|
|
||||||
T const& value() const { return m_value.value(); }
|
|
||||||
ErrorType& error() { return m_error.value(); }
|
|
||||||
ErrorType const& error() const { return m_error.value(); }
|
|
||||||
|
|
||||||
bool is_error() const { return m_error.has_value(); }
|
|
||||||
|
|
||||||
T release_value() { return m_value.release_value(); }
|
|
||||||
ErrorType release_error() { return m_error.release_value(); }
|
|
||||||
|
|
||||||
T release_value_but_fixme_should_propagate_errors() { return release_value(); }
|
T release_value_but_fixme_should_propagate_errors() { return release_value(); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Optional<T> m_value;
|
// 'downcast' is fishy in this context. Let's hide it by making it private.
|
||||||
Optional<ErrorType> m_error;
|
using Variant<T, ErrorType>::downcast;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Partial specialization for void value type
|
// Partial specialization for void value type
|
||||||
|
|
|
@ -39,9 +39,6 @@
|
||||||
#define EXPECT_VARIADIC_TRAIT_FALSE(trait, ...) \
|
#define EXPECT_VARIADIC_TRAIT_FALSE(trait, ...) \
|
||||||
static_assert(!trait<__VA_ARGS__>)
|
static_assert(!trait<__VA_ARGS__>)
|
||||||
|
|
||||||
struct Empty {
|
|
||||||
};
|
|
||||||
|
|
||||||
enum class Enummer : u8 {
|
enum class Enummer : u8 {
|
||||||
Dummmy,
|
Dummmy,
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue