Coverage Report

Created: 2026-02-16 07:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/serenity/AK/Result.h
Line
Count
Source
1
/*
2
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3
 *
4
 * SPDX-License-Identifier: BSD-2-Clause
5
 */
6
7
#pragma once
8
9
#include <AK/Assertions.h>
10
#include <AK/Optional.h>
11
12
namespace AK {
13
14
template<typename ValueT, typename ErrorT>
15
class [[nodiscard]] Result {
16
public:
17
    using ValueType = ValueT;
18
    using ErrorType = ErrorT;
19
20
    Result(ValueType const& res)
21
0
        : m_result(res)
22
0
    {
23
0
    }
24
25
    Result(ValueType&& res)
26
3.37M
        : m_result(move(res))
27
3.37M
    {
28
3.37M
    }
AK::Result<unsigned int, AK::GenericLexer::UnicodeEscapeError>::Result(unsigned int&&)
Line
Count
Source
26
3.37M
        : m_result(move(res))
27
3.37M
    {
28
3.37M
    }
Unexecuted instantiation: AK::Result<regex::RegexOptions<regex::ECMAScriptFlags>, AK::ByteString>::Result(regex::RegexOptions<regex::ECMAScriptFlags>&&)
Unexecuted instantiation: AK::Result<JS::NonnullGCPtr<JS::Script>, AK::Vector<JS::ParserError, 0ul> >::Result(JS::NonnullGCPtr<JS::Script>&&)
Unexecuted instantiation: AK::Result<JS::NonnullGCPtr<JS::SourceTextModule>, AK::Vector<JS::ParserError, 0ul> >::Result(JS::NonnullGCPtr<JS::SourceTextModule>&&)
Unexecuted instantiation: AK::Result<AK::ByteString, Line::Editor::Error>::Result(AK::ByteString&&)
Unexecuted instantiation: AK::Result<AK::Vector<unsigned long, 2ul>, Line::Editor::Error>::Result(AK::Vector<unsigned long, 2ul>&&)
29
30
    Result(ErrorType const& error)
31
0
        : m_error(error)
32
0
    {
33
0
    }
Unexecuted instantiation: AK::Result<JS::NonnullGCPtr<JS::Script>, AK::Vector<JS::ParserError, 0ul> >::Result(AK::Vector<JS::ParserError, 0ul> const&)
Unexecuted instantiation: AK::Result<JS::NonnullGCPtr<JS::SourceTextModule>, AK::Vector<JS::ParserError, 0ul> >::Result(AK::Vector<JS::ParserError, 0ul> const&)
Unexecuted instantiation: AK::Result<AK::ByteString, Line::Editor::Error>::Result(Line::Editor::Error const&)
Unexecuted instantiation: AK::Result<AK::Vector<unsigned long, 2ul>, Line::Editor::Error>::Result(Line::Editor::Error const&)
34
35
    Result(ErrorType&& error)
36
697k
        : m_error(move(error))
37
697k
    {
38
697k
    }
AK::Result<unsigned int, AK::GenericLexer::UnicodeEscapeError>::Result(AK::GenericLexer::UnicodeEscapeError&&)
Line
Count
Source
36
697k
        : m_error(move(error))
37
697k
    {
38
697k
    }
Unexecuted instantiation: AK::Result<regex::RegexOptions<regex::ECMAScriptFlags>, AK::ByteString>::Result(AK::ByteString&&)
Unexecuted instantiation: AK::Result<AK::ByteString, Line::Editor::Error>::Result(Line::Editor::Error&&)
Unexecuted instantiation: AK::Result<AK::Vector<unsigned long, 2ul>, Line::Editor::Error>::Result(Line::Editor::Error&&)
39
40
    Result(Result&& other) = default;
41
    Result(Result const& other) = default;
42
0
    ~Result() = default;
Unexecuted instantiation: AK::Result<JS::NonnullGCPtr<JS::Script>, AK::Vector<JS::ParserError, 0ul> >::~Result()
Unexecuted instantiation: AK::Result<regex::RegexOptions<regex::ECMAScriptFlags>, AK::ByteString>::~Result()
Unexecuted instantiation: AK::Result<JS::NonnullGCPtr<JS::SourceTextModule>, AK::Vector<JS::ParserError, 0ul> >::~Result()
Unexecuted instantiation: AK::Result<AK::ByteString, Line::Editor::Error>::~Result()
Unexecuted instantiation: AK::Result<AK::Vector<unsigned long, 2ul>, Line::Editor::Error>::~Result()
43
44
    ValueType& value()
45
3.37M
    {
46
3.37M
        return m_result.value();
47
3.37M
    }
AK::Result<unsigned int, AK::GenericLexer::UnicodeEscapeError>::value()
Line
Count
Source
45
3.37M
    {
46
3.37M
        return m_result.value();
47
3.37M
    }
Unexecuted instantiation: AK::Result<JS::NonnullGCPtr<JS::Script>, AK::Vector<JS::ParserError, 0ul> >::value()
Unexecuted instantiation: AK::Result<AK::Vector<unsigned long, 2ul>, Line::Editor::Error>::value()
Unexecuted instantiation: AK::Result<AK::ByteString, Line::Editor::Error>::value()
Unexecuted instantiation: AK::Result<JS::NonnullGCPtr<JS::SourceTextModule>, AK::Vector<JS::ParserError, 0ul> >::value()
48
49
    ErrorType& error()
50
0
    {
51
0
        return m_error.value();
52
0
    }
Unexecuted instantiation: AK::Result<JS::NonnullGCPtr<JS::SourceTextModule>, AK::Vector<JS::ParserError, 0ul> >::error()
Unexecuted instantiation: AK::Result<unsigned int, AK::GenericLexer::UnicodeEscapeError>::error()
Unexecuted instantiation: AK::Result<AK::Vector<unsigned long, 2ul>, Line::Editor::Error>::error()
Unexecuted instantiation: AK::Result<AK::ByteString, Line::Editor::Error>::error()
Unexecuted instantiation: AK::Result<JS::NonnullGCPtr<JS::Script>, AK::Vector<JS::ParserError, 0ul> >::error()
53
54
    bool is_error() const
55
4.07M
    {
56
4.07M
        return m_error.has_value();
57
4.07M
    }
AK::Result<unsigned int, AK::GenericLexer::UnicodeEscapeError>::is_error() const
Line
Count
Source
55
4.07M
    {
56
4.07M
        return m_error.has_value();
57
4.07M
    }
Unexecuted instantiation: AK::Result<JS::NonnullGCPtr<JS::Script>, AK::Vector<JS::ParserError, 0ul> >::is_error() const
Unexecuted instantiation: AK::Result<regex::RegexOptions<regex::ECMAScriptFlags>, AK::ByteString>::is_error() const
Unexecuted instantiation: AK::Result<JS::NonnullGCPtr<JS::SourceTextModule>, AK::Vector<JS::ParserError, 0ul> >::is_error() const
Unexecuted instantiation: AK::Result<AK::Vector<unsigned long, 2ul>, Line::Editor::Error>::is_error() const
Unexecuted instantiation: AK::Result<AK::ByteString, Line::Editor::Error>::is_error() const
58
59
    ValueType release_value()
60
0
    {
61
0
        return m_result.release_value();
62
0
    }
Unexecuted instantiation: AK::Result<regex::RegexOptions<regex::ECMAScriptFlags>, AK::ByteString>::release_value()
Unexecuted instantiation: AK::Result<JS::NonnullGCPtr<JS::SourceTextModule>, AK::Vector<JS::ParserError, 0ul> >::release_value()
Unexecuted instantiation: AK::Result<JS::NonnullGCPtr<JS::Script>, AK::Vector<JS::ParserError, 0ul> >::release_value()
63
64
    ErrorType release_error()
65
0
    {
66
0
        return m_error.release_value();
67
0
    }
68
69
private:
70
    Optional<ValueType> m_result;
71
    Optional<ErrorType> m_error;
72
};
73
74
// Partial specialization for void value type
75
template<typename ErrorT>
76
class [[nodiscard]] Result<void, ErrorT> {
77
public:
78
    using ValueType = void;
79
    using ErrorType = ErrorT;
80
81
    Result(ErrorType const& error)
82
        : m_error(error)
83
    {
84
    }
85
86
    Result(ErrorType&& error)
87
0
        : m_error(move(error))
88
0
    {
89
0
    }
90
91
61
    Result() = default;
92
    Result(Result&& other) = default;
93
    Result(Result const& other) = default;
94
    ~Result() = default;
95
96
    // For compatibility with TRY().
97
    void value() { }
98
    void release_value() { }
99
100
    ErrorType& error()
101
    {
102
        return m_error.value();
103
    }
104
105
    bool is_error() const
106
    {
107
        return m_error.has_value();
108
    }
109
110
    ErrorType release_error()
111
    {
112
        return m_error.release_value();
113
    }
114
115
private:
116
    Optional<ErrorType> m_error;
117
};
118
119
}
120
121
#if USING_AK_GLOBALLY
122
using AK::Result;
123
#endif