mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 10:57:35 +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:
parent
7b94ca21b3
commit
986544600a
1 changed files with 39 additions and 15 deletions
54
AK/Result.h
54
AK/Result.h
|
@ -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;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue