diff options
Diffstat (limited to 'include/ftl/optional.h')
-rw-r--r-- | include/ftl/optional.h | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/include/ftl/optional.h b/include/ftl/optional.h index 94d8e3d7cb..e245d88c1c 100644 --- a/include/ftl/optional.h +++ b/include/ftl/optional.h @@ -20,13 +20,14 @@ #include <optional> #include <utility> +#include <android-base/expected.h> #include <ftl/details/optional.h> namespace android::ftl { // Superset of std::optional<T> with monadic operations, as proposed in https://wg21.link/P0798R8. // -// TODO: Remove in C++23. +// TODO: Remove standard APIs in C++23. // template <typename T> struct Optional final : std::optional<T> { @@ -109,6 +110,13 @@ struct Optional final : std::optional<T> { return std::forward<F>(f)(); } + // Maps this Optional<T> to expected<T, E> where nullopt becomes E. + template <typename E> + constexpr auto ok_or(E&& e) && -> base::expected<T, E> { + if (has_value()) return std::move(value()); + return base::unexpected(std::forward<E>(e)); + } + // Delete new for this class. Its base doesn't have a virtual destructor, and // if it got deleted via base class pointer, it would cause undefined // behavior. There's not a good reason to allocate this object on the heap |