Commit fe4c0d46 authored by Spencer Baumgardner's avatar Spencer Baumgardner Committed by Facebook GitHub Bot

add support for reference types

Summary:
This diff adds support for reference types to the Async abstraction.

One example use-case is memoization where you generate the value asynchronously on cache-miss. You want may want to return a reference to avoid the expense of copying.

Some additional context is in D21438851.

Reviewed By: yfeldblum

Differential Revision: D21478238

fbshipit-source-id: 53e1b997abed2d40f0a2f9ab935925ad9bbc91ab
parent 0ef1daa2
......@@ -103,7 +103,7 @@ class [[nodiscard]] Async<void> {
template <typename T>
T&& await(Async<T>&& async) {
DCHECK(detail::onFiber());
return std::move(async.val_);
return static_cast<T&&>(async.val_);
}
inline void await(Async<void>&&) {
......
......@@ -2618,6 +2618,20 @@ async::Async<folly::Optional<std::string>> getOptionalAsyncString() {
async::Async<std::tuple<int, float, std::string>> getTuple() {
return {0, 0.0, "0"};
}
struct NonCopyableNonMoveable {
NonCopyableNonMoveable(const NonCopyableNonMoveable&) = delete;
NonCopyableNonMoveable(NonCopyableNonMoveable&&) = delete;
NonCopyableNonMoveable& operator=(NonCopyableNonMoveable const&) = delete;
NonCopyableNonMoveable& operator=(NonCopyableNonMoveable&&) = delete;
};
async::Async<NonCopyableNonMoveable const&> getReference() {
thread_local NonCopyableNonMoveable const value{};
return value;
}
} // namespace
TEST(FiberManager, asyncAwait) {
......@@ -2630,6 +2644,10 @@ TEST(FiberManager, asyncAwait) {
EXPECT_EQ(getString(), async::init_await(getAsyncString()));
EXPECT_EQ(getString(), *async::init_await(getOptionalAsyncString()));
async::init_await(getTuple());
decltype(auto) ref = async::init_await(getReference());
static_assert(
std::is_same<decltype(ref), NonCopyableNonMoveable const&>::value,
"");
})
.getVia(&evb));
}
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