From: Yedidya Feldblum Date: Wed, 7 Jun 2017 09:33:31 +0000 (-0700) Subject: Shrink the implementations of then and onError X-Git-Tag: v2017.06.12.00~20 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=7dc11fbd0971fb22ad7917db145815afbe790070;p=folly.git Shrink the implementations of then and onError Summary: [Folly] Shrink the implementations of `then` and `onError`. Just a small refactor. Reviewed By: spacedentist Differential Revision: D5180784 fbshipit-source-id: a399d18500a2b4c5d8f24dee54891cca802b4461 --- diff --git a/folly/futures/Future-inl.h b/folly/futures/Future-inl.h index 427be4da..79b73a07 100644 --- a/folly/futures/Future-inl.h +++ b/folly/futures/Future-inl.h @@ -85,6 +85,11 @@ class CoreCallbackState { return std::move(func_)(std::forward(args)...); } + template + auto tryInvoke(Args&&... args) noexcept { + return makeTryWith([&] { return invoke(std::forward(args)...); }); + } + void setTry(Try&& t) { stealPromise().setTry(std::move(t)); } @@ -263,26 +268,17 @@ Future::thenImplementation(F&& func, detail::argResult) { setCallback_( [state = detail::makeCoreCallbackState( std::move(p), std::forward(func))](Try && t) mutable { - auto ew = [&] { - if (!isTry && t.hasException()) { - return std::move(t.exception()); + if (!isTry && t.hasException()) { + state.setException(std::move(t.exception())); + } else { + auto tf2 = state.tryInvoke(t.template get()...); + if (tf2.hasException()) { + state.setException(std::move(tf2.exception())); } else { - try { - auto f2 = state.invoke(t.template get()...); - // that didn't throw, now we can steal p - f2.setCallback_([p = state.stealPromise()](Try && b) mutable { - p.setTry(std::move(b)); - }); - return exception_wrapper(); - } catch (const std::exception& e) { - return exception_wrapper(std::current_exception(), e); - } catch (...) { - return exception_wrapper(std::current_exception()); - } + tf2->setCallback_([p = state.stealPromise()](Try && b) mutable { + p.setTry(std::move(b)); + }); } - }(); - if (ew) { - state.setException(std::move(ew)); } }); @@ -326,7 +322,7 @@ typename std::enable_if< !detail::Extract::ReturnsFuture::value, Future>::type Future::onError(F&& func) { - typedef typename detail::Extract::FirstArg Exn; + typedef std::remove_reference_t::FirstArg> Exn; static_assert( std::is_same::RawReturn, T>::value, "Return type of onError callback must be T or Future"); @@ -338,9 +334,9 @@ Future::onError(F&& func) { setCallback_( [state = detail::makeCoreCallbackState( std::move(p), std::forward(func))](Try && t) mutable { - if (!t.template withException([&](Exn& e) { - state.setTry(makeTryWith([&] { return state.invoke(e); })); - })) { + if (auto e = t.template tryGetExceptionObject()) { + state.setTry(makeTryWith([&] { return state.invoke(*e); })); + } else { state.setTry(std::move(t)); } }); @@ -359,7 +355,7 @@ Future::onError(F&& func) { static_assert( std::is_same::Return, Future>::value, "Return type of onError callback must be T or Future"); - typedef typename detail::Extract::FirstArg Exn; + typedef std::remove_reference_t::FirstArg> Exn; Promise p; auto f = p.getFuture(); @@ -367,23 +363,16 @@ Future::onError(F&& func) { setCallback_( [state = detail::makeCoreCallbackState( std::move(p), std::forward(func))](Try && t) mutable { - if (!t.template withException([&](Exn& e) { - auto ew = [&] { - try { - auto f2 = state.invoke(e); - f2.setCallback_([p = state.stealPromise()]( - Try && t2) mutable { p.setTry(std::move(t2)); }); - return exception_wrapper(); - } catch (const std::exception& e2) { - return exception_wrapper(std::current_exception(), e2); - } catch (...) { - return exception_wrapper(std::current_exception()); - } - }(); - if (ew) { - state.setException(std::move(ew)); - } - })) { + if (auto e = t.template tryGetExceptionObject()) { + auto tf2 = state.tryInvoke(*e); + if (tf2.hasException()) { + state.setException(std::move(tf2.exception())); + } else { + tf2->setCallback_([p = state.stealPromise()](Try && t3) mutable { + p.setTry(std::move(t3)); + }); + } + } else { state.setTry(std::move(t)); } }); @@ -423,21 +412,13 @@ Future::onError(F&& func) { [state = detail::makeCoreCallbackState( std::move(p), std::forward(func))](Try t) mutable { if (t.hasException()) { - auto ew = [&] { - try { - auto f2 = state.invoke(std::move(t.exception())); - f2.setCallback_([p = state.stealPromise()](Try t2) mutable { - p.setTry(std::move(t2)); - }); - return exception_wrapper(); - } catch (const std::exception& e2) { - return exception_wrapper(std::current_exception(), e2); - } catch (...) { - return exception_wrapper(std::current_exception()); - } - }(); - if (ew) { - state.setException(std::move(ew)); + auto tf2 = state.tryInvoke(std::move(t.exception())); + if (tf2.hasException()) { + state.setException(std::move(tf2.exception())); + } else { + tf2->setCallback_([p = state.stealPromise()](Try t3) mutable { + p.setTry(std::move(t3)); + }); } } else { state.setTry(std::move(t));