@@ -226,6 +226,15 @@ class non_value_result {
226
226
return std::move (ew_);
227
227
}
228
228
229
+ // / AVOID. Most code should use `result` coros, which catch most exceptions
230
+ // / automatically. Or, for a stronger guarantee, see `result_catch_all`.
231
+ static non_value_result from_current_exception () {
232
+ // Something was already thrown, and the user likely wants a result, so
233
+ // it's appropriate to accept even `OperationCancelled` here.
234
+ return non_value_result::make_legacy_error_or_cancellation (
235
+ exception_wrapper{current_exception ()});
236
+ }
237
+
229
238
friend inline bool operator ==(
230
239
const non_value_result& lhs, const non_value_result& rhs) {
231
240
return lhs.ew_ == rhs.ew_ ;
@@ -294,7 +303,7 @@ class result_crtp {
294
303
static_assert (!std::is_same_v<stopped_result_t , std::remove_cvref_t <T>>);
295
304
296
305
public:
297
- using value_type = T;
306
+ using value_type = T; // NB: can be a reference
298
307
299
308
protected:
300
309
using storage_type = detail::result_ref_wrap<lift_unit_t <T>>;
@@ -691,9 +700,8 @@ template <typename T>
691
700
struct is_result <result<T>> : std::true_type {};
692
701
693
702
// This short-circuiting coroutine implementation was modeled on
694
- // `folly/Expected.h`, which is likely to follow the state of the art in
695
- // compiler support & optimizations. So, if you're looking at this, please
696
- // compare it to the original, and backport any improvements here.
703
+ // `folly/Expected.h`. Please try to port any compiler fixes or
704
+ // optimizations across both.
697
705
namespace detail {
698
706
699
707
template <typename >
@@ -712,12 +720,13 @@ struct result_promise_return {
712
720
void operator =(result_promise_return const &) = delete ;
713
721
result_promise_return (result_promise_return&&) = delete ;
714
722
void operator =(result_promise_return&&) = delete ;
723
+ // Remove this once clang 15 is well-forgotten. From D42260201:
715
724
// letting dtor be trivial makes the coroutine crash
716
- // TODO: fix clang/llvm codegen
717
725
~result_promise_return () {}
718
726
719
727
/* implicit */ operator result<T>() {
720
- // D42260201: handle both deferred and eager return-object conversion
728
+ // Simplify this once clang 15 is well-forgotten. From D42260201:
729
+ // handle both deferred and eager return-object conversion
721
730
// behaviors see docs for detect_promise_return_object_eager_conversion
722
731
if (coro::detect_promise_return_object_eager_conversion ()) {
723
732
assert (storage_.is_expected_empty ());
@@ -747,10 +756,7 @@ struct result_promise_base {
747
756
return {};
748
757
}
749
758
void unhandled_exception () noexcept {
750
- // We're making a `result`, so it's OK to forward all exceptions into it,
751
- // including `OperationCancelled`.
752
- *value_ = non_value_result::make_legacy_error_or_cancellation (
753
- exception_wrapper{std::current_exception ()});
759
+ *value_ = non_value_result::from_current_exception ();
754
760
}
755
761
756
762
result_promise_return<T> get_return_object () noexcept { return *this ; }
@@ -781,7 +787,8 @@ struct result_promise<T, typename std::enable_if<std::is_void_v<T>>::type>
781
787
template <typename T>
782
788
using result_promise_handle = folly::coro::coroutine_handle<result_promise<T>>;
783
789
784
- // This is separate to let `result_generator` reuse the awaitables below.
790
+ // This is separate to let future https://fburl.com/result-generator-impl reuse
791
+ // the awaitables below.
785
792
struct result_await_suspender {
786
793
// Future: check if all these `FOLLY_ALWAYS_INLINE`s aren't a pessimization.
787
794
template <typename T, typename U>
@@ -934,10 +941,7 @@ result_catch_all(F&& fn) noexcept {
934
941
return static_cast <F&&>(fn)();
935
942
}
936
943
} catch (...) {
937
- // We're a making `result`, so it's OK to forward all exceptions into it,
938
- // including `OperationCancelled`.
939
- return non_value_result::make_legacy_error_or_cancellation (
940
- exception_wrapper{std::current_exception ()});
944
+ return non_value_result::from_current_exception ();
941
945
}
942
946
}
943
947
0 commit comments