summaryrefslogtreecommitdiff
path: root/AK
diff options
context:
space:
mode:
authorAndrew Kaster <andrewdkaster@gmail.com>2021-01-01 03:39:04 -0700
committerAndreas Kling <kling@serenityos.org>2021-01-01 23:01:48 +0100
commit986544600a94d0e3ae04cf253ad27b696961f142 (patch)
treecd3905203ad232664506ab688f910040177101ce /AK
parent7b94ca21b3e99baa5102a0d069a1f637dff5ffd6 (diff)
downloadserenity-986544600a94d0e3ae04cf253ad27b696961f142.zip
AK: Add Result<void, ErrorType> specialization, cleanup
Add a specialization for a void ValueType. This is useful if a generic function wants to return a Result<T, E> where the user might not actually care abut the T, and default it to void. In this case it basically becomes Unexpected<E> instead of Result, but hey, it works :)
Diffstat (limited to 'AK')
-rw-r--r--AK/Result.h44
1 files changed, 34 insertions, 10 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,27 +60,49 @@ public:
{
}
- Result(Result&& other)
- : m_result(move(other.m_result))
- , m_error(move(other.m_error))
+ Result(Result&& other) = default;
+ Result(const Result& other) = default;
+ ~Result() = default;
+
+ ValueType& value()
{
+ return m_result.value();
}
- Result(Result& other)
- : m_result(other.m_result)
- , m_error(other.m_error)
+ ErrorType& error()
{
+ return m_error.value();
}
- ~Result()
+ bool is_error() const
{
+ return m_error.has_value();
}
- ValueType& value()
+private:
+ Optional<ValueType> m_result;
+ Optional<ErrorType> m_error;
+};
+
+// Partial specialization for void value type
+template<typename ErrorType>
+class [[nodiscard]] Result<void, ErrorType> {
+public:
+ Result(const ErrorType& error)
+ : m_error(error)
{
- return m_result.value();
}
+ 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();
@@ -89,7 +114,6 @@ public:
}
private:
- Optional<ValueType> m_result;
Optional<ErrorType> m_error;
};