Commit 13bccbbf authored by Dylan Yudaken's avatar Dylan Yudaken Committed by Facebook GitHub Bot

Support returning move-only types in folly::Expected::then

Summary: the codepath for .then is copying the return value in one place, which prevents using move-only types (and might incur extra costs).

Reviewed By: yfeldblum

Differential Revision: D29159637

fbshipit-source-id: 892b73266cfe45c9e09b9b648d7b7703871c4323
parent b194210a
......@@ -499,16 +499,18 @@ inline T&& operator,(T&& t, Unit) noexcept {
struct ExpectedHelper {
template <class Error, class T>
static constexpr Expected<T, Error> return_(T t) {
return folly::makeExpected<Error>(t);
static constexpr Expected<typename std::decay<T>::type, Error> return_(
T&& t) {
return folly::makeExpected<Error>(static_cast<T&&>(t));
}
template <
class Error,
class T,
class U FOLLY_REQUIRES_TRAILING(
expected_detail::IsConvertible<U&&, Error>::value)>
static constexpr Expected<T, Error> return_(Expected<T, U> t) {
return t;
static constexpr Expected<T, Error> return_(Expected<T, U>&& t) {
return Expected<T, Error>(static_cast<Expected<T, U>&&>(t));
}
template <class This>
......
......@@ -877,4 +877,20 @@ TEST(Expected, ConstructorConstructibleNotConvertible) {
ce = e;
}
}
TEST(Expected, TestUnique) {
auto mk = []() -> Expected<std::unique_ptr<int>, int> {
return std::make_unique<int>(1);
};
EXPECT_EQ(
2, **mk().then([](auto r) { return std::make_unique<int>(*r + 1); }));
// Test converting errors works
EXPECT_EQ(
2, **mk().then([](auto r) -> Expected<std::unique_ptr<int>, double> {
return std::make_unique<int>(*r + 1);
}));
}
} // namespace folly
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