| 68 | * C++ standard when it exists. |
| 69 | */ |
| 70 | template <typename T, typename E = Error> class [[nodiscard]] Result { |
| 71 | std::variant<T, E> data; |
| 72 | |
| 73 | public: |
| 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 | |
| 111 | private: |
| 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 |
no test coverage detected