mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 00:50:22 +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
Notes:
sideshowbarker
2024-07-19 00:15:07 +09:00
Author: https://github.com/ADKaster Commit: https://github.com/SerenityOS/serenity/commit/986544600a9 Pull-request: https://github.com/SerenityOS/serenity/pull/4705 Reviewed-by: https://github.com/tomuta
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)
|
||||
{
|
||||
}
|
||||
|
||||
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<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;
|
||||
|
|
Loading…
Reference in a new issue