Commit a74807f5 authored by Shai Szulanski's avatar Shai Szulanski Committed by Facebook GitHub Bot

Rename Try::throwIfFailed -> throwUnlessValue

Summary: Update name to match usage of Try as tri-state, since this method also throws if the Try is empty

Reviewed By: yfeldblum

Differential Revision: D25737810

fbshipit-source-id: a4166153362f07353d212216fbaf7105867eef2a
parent 8d485aeb
...@@ -146,30 +146,30 @@ exception_wrapper& Try<T>::emplaceException(Args&&... args) noexcept( ...@@ -146,30 +146,30 @@ exception_wrapper& Try<T>::emplaceException(Args&&... args) noexcept(
template <class T> template <class T>
T& Try<T>::value() & { T& Try<T>::value() & {
throwIfFailed(); throwUnlessValue();
return value_; return value_;
} }
template <class T> template <class T>
T&& Try<T>::value() && { T&& Try<T>::value() && {
throwIfFailed(); throwUnlessValue();
return std::move(value_); return std::move(value_);
} }
template <class T> template <class T>
const T& Try<T>::value() const& { const T& Try<T>::value() const& {
throwIfFailed(); throwUnlessValue();
return value_; return value_;
} }
template <class T> template <class T>
const T&& Try<T>::value() const&& { const T&& Try<T>::value() const&& {
throwIfFailed(); throwUnlessValue();
return std::move(value_); return std::move(value_);
} }
template <class T> template <class T>
void Try<T>::throwIfFailed() const { void Try<T>::throwUnlessValue() const {
switch (contains_) { switch (contains_) {
case Contains::VALUE: case Contains::VALUE:
return; return;
...@@ -181,6 +181,11 @@ void Try<T>::throwIfFailed() const { ...@@ -181,6 +181,11 @@ void Try<T>::throwIfFailed() const {
} }
} }
template <class T>
void Try<T>::throwIfFailed() const {
throwUnlessValue();
}
template <class T> template <class T>
void Try<T>::destroy() noexcept { void Try<T>::destroy() noexcept {
auto oldContains = std::exchange(contains_, Contains::NOTHING); auto oldContains = std::exchange(contains_, Contains::NOTHING);
...@@ -225,6 +230,10 @@ void Try<void>::throwIfFailed() const { ...@@ -225,6 +230,10 @@ void Try<void>::throwIfFailed() const {
} }
} }
void Try<void>::throwUnlessValue() const {
throwIfFailed();
}
template <typename F> template <typename F>
typename std::enable_if< typename std::enable_if<
!std::is_same<invoke_result_t<F>, void>::value, !std::is_same<invoke_result_t<F>, void>::value,
......
...@@ -150,42 +150,44 @@ class Try { ...@@ -150,42 +150,44 @@ class Try {
std::is_nothrow_constructible<exception_wrapper, Args&&...>::value); std::is_nothrow_constructible<exception_wrapper, Args&&...>::value);
/* /*
* Get a mutable reference to the contained value. If the Try contains an * Get a mutable reference to the contained value.
* exception it will be rethrown. * [Re]throws if the Try contains an exception or is empty.
* *
* @returns mutable reference to the contained value * @returns mutable reference to the contained value
*/ */
T& value() &; T& value() &;
/* /*
* Get a rvalue reference to the contained value. If the Try contains an * Get a rvalue reference to the contained value.
* exception it will be rethrown. * [Re]throws if the Try contains an exception or is empty.
* *
* @returns rvalue reference to the contained value * @returns rvalue reference to the contained value
*/ */
T&& value() &&; T&& value() &&;
/* /*
* Get a const reference to the contained value. If the Try contains an * Get a const reference to the contained value.
* exception it will be rethrown. * [Re]throws if the Try contains an exception or is empty.
* *
* @returns const reference to the contained value * @returns const reference to the contained value
*/ */
const T& value() const&; const T& value() const&;
/* /*
* Get a const rvalue reference to the contained value. If the Try contains an * Get a const rvalue reference to the contained value.
* exception it will be rethrown. * [Re]throws if the Try contains an exception or is empty.
* *
* @returns const rvalue reference to the contained value * @returns const rvalue reference to the contained value
*/ */
const T&& value() const&&; const T&& value() const&&;
/* /*
* If the Try contains an exception, rethrow it. Otherwise do nothing. * [Re]throw if the Try contains an exception or is empty. Otherwise do
* nothing.
*/ */
void throwIfFailed() const; void throwUnlessValue() const;
[[deprecated("Replaced by throwUnlessValue")]] void throwIfFailed() const;
/* /*
* Const dereference operator. If the Try contains an exception it will be * Const dereference operator.
* rethrown. * [Re]throws if the Try contains an exception or is empty.
* *
* @returns const reference to the contained value * @returns const reference to the contained value
*/ */
...@@ -212,8 +214,8 @@ class Try { ...@@ -212,8 +214,8 @@ class Try {
const T&& operator*() const&& { return std::move(value()); } const T&& operator*() const&& { return std::move(value()); }
/* /*
* Const arrow operator. If the Try contains an exception it will be * Const arrow operator.
* rethrown. * [Re]throws if the Try contains an exception or is empty.
* *
* @returns const reference to the contained value * @returns const reference to the contained value
*/ */
...@@ -438,6 +440,7 @@ class Try<void> { ...@@ -438,6 +440,7 @@ class Try<void> {
// If the Try contains an exception, throws it // If the Try contains an exception, throws it
inline void throwIfFailed() const; inline void throwIfFailed() const;
inline void throwUnlessValue() const;
// @returns False if the Try contains an exception, true otherwise // @returns False if the Try contains an exception, true otherwise
bool hasValue() const { return hasValue_; } bool hasValue() const { return hasValue_; }
......
...@@ -1049,13 +1049,14 @@ TEST(Future, throwCaughtInImmediateThen) { ...@@ -1049,13 +1049,14 @@ TEST(Future, throwCaughtInImmediateThen) {
TEST(Future, throwIfFailed) { TEST(Future, throwIfFailed) {
makeFuture<Unit>(eggs).then( makeFuture<Unit>(eggs).then(
[=](Try<Unit>&& t) { EXPECT_THROW(t.throwIfFailed(), eggs_t); }); [=](Try<Unit>&& t) { EXPECT_THROW(t.throwUnlessValue(), eggs_t); });
makeFuture().then([=](Try<Unit>&& t) { EXPECT_NO_THROW(t.throwIfFailed()); }); makeFuture().then(
[=](Try<Unit>&& t) { EXPECT_NO_THROW(t.throwUnlessValue()); });
makeFuture<int>(eggs).then( makeFuture<int>(eggs).then(
[=](Try<int>&& t) { EXPECT_THROW(t.throwIfFailed(), eggs_t); }); [=](Try<int>&& t) { EXPECT_THROW(t.throwUnlessValue(), eggs_t); });
makeFuture<int>(42).then( makeFuture<int>(42).then(
[=](Try<int>&& t) { EXPECT_NO_THROW(t.throwIfFailed()); }); [=](Try<int>&& t) { EXPECT_NO_THROW(t.throwUnlessValue()); });
} }
TEST(Future, getFutureAfterSetValue) { TEST(Future, getFutureAfterSetValue) {
......
...@@ -110,7 +110,7 @@ TEST(RetryingTest, future_factory_throws) { ...@@ -110,7 +110,7 @@ TEST(RetryingTest, future_factory_throws) {
}) })
.wait() .wait()
.result(); .result();
EXPECT_THROW(result.throwIfFailed(), ThrownException); EXPECT_THROW(result.throwUnlessValue(), ThrownException);
} }
TEST(RetryingTest, future_factory_throws_unsafe) { TEST(RetryingTest, future_factory_throws_unsafe) {
...@@ -131,7 +131,7 @@ TEST(RetryingTest, future_factory_throws_unsafe) { ...@@ -131,7 +131,7 @@ TEST(RetryingTest, future_factory_throws_unsafe) {
}) })
.wait() .wait()
.result(); .result();
EXPECT_THROW(result.throwIfFailed(), ThrownException); EXPECT_THROW(result.throwUnlessValue(), ThrownException);
} }
TEST(RetryingTest, policy_throws) { TEST(RetryingTest, policy_throws) {
......
...@@ -963,7 +963,7 @@ TEST(SemiFuture, semiFutureWithinNoValueReferenceWhenTimeOut) { ...@@ -963,7 +963,7 @@ TEST(SemiFuture, semiFutureWithinNoValueReferenceWhenTimeOut) {
auto f = promise.getSemiFuture().within(veryShort).toUnsafeFuture().thenTry( auto f = promise.getSemiFuture().within(veryShort).toUnsafeFuture().thenTry(
[](folly::Try<std::shared_ptr<int>>&& callbackInput) { [](folly::Try<std::shared_ptr<int>>&& callbackInput) {
EXPECT_THROW(callbackInput.throwIfFailed(), FutureTimeout); EXPECT_THROW(callbackInput.throwUnlessValue(), FutureTimeout);
}); });
std::move(f).get(); std::move(f).get();
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment