Commit 64fc8458 authored by Shai Szulanski's avatar Shai Szulanski Committed by Facebook Github Bot

Add coro::toSemiFuture

Summary:
Currently the best way to get an eager SemiFuture from an AsyncGenerator is
  folly::coro::co_invoke(
      [&]()
          -> folly::coro::Task<folly::coro::AsyncGenerator<T&&>::NextResult> {
        co_return co_await gen.next();
      })
      .scheduleOn(&ex)
      .start();
which is very verbose. This diff adds helpers to make this a simple function call.

Reviewed By: andriigrynenko

Differential Revision: D19835473

fbshipit-source-id: f6e7e72cad0e0596df375d2662c87d4412536c03
parent 0b5752df
...@@ -240,6 +240,20 @@ struct remove_cvref { ...@@ -240,6 +240,20 @@ struct remove_cvref {
template <typename T> template <typename T>
using remove_cvref_t = typename remove_cvref<T>::type; using remove_cvref_t = typename remove_cvref<T>::type;
/**
* A type trait to unwrap a std::reference_wrapper<T> to a type T
*/
template <typename T>
struct remove_reference_wrapper {
using type = T;
};
template <typename T>
struct remove_reference_wrapper<std::reference_wrapper<T>> {
using type = T;
};
template <typename T>
using remove_reference_wrapper_t = typename remove_reference_wrapper<T>::type;
namespace detail { namespace detail {
template <typename Src> template <typename Src>
struct like_ { struct like_ {
......
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <folly/experimental/coro/Invoke.h>
#include <folly/experimental/coro/Task.h>
#include <folly/experimental/coro/Traits.h>
namespace folly {
namespace coro {
// Converts the given SemiAwaitable to a Task (without starting it)
template <typename SemiAwaitable>
Task<semi_await_result_t<SemiAwaitable>> toTask(SemiAwaitable&& a) {
return co_invoke(
[a = std::forward<SemiAwaitable>(
a)]() mutable -> Task<semi_await_result_t<SemiAwaitable>> {
co_return co_await std::forward<SemiAwaitable>(a);
});
}
template <typename SemiAwaitable>
Task<semi_await_result_t<SemiAwaitable>> toTask(
std::reference_wrapper<SemiAwaitable> a) {
return co_invoke(
[a = std::move(a)]() mutable -> Task<semi_await_result_t<SemiAwaitable>> {
co_return co_await a.get();
});
}
// Converts the given SemiAwaitable to a SemiFuture (without starting it)
template <typename SemiAwaitable>
SemiFuture<
lift_unit_t<semi_await_result_t<remove_reference_wrapper_t<SemiAwaitable>>>>
toSemiFuture(SemiAwaitable&& a) {
return toTask(std::forward<SemiAwaitable>(a)).semi();
}
// Converts the given SemiAwaitable to a Future, starting it on the Executor
template <typename SemiAwaitable>
Future<
lift_unit_t<semi_await_result_t<remove_reference_wrapper_t<SemiAwaitable>>>>
toFuture(SemiAwaitable&& a, Executor::KeepAlive<> ex) {
return toTask(std::forward<SemiAwaitable>(a)).scheduleOn(ex).start().via(ex);
}
} // namespace coro
} // namespace folly
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <folly/Portability.h>
#if FOLLY_HAS_COROUTINES
#include <folly/executors/ManualExecutor.h>
#include <folly/experimental/coro/AsyncGenerator.h>
#include <folly/experimental/coro/Baton.h>
#include <folly/experimental/coro/BlockingWait.h>
#include <folly/experimental/coro/FutureUtil.h>
#include <folly/experimental/coro/Task.h>
#include <folly/portability/GTest.h>
static folly::coro::Task<int> makeTask() {
co_return 42;
}
static folly::coro::AsyncGenerator<int> makeGen() {
co_yield 42;
}
TEST(FutureUtilTest, ToTask) {
EXPECT_EQ(folly::coro::blockingWait(folly::coro::toTask(makeTask())), 42);
auto gen = makeGen();
EXPECT_EQ(*folly::coro::blockingWait(folly::coro::toTask(gen.next())), 42);
folly::coro::Baton baton;
auto task = folly::coro::toTask(std::ref(baton));
baton.post();
folly::coro::blockingWait(std::move(task));
}
TEST(FutureUtilTest, ToSemiFuture) {
folly::ManualExecutor ex;
auto semi = folly::coro::toSemiFuture(makeTask());
EXPECT_FALSE(semi.isReady());
semi = std::move(semi).via(&ex);
EXPECT_FALSE(semi.isReady());
ex.drain();
EXPECT_TRUE(semi.isReady());
EXPECT_EQ(std::move(semi).get(), 42);
auto gen = makeGen();
auto semi2 = folly::coro::toSemiFuture(gen.next());
EXPECT_FALSE(semi2.isReady());
semi2 = std::move(semi2).via(&ex);
EXPECT_FALSE(semi2.isReady());
ex.drain();
EXPECT_TRUE(semi2.isReady());
EXPECT_EQ(*std::move(semi2).get(), 42);
folly::coro::Baton baton;
auto semi3 = folly::coro::toSemiFuture(std::ref(baton));
EXPECT_FALSE(semi3.isReady());
semi3 = std::move(semi3).via(&ex);
EXPECT_FALSE(semi3.isReady());
ex.drain();
EXPECT_FALSE(semi3.isReady());
baton.post();
ex.drain();
EXPECT_TRUE(semi3.isReady());
}
TEST(FutureUtilTest, ToFuture) {
folly::ManualExecutor ex;
auto fut = folly::coro::toFuture(makeTask(), &ex);
EXPECT_FALSE(fut.isReady());
ex.drain();
EXPECT_TRUE(fut.isReady());
EXPECT_EQ(std::move(fut).get(), 42);
auto gen = makeGen();
auto fut2 = folly::coro::toFuture(gen.next(), &ex);
EXPECT_FALSE(fut2.isReady());
ex.drain();
EXPECT_TRUE(fut2.isReady());
EXPECT_EQ(*std::move(fut2).get(), 42);
folly::coro::Baton baton;
auto fut3 = folly::coro::toFuture(std::ref(baton), &ex);
EXPECT_FALSE(fut3.isReady());
ex.drain();
EXPECT_FALSE(fut3.isReady());
baton.post();
ex.drain();
EXPECT_TRUE(fut3.isReady());
}
#endif
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