12#include <userver/compiler/impl/lifetime.hpp>
15USERVER_NAMESPACE_BEGIN
28concept StandardInteger =
29 std::integral<T> && !std::same_as<T, bool> && !std::same_as<T, char> && !std::same_as<T, wchar_t> &&
30 !std::same_as<T, char8_t> && !std::same_as<T, char16_t> && !std::same_as<T, char32_t>;
34template <
class T,
class U>
35constexpr bool ExpectedEqual(
const T& lhs,
const U& rhs) {
36 if constexpr (StandardInteger<T> && StandardInteger<U>) {
37 return std::cmp_equal(lhs, rhs);
45class bad_expected_access :
public std::exception {
47 using std::exception::exception;
49 explicit bad_expected_access(
const std::string& message)
53 const char* what()
const noexcept override;
60class [[nodiscard]] unexpected {
62 unexpected(
const E& error);
63 unexpected(E&& error);
65 template <
class... Args>
66 unexpected(Args&&... args);
68 template <
class U,
class... Args>
69 unexpected(std::initializer_list<U> il, Args&&... args);
71 E& error()
noexcept USERVER_IMPL_LIFETIME_BOUND;
72 const E& error()
const noexcept USERVER_IMPL_LIFETIME_BOUND;
84template <
class S,
class E>
85class [[nodiscard]] expected {
88 expected(
const S& success);
89 expected(S&& success);
94 requires std::is_convertible_v<G, E>
98 requires std::is_convertible_v<G, E>
105 explicit operator
bool() const noexcept;
110 S&
value() & USERVER_IMPL_LIFETIME_BOUND;
113 S&&
value() && USERVER_IMPL_LIFETIME_BOUND;
116 const S&
value() const& USERVER_IMPL_LIFETIME_BOUND;
121 S& operator*() & noexcept USERVER_IMPL_LIFETIME_BOUND;
124 S&& operator*() && noexcept USERVER_IMPL_LIFETIME_BOUND;
127 const S& operator*() const& noexcept USERVER_IMPL_LIFETIME_BOUND;
132 S* operator->() noexcept USERVER_IMPL_LIFETIME_BOUND;
135 const S* operator->() const noexcept USERVER_IMPL_LIFETIME_BOUND;
140 E&
error() USERVER_IMPL_LIFETIME_BOUND;
143 const E&
error() const USERVER_IMPL_LIFETIME_BOUND;
150class [[nodiscard]] expected<
void, E> {
152 constexpr expected()
noexcept;
157 requires std::is_convertible_v<G, E>
161 requires std::is_convertible_v<G, E>
164 bool has_value()
const noexcept;
165 explicit operator bool()
const noexcept;
168 E& error() USERVER_IMPL_LIFETIME_BOUND;
169 const E& error()
const USERVER_IMPL_LIFETIME_BOUND;
172 std::variant<std::monostate, unexpected<E>> data_;
176unexpected<E>::unexpected(
const E& error)
181unexpected<E>::unexpected(E&& error)
182 : value_{
std::forward<E>(error)}
186template <
class... Args>
187unexpected<E>::unexpected(Args&&... args)
188 : value_(std::forward<Args>(args)...)
192template <
class U,
class... Args>
193unexpected<E>::unexpected(std::initializer_list<U> il, Args&&... args)
194 : value_(il, std::forward<Args>(args)...)
198E& unexpected<E>::error() noexcept USERVER_IMPL_LIFETIME_BOUND {
203const E& unexpected<E>::error() const noexcept USERVER_IMPL_LIFETIME_BOUND {
207template <
class S,
class E>
208constexpr expected<S, E>::expected()
209 : data_(std::in_place_index<0>)
212template <
class S,
class E>
213expected<S, E>::expected(
const S& success)
217template <
class S,
class E>
218expected<S, E>::expected(S&& success)
219 : data_(std::forward<S>(success))
222template <
class S,
class E>
224 : data_(error.error())
227template <
class S,
class E>
229 : data_(std::forward<
unexpected<E>>(error.error()))
232template <
class S,
class E>
234requires std::is_convertible_v<G, E>
236 : data_(utils::
unexpected<E>(std::forward<G>(error.error())))
239template <
class S,
class E>
241requires std::is_convertible_v<G, E>
243 : data_(utils::
unexpected<E>(std::forward<G>(error.error())))
246template <
class S,
class E>
248 return std::holds_alternative<S>(data_);
251template <
class S,
class E>
252expected<S, E>::operator bool() const noexcept {
256template <
class S,
class E>
258 S* result = std::get_if<S>(&data_);
259 if (result ==
nullptr) {
265template <
class S,
class E>
267 return std::move(
value());
270template <
class S,
class E>
272 const S* result = std::get_if<S>(&data_);
273 if (result ==
nullptr) {
279template <
class S,
class E>
281 UASSERT_MSG(
has_value(),
"Trying to dereference utils::expected that does not contain a value");
282 return *std::get_if<S>(&data_);
285template <
class S,
class E>
287 return std::move(**
this);
290template <
class S,
class E>
292 UASSERT_MSG(
has_value(),
"Trying to dereference utils::expected that does not contain a value");
293 return *std::get_if<S>(&data_);
296template <
class S,
class E>
298 UASSERT_MSG(
has_value(),
"Trying to dereference utils::expected that does not contain a value");
299 return std::get_if<S>(&data_);
302template <
class S,
class E>
304 UASSERT_MSG(
has_value(),
"Trying to dereference utils::expected that does not contain a value");
305 return std::get_if<S>(&data_);
308template <
class S,
class E>
310 auto* result = std::get_if<unexpected<E>>(&data_);
311 if (result ==
nullptr) {
314 return result->error();
317template <
class S,
class E>
319 const auto* result = std::get_if<unexpected<E>>(&data_);
320 if (result ==
nullptr) {
323 return result->error();
330template <
class S,
class E>
331requires std::equality_comparable<S> && std::equality_comparable<E>
336 return lhs.
has_value() ? impl::ExpectedEqual(*lhs, *rhs) : impl::ExpectedEqual(lhs.
error(), rhs.
error());
343template <
class S,
class E,
class T>
344requires(!std::same_as<T, expected<S, E>>) && std::equality_comparable_with<S, T>
346 return lhs.
has_value() && impl::ExpectedEqual(*lhs, rhs);
353template <
class S,
class E,
class G>
354requires std::equality_comparable_with<E, G>
356 return !lhs.
has_value() && impl::ExpectedEqual(lhs.
error(), rhs.error());
360constexpr expected<void, E>::expected() noexcept: data_(
std::in_place_index<0>) {}
364 : data_(error.error())
368expected<void, E>::expected(unexpected<E>&& error)
369 : data_(
std::forward<unexpected<E>>(error.error()))
374requires std::is_convertible_v<G, E>
375expected<void, E>::expected(
const unexpected<G>& error)
376 : data_(
utils::unexpected<E>(
std::forward<G>(error.error())))
381requires std::is_convertible_v<G, E>
382expected<void, E>::expected(unexpected<G>&& error)
383 : data_(
utils::unexpected<E>(
std::forward<G>(error.error())))
387bool expected<void, E>::has_value() const noexcept {
388 return data_.index() == 0;
392expected<void, E>::operator bool() const noexcept {
397void expected<void, E>::value()
const {
399 throw bad_expected_access(
"Trying to get undefined value from utils::expected");
404E& expected<void, E>::error() USERVER_IMPL_LIFETIME_BOUND {
405 auto* result = std::get_if<unexpected<E>>(&data_);
406 if (result ==
nullptr) {
407 throw bad_expected_access(
"Trying to get undefined error value from utils::expected");
409 return result->error();
413const E& expected<void, E>::error() const USERVER_IMPL_LIFETIME_BOUND {
414 const auto* result = std::get_if<unexpected<E>>(&data_);
415 if (result ==
nullptr) {
416 throw bad_expected_access(
"Trying to get undefined error value from utils::expected");
418 return result->error();
426requires std::equality_comparable<E>
439template <
class E,
class G>
440requires std::equality_comparable_with<E, G>
442 return !lhs.
has_value() && impl::ExpectedEqual(lhs.
error(), rhs.error());