mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 00:50:22 +00:00
AK+Tests: Use less space in ErrorOr
This commit is contained in:
parent
c673b7220a
commit
208d85e707
Notes:
sideshowbarker
2024-07-17 22:44:28 +09:00
Author: https://github.com/BenWiederhake Commit: https://github.com/SerenityOS/serenity/commit/208d85e707c Pull-request: https://github.com/SerenityOS/serenity/pull/11274
2 changed files with 15 additions and 34 deletions
46
AK/Error.h
46
AK/Error.h
|
@ -9,6 +9,7 @@
|
|||
#include <AK/Optional.h>
|
||||
#include <AK/StringView.h>
|
||||
#include <AK/Try.h>
|
||||
#include <AK/Variant.h>
|
||||
|
||||
#if defined(__serenity__) && defined(KERNEL)
|
||||
# include <LibC/errno_numbers.h>
|
||||
|
@ -55,58 +56,41 @@ private:
|
|||
};
|
||||
|
||||
template<typename T, typename ErrorType>
|
||||
class [[nodiscard]] ErrorOr {
|
||||
class [[nodiscard]] ErrorOr final : public Variant<T, ErrorType> {
|
||||
public:
|
||||
ErrorOr(T const& value)
|
||||
: m_value(value)
|
||||
{
|
||||
}
|
||||
|
||||
ErrorOr(T&& value)
|
||||
: m_value(move(value))
|
||||
{
|
||||
}
|
||||
using Variant<T, ErrorType>::Variant;
|
||||
|
||||
template<typename U>
|
||||
ALWAYS_INLINE ErrorOr(U&& value) requires(!IsSame<RemoveCVReference<U>, ErrorOr<T>>)
|
||||
: m_value(forward<U>(value))
|
||||
: Variant<T, ErrorType>(forward<U>(value))
|
||||
{
|
||||
}
|
||||
|
||||
#ifdef __serenity__
|
||||
ErrorOr(ErrnoCode code)
|
||||
: m_error(Error::from_errno(code))
|
||||
: Variant<T, ErrorType>(Error::from_errno(code))
|
||||
{
|
||||
}
|
||||
#endif
|
||||
|
||||
ErrorOr(ErrorType&& error)
|
||||
: m_error(move(error))
|
||||
T& value()
|
||||
{
|
||||
return this->template get<T>();
|
||||
}
|
||||
T const& value() const { return this->template get<T>(); }
|
||||
ErrorType& error() { return this->template get<ErrorType>(); }
|
||||
ErrorType const& error() const { return this->template get<ErrorType>(); }
|
||||
|
||||
ErrorOr(ErrorOr&& other) = default;
|
||||
ErrorOr(ErrorOr const& other) = default;
|
||||
~ErrorOr() = default;
|
||||
bool is_error() const { return this->template has<ErrorType>(); }
|
||||
|
||||
ErrorOr& operator=(ErrorOr&& other) = default;
|
||||
ErrorOr& operator=(ErrorOr const& other) = default;
|
||||
|
||||
T& value() { return m_value.value(); }
|
||||
T const& value() const { return m_value.value(); }
|
||||
ErrorType& error() { return m_error.value(); }
|
||||
ErrorType const& error() const { return m_error.value(); }
|
||||
|
||||
bool is_error() const { return m_error.has_value(); }
|
||||
|
||||
T release_value() { return m_value.release_value(); }
|
||||
ErrorType release_error() { return m_error.release_value(); }
|
||||
T release_value() { return move(value()); }
|
||||
ErrorType release_error() { return move(error()); }
|
||||
|
||||
T release_value_but_fixme_should_propagate_errors() { return release_value(); }
|
||||
|
||||
private:
|
||||
Optional<T> m_value;
|
||||
Optional<ErrorType> m_error;
|
||||
// 'downcast' is fishy in this context. Let's hide it by making it private.
|
||||
using Variant<T, ErrorType>::downcast;
|
||||
};
|
||||
|
||||
// Partial specialization for void value type
|
||||
|
|
|
@ -39,9 +39,6 @@
|
|||
#define EXPECT_VARIADIC_TRAIT_FALSE(trait, ...) \
|
||||
static_assert(!trait<__VA_ARGS__>)
|
||||
|
||||
struct Empty {
|
||||
};
|
||||
|
||||
enum class Enummer : u8 {
|
||||
Dummmy,
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue