diff --git a/AK/Error.h b/AK/Error.h index 1318b206a1..f1cf38face 100644 --- a/AK/Error.h +++ b/AK/Error.h @@ -9,6 +9,7 @@ #include #include #include +#include #if defined(__serenity__) && defined(KERNEL) # include @@ -55,58 +56,41 @@ private: }; template -class [[nodiscard]] ErrorOr { +class [[nodiscard]] ErrorOr final : public Variant { public: - ErrorOr(T const& value) - : m_value(value) - { - } - - ErrorOr(T&& value) - : m_value(move(value)) - { - } + using Variant::Variant; template ALWAYS_INLINE ErrorOr(U&& value) requires(!IsSame, ErrorOr>) - : m_value(forward(value)) + : Variant(forward(value)) { } #ifdef __serenity__ ErrorOr(ErrnoCode code) - : m_error(Error::from_errno(code)) + : Variant(Error::from_errno(code)) { } #endif - ErrorOr(ErrorType&& error) - : m_error(move(error)) + T& value() { + return this->template get(); } + T const& value() const { return this->template get(); } + ErrorType& error() { return this->template get(); } + ErrorType const& error() const { return this->template get(); } - ErrorOr(ErrorOr&& other) = default; - ErrorOr(ErrorOr const& other) = default; - ~ErrorOr() = default; + bool is_error() const { return this->template has(); } - ErrorOr& operator=(ErrorOr&& other) = default; - ErrorOr& operator=(ErrorOr const& other) = default; - - 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() { return move(value()); } + ErrorType release_error() { return move(error()); } T release_value_but_fixme_should_propagate_errors() { return release_value(); } private: - Optional m_value; - Optional m_error; + // 'downcast' is fishy in this context. Let's hide it by making it private. + using Variant::downcast; }; // Partial specialization for void value type diff --git a/Tests/AK/TestTypeTraits.cpp b/Tests/AK/TestTypeTraits.cpp index 298906c56b..7e10af761a 100644 --- a/Tests/AK/TestTypeTraits.cpp +++ b/Tests/AK/TestTypeTraits.cpp @@ -39,9 +39,6 @@ #define EXPECT_VARIADIC_TRAIT_FALSE(trait, ...) \ static_assert(!trait<__VA_ARGS__>) -struct Empty { -}; - enum class Enummer : u8 { Dummmy, };