1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 11:07:45 +00:00

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 :)
This commit is contained in:
Andrew Kaster 2021-01-01 03:39:04 -07:00 committed by Andreas Kling
parent 7b94ca21b3
commit 986544600a

View file

@ -38,14 +38,17 @@ public:
: m_result(res) : m_result(res)
{ {
} }
Result(ValueType&& res) Result(ValueType&& res)
: m_result(move(res)) : m_result(move(res))
{ {
} }
Result(const ErrorType& error) Result(const ErrorType& error)
: m_error(error) : m_error(error)
{ {
} }
Result(ErrorType&& error) Result(ErrorType&& error)
: m_error(move(error)) : m_error(move(error))
{ {
@ -57,21 +60,9 @@ public:
{ {
} }
Result(Result&& other) Result(Result&& other) = default;
: m_result(move(other.m_result)) Result(const Result& other) = default;
, m_error(move(other.m_error)) ~Result() = default;
{
}
Result(Result& other)
: m_result(other.m_result)
, m_error(other.m_error)
{
}
~Result()
{
}
ValueType& value() ValueType& value()
{ {
@ -93,6 +84,39 @@ private:
Optional<ErrorType> m_error; 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)
{
}
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<ErrorType> m_error;
};
} }
using AK::Result; using AK::Result;