From 986544600a94d0e3ae04cf253ad27b696961f142 Mon Sep 17 00:00:00 2001 From: Andrew Kaster Date: Fri, 1 Jan 2021 03:39:04 -0700 Subject: [PATCH] AK: Add Result specialization, cleanup Add a specialization for a void ValueType. This is useful if a generic function wants to return a Result where the user might not actually care abut the T, and default it to void. In this case it basically becomes Unexpected instead of Result, but hey, it works :) --- AK/Result.h | 54 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 15 deletions(-) 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;