MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / Result

Class Result

crates/c-api/include/wasmtime/error.hh:70–116  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

68 * C++ standard when it exists.
69 */
70template <typename T, typename E = Error> class [[nodiscard]] Result {
71 std::variant<T, E> data;
72
73public:
74 /// \brief Creates a `Result` from its successful value.
75 Result(T t) : data(std::move(t)) {}
76 /// \brief Creates a `Result` from an error value.
77 Result(E e) : data(std::move(e)) {}
78
79 /// \brief Returns `true` if this result is a success, `false` if it's an
80 /// error
81 explicit operator bool() const { return data.index() == 0; }
82
83 /// \brief Returns the error, if present, aborts if this is not an error.
84 E &&err() { return std::get<E>(std::move(data)); }
85 /// \brief Returns the error, if present, aborts if this is not an error.
86 const E &&err() const { return std::get<E>(std::move(data)); }
87
88 /// \brief Returns the success, if present, aborts if this is an error.
89 T &&ok() { return std::get<T>(std::move(data)); }
90 /// \brief Returns the success, if present, aborts if this is an error.
91 const T &&ok() const { return std::get<T>(std::move(data)); }
92
93 /// \brief Returns the success, if present, aborts if this is an error.
94 T &ok_ref() { return std::get<T>(data); }
95 /// \brief Returns the success, if present, aborts if this is an error.
96 const T &ok_ref() const { return std::get<T>(data); }
97
98 /// \brief Returns the error, if present, aborts if this is not an error.
99 E &err_ref() { return std::get<E>(data); }
100 /// \brief Returns the error, if present, aborts if this is not an error.
101 const E &err_ref() const { return std::get<E>(data); }
102
103 /// \brief Returns the success, if present, aborts if this is an error.
104 T unwrap() {
105 if (!*this) {
106 unwrap_failed();
107 }
108 return this->ok();
109 }
110
111private:
112 [[noreturn]] void unwrap_failed() {
113 fprintf(stderr, "error: %s\n", this->err().message().c_str()); // NOLINT
114 std::abort();
115 }
116};
117
118} // namespace wasmtime
119

Callers 14

value_defMethod · 0.85
valFunction · 0.85
liftMethod · 0.85
loadMethod · 0.85
fromMethod · 0.85
make_resultMethod · 0.85
fromMethod · 0.85
fromMethod · 0.85
defined_typeMethod · 0.85
convert_variantMethod · 0.85
arbitrary_valFunction · 0.85

Calls 1

indexMethod · 0.45

Tested by

no test coverage detected