#include <type_traits>
#include <utility>
+#include <glog/logging.h>
+
#include <folly/Likely.h>
+#include <folly/Optional.h>
#include <folly/Portability.h>
#include <folly/Preprocessor.h>
#include <folly/Traits.h>
// Details...
namespace expected_detail {
+
+template <typename Value, typename Error>
+struct PromiseReturn;
+
#ifdef _MSC_VER
// MSVC 2015 can't handle the StrictConjunction, so we have
// to use std::conjunction instead.
}
FOLLY_POP_WARNING
};
-}
+} // namespace expected_detail_ExpectedHelper
/* using override */ using expected_detail_ExpectedHelper::ExpectedHelper;
struct UnexpectedTag {};
BadExpectedAccess() : std::logic_error("bad Expected access") {}
};
+namespace expected_detail {
+
+[[noreturn]] void throwBadExpectedAccess();
+
+} // namespace expected_detail
+
/**
* Unexpected - a helper type used to disambiguate the construction of
* Expected objects in the error state.
return *this;
}
+ // Used only when an Expected is used with coroutines on MSVC
+ /* implicit */ Expected(const expected_detail::PromiseReturn<Value, Error>& p)
+ : Expected{} {
+ p.promise_->value_ = this;
+ }
+
template <class... Ts FOLLY_REQUIRES_TRAILING(
std::is_constructible<Value, Ts&&...>::value)>
void emplace(Ts&&... ts) {
void swap(Expected& that) noexcept(
expected_detail::StrictAllOf<IsNothrowSwappable, Value, Error>::value) {
if (this->uninitializedByException() || that.uninitializedByException()) {
- throw BadExpectedAccess();
+ expected_detail::throwBadExpectedAccess();
}
using std::swap;
if (*this) {
std::declval<const Base&>(),
std::declval<Fns>()...)) {
if (this->uninitializedByException()) {
- throw BadExpectedAccess();
+ expected_detail::throwBadExpectedAccess();
}
return expected_detail::ExpectedHelper::then_(
base(), static_cast<Fns&&>(fns)...);
std::declval<Base&>(),
std::declval<Fns>()...)) {
if (this->uninitializedByException()) {
- throw BadExpectedAccess();
+ expected_detail::throwBadExpectedAccess();
}
return expected_detail::ExpectedHelper::then_(
base(), static_cast<Fns&&>(fns)...);
std::declval<Base&&>(),
std::declval<Fns>()...)) {
if (this->uninitializedByException()) {
- throw BadExpectedAccess();
+ expected_detail::throwBadExpectedAccess();
}
return expected_detail::ExpectedHelper::then_(
std::move(base()), static_cast<Fns&&>(fns)...);
std::declval<Yes>()(std::declval<const Value&>())) {
using Ret = decltype(std::declval<Yes>()(std::declval<const Value&>()));
if (this->uninitializedByException()) {
- throw BadExpectedAccess();
+ expected_detail::throwBadExpectedAccess();
}
return Ret(expected_detail::ExpectedHelper::thenOrThrow_(
base(), static_cast<Yes&&>(yes), static_cast<No&&>(no)));
std::declval<Yes>()(std::declval<Value&>())) {
using Ret = decltype(std::declval<Yes>()(std::declval<Value&>()));
if (this->uninitializedByException()) {
- throw BadExpectedAccess();
+ expected_detail::throwBadExpectedAccess();
}
return Ret(expected_detail::ExpectedHelper::thenOrThrow_(
base(), static_cast<Yes&&>(yes), static_cast<No&&>(no)));
std::declval<Yes>()(std::declval<Value&&>())) {
using Ret = decltype(std::declval<Yes>()(std::declval<Value&&>()));
if (this->uninitializedByException()) {
- throw BadExpectedAccess();
+ expected_detail::throwBadExpectedAccess();
}
return Ret(expected_detail::ExpectedHelper::thenOrThrow_(
std::move(base()), static_cast<Yes&&>(yes), static_cast<No&&>(no)));
if (LIKELY(hasError())) {
throw typename Unexpected<Error>::BadExpectedAccess(this->error_);
}
- throw BadExpectedAccess();
+ expected_detail::throwBadExpectedAccess();
}
}
void requireError() const {
if (UNLIKELY(!hasError())) {
- throw BadExpectedAccess();
+ expected_detail::throwBadExpectedAccess();
}
}
operator==(
const Expected<Value, Error>& lhs,
const Expected<Value, Error>& rhs) {
- if (UNLIKELY(lhs.which_ != rhs.which_)) {
- return UNLIKELY(lhs.uninitializedByException()) ? false
- : throw BadExpectedAccess();
- }
if (UNLIKELY(lhs.uninitializedByException())) {
- throw BadExpectedAccess();
+ expected_detail::throwBadExpectedAccess();
+ }
+ if (UNLIKELY(lhs.which_ != rhs.which_)) {
+ return false;
}
if (UNLIKELY(lhs.hasError())) {
return true; // All error states are considered equal
const Expected<Value, Error>& rhs) {
if (UNLIKELY(
lhs.uninitializedByException() || rhs.uninitializedByException())) {
- throw BadExpectedAccess();
+ expected_detail::throwBadExpectedAccess();
}
if (UNLIKELY(lhs.hasError())) {
return !rhs.hasError();
#undef FOLLY_REQUIRES
#undef FOLLY_REQUIRES_TRAILING
+
+// Enable the use of folly::Expected with `co_await`
+// Inspired by https://github.com/toby-allsopp/coroutine_monad
+#if FOLLY_HAS_COROUTINES
+#include <experimental/coroutine>
+
+namespace folly {
+namespace expected_detail {
+template <typename Value, typename Error>
+struct Promise;
+
+template <typename Value, typename Error>
+struct PromiseReturn {
+ Optional<Expected<Value, Error>> storage_;
+ Promise<Value, Error>* promise_;
+ /* implicit */ PromiseReturn(Promise<Value, Error>& promise) noexcept
+ : promise_(&promise) {
+ promise_->value_ = &storage_;
+ }
+ PromiseReturn(PromiseReturn&& that) noexcept
+ : PromiseReturn{*that.promise_} {}
+ ~PromiseReturn() {}
+ /* implicit */ operator Expected<Value, Error>() & {
+ return std::move(*storage_);
+ }
+};
+
+template <typename Value, typename Error>
+struct Promise {
+ Optional<Expected<Value, Error>>* value_ = nullptr;
+ Promise() = default;
+ Promise(Promise const&) = delete;
+ // This should work regardless of whether the compiler generates:
+ // folly::Expected<Value, Error> retobj{ p.get_return_object(); } // MSVC
+ // or:
+ // auto retobj = p.get_return_object(); // clang
+ PromiseReturn<Value, Error> get_return_object() noexcept {
+ return *this;
+ }
+ std::experimental::suspend_never initial_suspend() const noexcept {
+ return {};
+ }
+ std::experimental::suspend_never final_suspend() const {
+ return {};
+ }
+ template <typename U>
+ void return_value(U&& u) {
+ value_->emplace(static_cast<U&&>(u));
+ }
+ void unhandled_exception() {
+ // Technically, throwing from unhandled_exception is underspecified:
+ // https://github.com/GorNishanov/CoroutineWording/issues/17
+ throw;
+ }
+};
+
+template <typename Value, typename Error>
+struct Awaitable {
+ Expected<Value, Error> o_;
+
+ explicit Awaitable(Expected<Value, Error> o) : o_(std::move(o)) {}
+
+ bool await_ready() const noexcept {
+ return o_.hasValue();
+ }
+ Value await_resume() {
+ return std::move(o_.value());
+ }
+
+ // Explicitly only allow suspension into a Promise
+ template <typename U>
+ void await_suspend(std::experimental::coroutine_handle<Promise<U, Error>> h) {
+ *h.promise().value_ = makeUnexpected(std::move(o_.error()));
+ // Abort the rest of the coroutine. resume() is not going to be called
+ h.destroy();
+ }
+};
+} // namespace expected_detail
+
+template <typename Value, typename Error>
+expected_detail::Awaitable<Value, Error>
+/* implicit */ operator co_await(Expected<Value, Error> o) {
+ return expected_detail::Awaitable<Value, Error>{std::move(o)};
+}
+} // namespace folly
+
+// This makes folly::Optional<Value> useable as a coroutine return type..
+FOLLY_NAMESPACE_STD_BEGIN
+namespace experimental {
+template <typename Value, typename Error, typename... Args>
+struct coroutine_traits<folly::Expected<Value, Error>, Args...> {
+ using promise_type = folly::expected_detail::Promise<Value, Error>;
+};
+} // namespace experimental
+FOLLY_NAMESPACE_STD_END
+#endif // FOLLY_HAS_COROUTINES