diff --git a/AK/Result.h b/AK/Result.h index c545ab367c..bc1cf3300c 100644 --- a/AK/Result.h +++ b/AK/Result.h @@ -38,14 +38,17 @@ public: : m_result(res) { } + Result(ValueType&& res) : m_result(move(res)) { } + Result(const ErrorType& error) : m_error(error) { } + Result(ErrorType&& error) : m_error(move(error)) { @@ -57,21 +60,9 @@ public: { } - Result(Result&& other) - : m_result(move(other.m_result)) - , m_error(move(other.m_error)) - { - } - - Result(Result& other) - : m_result(other.m_result) - , m_error(other.m_error) - { - } - - ~Result() - { - } + Result(Result&& other) = default; + Result(const Result& other) = default; + ~Result() = default; ValueType& value() { @@ -93,6 +84,39 @@ private: Optional m_error; }; +// Partial specialization for void value type +template +class [[nodiscard]] Result { +public: + Result(const ErrorType& error) + : m_error(error) + { + } + + Result(ErrorType&& error) + : m_error(move(error)) + { + } + + Result() = default; + Result(Result&& other) = default; + Result(const Result& other) = default; + ~Result() = default; + + ErrorType& error() + { + return m_error.value(); + } + + bool is_error() const + { + return m_error.has_value(); + } + +private: + Optional m_error; +}; + } using AK::Result;