20 #ifndef CS_CRYPTO_UTIL_RESULT_H
21 #define CS_CRYPTO_UTIL_RESULT_H
24 #include <type_traits>
26 namespace cs_crypto::util {
31 static_assert(! std::is_same_v<E,
void>,
"Error type can not be void");
36 constexpr explicit error(
const E &err)
noexcept
41 constexpr explicit error(E &&err)
noexcept
42 : m_error(std::move(err))
46 ~error()
noexcept =
default;
48 constexpr error(
const error &)
noexcept =
default;
49 constexpr error &operator=(
const error &) &
noexcept =
default;
51 constexpr error(error &&)
noexcept =
default;
52 constexpr error &operator=(error &&) &
noexcept =
default;
54 constexpr const E &value() &
noexcept
59 constexpr const E &value()
const &
noexcept
64 constexpr E &&value() &&
noexcept
66 return std::move(m_error);
73 template <
typename Value,
typename Error>
77 constexpr explicit result(Value &&value)
noexcept
78 : m_content(std::forward<Value>(value))
82 constexpr explicit result(Error &&err)
noexcept
83 : m_content(error(std::move(err)))
87 ~result()
noexcept =
default;
89 constexpr result(
const result &)
noexcept =
default;
90 constexpr result &operator=(
const result &) &
noexcept =
default;
92 constexpr result(result &&)
noexcept =
default;
93 constexpr result &operator=(result &&) &
noexcept =
default;
95 constexpr const Value &value()
const &
noexcept
97 return std::get<Value>(m_content);
100 constexpr const Value &value() &
noexcept
102 return std::get<Value>(m_content);
105 constexpr Value &&value() &&
noexcept
107 return std::get<Value>(std::move(m_content));
110 constexpr const Error &err()
const &
noexcept
112 return std::get<error_t>(m_content).value();
115 constexpr const Error &err() &
noexcept
117 return std::get<error_t>(m_content).value();
120 constexpr Error &&err() &&
noexcept
122 return std::get<error_t>(std::move(m_content)).value();
125 constexpr bool is_error()
const noexcept
127 return std::holds_alternative<error_t>(m_content);
130 constexpr bool is_ok()
const noexcept
132 return std::holds_alternative<Value>(m_content);
136 using error_t = error<Error>;
139 const std::variant<Value, error_t> m_content;