:github_url: https://github.com/nyx-org/gaia .. _program_listing_file_lib_result.hpp: Program Listing for File result.hpp =================================== |exhale_lsh| :ref:`Return to documentation for file ` (``lib/result.hpp``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp /* SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #ifdef __KERNEL__ #include #endif #include namespace Gaia { struct Void {}; template class Ok { public: constexpr explicit Ok(T value) : value(std::move(value)) {} T value; }; template class Err { public: constexpr explicit Err(T value) : value(value) {} T value; }; template class Result { public: constexpr Result(Ok value) : variant(std::move(value)) {} constexpr Result(Err value) : variant(std::move(value)) {} constexpr bool is_ok() { return variant.template is>(); } constexpr bool is_err() { return variant.template is>(); } constexpr T unwrap(const FormatWithLocation &fmt = "unwrap failed: cannot unwrap an error type, err: {}") { if (is_ok()) return value().value(); panic(fmt, error_to_string(error().value())); } constexpr T unwrap_or(T _value) { if (is_ok()) return value().value(); return _value; } constexpr frg::optional error() { if (is_err()) return variant.template get>().value; return frg::null_opt; } constexpr frg::optional value() { if (is_ok()) return std::move(variant.template get>().value); return frg::null_opt; } private: frg::variant, Err> variant; }; #define TRY(X) \ ({ \ auto __ret = (X); \ if (__ret.is_err()) \ return Err(__ret.error().value()); \ __ret.value().value(); \ }) } // namespace Gaia