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(
template <class T>
T& Try<T>::value() & {
throwIfFailed();
throwUnlessValue();
return value_;
}
template <class T>
T&& Try<T>::value() && {
throwIfFailed();
throwUnlessValue();
return std::move(value_);
}
template <class T>
const T& Try<T>::value() const& {
throwIfFailed();
throwUnlessValue();
return value_;
}
template <class T>
const T&& Try<T>::value() const&& {
throwIfFailed();
throwUnlessValue();
return std::move(value_);
}
template <class T>
void Try<T>::throwIfFailed() const {
void Try<T>::throwUnlessValue() const {
switch (contains_) {
case Contains::VALUE:
return;
......@@ -181,6 +181,11 @@ void Try<T>::throwIfFailed() const {
}
}
template <class T>
void Try<T>::throwIfFailed() const {
throwUnlessValue();
}
template <class T>
void Try<T>::destroy() noexcept {
auto oldContains = std::exchange(contains_, Contains::NOTHING);
......@@ -225,6 +230,10 @@ void Try<void>::throwIfFailed() const {
}
}
void Try<void>::throwUnlessValue() const {
throwIfFailed();
}
template <typename F>
typename std::enable_if<
!std::is_same<invoke_result_t<F>, void>::value,
......
......@@ -150,42 +150,44 @@ class Try {
std::is_nothrow_constructible<exception_wrapper, Args&&...>::value);
/*
* Get a mutable reference to the contained value. If the Try contains an
* exception it will be rethrown.
* Get a mutable reference to the contained value.
* [Re]throws if the Try contains an exception or is empty.
*
* @returns mutable reference to the contained value
*/
T& value() &;
/*
* Get a rvalue reference to the contained value. If the Try contains an
* exception it will be rethrown.
* Get a rvalue reference to the contained value.
* [Re]throws if the Try contains an exception or is empty.
*
* @returns rvalue reference to the contained value
*/
T&& value() &&;
/*
* Get a const reference to the contained value. If the Try contains an
* exception it will be rethrown.
* Get a const reference to the contained value.
* [Re]throws if the Try contains an exception or is empty.
*
* @returns const reference to the contained value
*/
const T& value() const&;
/*
* Get a const rvalue reference to the contained value. If the Try contains an
* exception it will be rethrown.
* Get a const rvalue reference to the contained value.
* [Re]throws if the Try contains an exception or is empty.
*
* @returns const rvalue reference to the contained value
*/
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
* rethrown.
* Const dereference operator.
* [Re]throws if the Try contains an exception or is empty.
*
* @returns const reference to the contained value
*/
......@@ -212,8 +214,8 @@ class Try {
const T&& operator*() const&& { return std::move(value()); }
/*
* Const arrow operator. If the Try contains an exception it will be
* rethrown.
* Const arrow operator.
* [Re]throws if the Try contains an exception or is empty.
*
* @returns const reference to the contained value
*/
......@@ -438,6 +440,7 @@ class Try<void> {
// If the Try contains an exception, throws it
inline void throwIfFailed() const;
inline void throwUnlessValue() const;
// @returns False if the Try contains an exception, true otherwise
bool hasValue() const { return hasValue_; }
......
......@@ -1049,13 +1049,14 @@ TEST(Future, throwCaughtInImmediateThen) {
TEST(Future, throwIfFailed) {
makeFuture<Unit>(eggs).then(
[=](Try<Unit>&& t) { EXPECT_THROW(t.throwIfFailed(), eggs_t); });
makeFuture().then([=](Try<Unit>&& t) { EXPECT_NO_THROW(t.throwIfFailed()); });
[=](Try<Unit>&& t) { EXPECT_THROW(t.throwUnlessValue(), eggs_t); });
makeFuture().then(
[=](Try<Unit>&& t) { EXPECT_NO_THROW(t.throwUnlessValue()); });
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(
[=](Try<int>&& t) { EXPECT_NO_THROW(t.throwIfFailed()); });
[=](Try<int>&& t) { EXPECT_NO_THROW(t.throwUnlessValue()); });
}
TEST(Future, getFutureAfterSetValue) {
......
......@@ -110,7 +110,7 @@ TEST(RetryingTest, future_factory_throws) {
})
.wait()
.result();
EXPECT_THROW(result.throwIfFailed(), ThrownException);
EXPECT_THROW(result.throwUnlessValue(), ThrownException);
}
TEST(RetryingTest, future_factory_throws_unsafe) {
......@@ -131,7 +131,7 @@ TEST(RetryingTest, future_factory_throws_unsafe) {
})
.wait()
.result();
EXPECT_THROW(result.throwIfFailed(), ThrownException);
EXPECT_THROW(result.throwUnlessValue(), ThrownException);
}
TEST(RetryingTest, policy_throws) {
......
......@@ -963,7 +963,7 @@ TEST(SemiFuture, semiFutureWithinNoValueReferenceWhenTimeOut) {
auto f = promise.getSemiFuture().within(veryShort).toUnsafeFuture().thenTry(
[](folly::Try<std::shared_ptr<int>>&& callbackInput) {
EXPECT_THROW(callbackInput.throwIfFailed(), FutureTimeout);
EXPECT_THROW(callbackInput.throwUnlessValue(), FutureTimeout);
});
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