Commit 02177495 authored by Andrii Grynenko's avatar Andrii Grynenko Committed by Facebook GitHub Bot

Use coro::* instead of std::experimental::*

Summary: Depending on the version of the std library, coro types may be in std or std::experimental namespace. Use coro namespace instead to make switching library implementation easier.

Reviewed By: yfeldblum

Differential Revision: D26744795

fbshipit-source-id: 8856b901d4d757bf378acb56f236135feb0c5488
parent 6aa5e27b
...@@ -1324,7 +1324,7 @@ bool operator>(const Value& other, const Expected<Value, Error>&) = delete; ...@@ -1324,7 +1324,7 @@ bool operator>(const Value& other, const Expected<Value, Error>&) = delete;
// Enable the use of folly::Expected with `co_await` // Enable the use of folly::Expected with `co_await`
// Inspired by https://github.com/toby-allsopp/coroutine_monad // Inspired by https://github.com/toby-allsopp/coroutine_monad
#if FOLLY_HAS_COROUTINES #if FOLLY_HAS_COROUTINES
#include <experimental/coroutine> #include <folly/experimental/coro/Coroutine.h>
namespace folly { namespace folly {
namespace expected_detail { namespace expected_detail {
...@@ -1357,10 +1357,8 @@ struct Promise { ...@@ -1357,10 +1357,8 @@ struct Promise {
// or: // or:
// auto retobj = p.get_return_object(); // clang // auto retobj = p.get_return_object(); // clang
PromiseReturn<Value, Error> get_return_object() noexcept { return *this; } PromiseReturn<Value, Error> get_return_object() noexcept { return *this; }
std::experimental::suspend_never initial_suspend() const noexcept { coro::suspend_never initial_suspend() const noexcept { return {}; }
return {}; coro::suspend_never final_suspend() const noexcept { return {}; }
}
std::experimental::suspend_never final_suspend() const noexcept { return {}; }
template <typename U = Value> template <typename U = Value>
void return_value(U&& u) { void return_value(U&& u) {
value_->emplace(static_cast<U&&>(u)); value_->emplace(static_cast<U&&>(u));
...@@ -1383,7 +1381,7 @@ struct Awaitable { ...@@ -1383,7 +1381,7 @@ struct Awaitable {
// Explicitly only allow suspension into a Promise // Explicitly only allow suspension into a Promise
template <typename U> template <typename U>
void await_suspend(std::experimental::coroutine_handle<Promise<U, Error>> h) { void await_suspend(coro::coroutine_handle<Promise<U, Error>> h) {
*h.promise().value_ = makeUnexpected(std::move(o_.error())); *h.promise().value_ = makeUnexpected(std::move(o_.error()));
// Abort the rest of the coroutine. resume() is not going to be called // Abort the rest of the coroutine. resume() is not going to be called
h.destroy(); h.destroy();
......
...@@ -611,7 +611,7 @@ FOLLY_NAMESPACE_STD_END ...@@ -611,7 +611,7 @@ FOLLY_NAMESPACE_STD_END
// Enable the use of folly::Optional with `co_await` // Enable the use of folly::Optional with `co_await`
// Inspired by https://github.com/toby-allsopp/coroutine_monad // Inspired by https://github.com/toby-allsopp/coroutine_monad
#if FOLLY_HAS_COROUTINES #if FOLLY_HAS_COROUTINES
#include <experimental/coroutine> #include <folly/experimental/coro/Coroutine.h>
namespace folly { namespace folly {
namespace detail { namespace detail {
...@@ -642,10 +642,8 @@ struct OptionalPromise { ...@@ -642,10 +642,8 @@ struct OptionalPromise {
// or: // or:
// auto retobj = p.get_return_object(); // clang // auto retobj = p.get_return_object(); // clang
OptionalPromiseReturn<Value> get_return_object() noexcept { return *this; } OptionalPromiseReturn<Value> get_return_object() noexcept { return *this; }
std::experimental::suspend_never initial_suspend() const noexcept { coro::suspend_never initial_suspend() const noexcept { return {}; }
return {}; coro::suspend_never final_suspend() const noexcept { return {}; }
}
std::experimental::suspend_never final_suspend() const noexcept { return {}; }
template <typename U = Value> template <typename U = Value>
void return_value(U&& u) { void return_value(U&& u) {
*value_ = static_cast<U&&>(u); *value_ = static_cast<U&&>(u);
...@@ -665,8 +663,7 @@ struct OptionalAwaitable { ...@@ -665,8 +663,7 @@ struct OptionalAwaitable {
// Explicitly only allow suspension into an OptionalPromise // Explicitly only allow suspension into an OptionalPromise
template <typename U> template <typename U>
void await_suspend( void await_suspend(coro::coroutine_handle<OptionalPromise<U>> h) const {
std::experimental::coroutine_handle<OptionalPromise<U>> h) const {
// Abort the rest of the coroutine. resume() is not going to be called // Abort the rest of the coroutine. resume() is not going to be called
h.destroy(); h.destroy();
} }
......
...@@ -80,7 +80,7 @@ class Barrier { ...@@ -80,7 +80,7 @@ class Barrier {
} }
return std::exchange(continuation_, {}); return std::exchange(continuation_, {});
} else { } else {
return noop_coroutine(); return coro::noop_coroutine();
} }
} }
...@@ -95,12 +95,12 @@ class Barrier { ...@@ -95,12 +95,12 @@ class Barrier {
auto coro = std::exchange(continuation_, {}); auto coro = std::exchange(continuation_, {});
if (asyncFrame_ != nullptr) { if (asyncFrame_ != nullptr) {
folly::resumeCoroutineWithNewAsyncStackRoot(coro, *asyncFrame_); folly::resumeCoroutineWithNewAsyncStackRoot(coro, *asyncFrame_);
return noop_coroutine(); return coro::noop_coroutine();
} else { } else {
return coro; return coro;
} }
} else { } else {
return noop_coroutine(); return coro::noop_coroutine();
} }
} }
......
...@@ -103,7 +103,7 @@ struct TrickyAwaitable { ...@@ -103,7 +103,7 @@ struct TrickyAwaitable {
bool await_ready() const { return false; } bool await_ready() const { return false; }
bool await_suspend(std::experimental::coroutine_handle<>) { bool await_suspend(folly::coro::coroutine_handle<>) {
value_ = std::make_unique<int>(42); value_ = std::make_unique<int>(42);
return false; return false;
} }
...@@ -144,7 +144,7 @@ class SimplePromise { ...@@ -144,7 +144,7 @@ class SimplePromise {
bool await_ready() { return awaiter_.await_ready(); } bool await_ready() { return awaiter_.await_ready(); }
template <typename Promise> template <typename Promise>
auto await_suspend(std::experimental::coroutine_handle<Promise> h) { auto await_suspend(folly::coro::coroutine_handle<Promise> h) {
return awaiter_.await_suspend(h); return awaiter_.await_suspend(h);
} }
......
...@@ -28,9 +28,9 @@ class Wait { ...@@ -28,9 +28,9 @@ class Wait {
public: public:
Wait get_return_object() { return Wait(promise_.get_future()); } Wait get_return_object() { return Wait(promise_.get_future()); }
std::experimental::suspend_never initial_suspend() noexcept { return {}; } folly::coro::suspend_never initial_suspend() noexcept { return {}; }
std::experimental::suspend_never final_suspend() noexcept { return {}; } folly::coro::suspend_never final_suspend() noexcept { return {}; }
void return_void() { promise_.set_value(); } void return_void() { promise_.set_value(); }
...@@ -69,16 +69,15 @@ class InlineTask { ...@@ -69,16 +69,15 @@ class InlineTask {
bool await_ready() const { return false; } bool await_ready() const { return false; }
std::experimental::coroutine_handle<> await_suspend( folly::coro::coroutine_handle<> await_suspend(
std::experimental::coroutine_handle<> awaiter) { folly::coro::coroutine_handle<> awaiter) {
promise_->valuePtr_ = &value_; promise_->valuePtr_ = &value_;
promise_->awaiter_ = std::move(awaiter); promise_->awaiter_ = std::move(awaiter);
return std::experimental::coroutine_handle<promise_type>::from_promise( return folly::coro::coroutine_handle<promise_type>::from_promise(*promise_);
*promise_);
} }
T await_resume() { T await_resume() {
std::experimental::coroutine_handle<promise_type>::from_promise( folly::coro::coroutine_handle<promise_type>::from_promise(
*std::exchange(promise_, nullptr)) *std::exchange(promise_, nullptr))
.destroy(); .destroy();
T value = std::move(value_); T value = std::move(value_);
...@@ -96,14 +95,14 @@ class InlineTask { ...@@ -96,14 +95,14 @@ class InlineTask {
void unhandled_exception() { std::terminate(); } void unhandled_exception() { std::terminate(); }
std::experimental::suspend_always initial_suspend() { return {}; } folly::coro::suspend_always initial_suspend() { return {}; }
class FinalSuspender { class FinalSuspender {
public: public:
bool await_ready() noexcept { return false; } bool await_ready() noexcept { return false; }
auto await_suspend( auto await_suspend(
std::experimental::coroutine_handle<promise_type> h) noexcept { folly::coro::coroutine_handle<promise_type> h) noexcept {
return h.promise().awaiter_; return h.promise().awaiter_;
} }
...@@ -116,7 +115,7 @@ class InlineTask { ...@@ -116,7 +115,7 @@ class InlineTask {
friend class InlineTask; friend class InlineTask;
T* valuePtr_; T* valuePtr_;
std::experimental::coroutine_handle<> awaiter_; folly::coro::coroutine_handle<> awaiter_;
}; };
private: private:
...@@ -161,16 +160,15 @@ class InlineTaskAllocator { ...@@ -161,16 +160,15 @@ class InlineTaskAllocator {
bool await_ready() const { return false; } bool await_ready() const { return false; }
std::experimental::coroutine_handle<> await_suspend( folly::coro::coroutine_handle<> await_suspend(
std::experimental::coroutine_handle<> awaiter) { folly::coro::coroutine_handle<> awaiter) {
promise_->valuePtr_ = &value_; promise_->valuePtr_ = &value_;
promise_->awaiter_ = std::move(awaiter); promise_->awaiter_ = std::move(awaiter);
return std::experimental::coroutine_handle<promise_type>::from_promise( return folly::coro::coroutine_handle<promise_type>::from_promise(*promise_);
*promise_);
} }
T await_resume() { T await_resume() {
std::experimental::coroutine_handle<promise_type>::from_promise( folly::coro::coroutine_handle<promise_type>::from_promise(
*std::exchange(promise_, nullptr)) *std::exchange(promise_, nullptr))
.destroy(); .destroy();
T value = std::move(value_); T value = std::move(value_);
...@@ -205,14 +203,14 @@ class InlineTaskAllocator { ...@@ -205,14 +203,14 @@ class InlineTaskAllocator {
[[noreturn]] void unhandled_exception() noexcept { std::terminate(); } [[noreturn]] void unhandled_exception() noexcept { std::terminate(); }
std::experimental::suspend_always initial_suspend() noexcept { return {}; } folly::coro::suspend_always initial_suspend() noexcept { return {}; }
class FinalSuspender { class FinalSuspender {
public: public:
bool await_ready() noexcept { return false; } bool await_ready() noexcept { return false; }
auto await_suspend( auto await_suspend(
std::experimental::coroutine_handle<promise_type> h) noexcept { folly::coro::coroutine_handle<promise_type> h) noexcept {
return h.promise().awaiter_; return h.promise().awaiter_;
} }
...@@ -225,7 +223,7 @@ class InlineTaskAllocator { ...@@ -225,7 +223,7 @@ class InlineTaskAllocator {
friend class InlineTaskAllocator; friend class InlineTaskAllocator;
T* valuePtr_; T* valuePtr_;
std::experimental::coroutine_handle<> awaiter_; folly::coro::coroutine_handle<> awaiter_;
}; };
private: private:
......
...@@ -37,9 +37,9 @@ class Wait { ...@@ -37,9 +37,9 @@ class Wait {
public: public:
Wait get_return_object() { return Wait(promise_.get_future()); } Wait get_return_object() { return Wait(promise_.get_future()); }
std::experimental::suspend_never initial_suspend() noexcept { return {}; } folly::coro::suspend_never initial_suspend() noexcept { return {}; }
std::experimental::suspend_never final_suspend() noexcept { return {}; } folly::coro::suspend_never final_suspend() noexcept { return {}; }
void return_void() { promise_.set_value(); } void return_void() { promise_.set_value(); }
...@@ -78,16 +78,15 @@ class InlineTask { ...@@ -78,16 +78,15 @@ class InlineTask {
bool await_ready() const { return false; } bool await_ready() const { return false; }
std::experimental::coroutine_handle<> await_suspend( folly::coro::coroutine_handle<> await_suspend(
std::experimental::coroutine_handle<> awaiter) { folly::coro::coroutine_handle<> awaiter) {
promise_->valuePtr_ = &value_; promise_->valuePtr_ = &value_;
promise_->awaiter_ = std::move(awaiter); promise_->awaiter_ = std::move(awaiter);
return std::experimental::coroutine_handle<promise_type>::from_promise( return folly::coro::coroutine_handle<promise_type>::from_promise(*promise_);
*promise_);
} }
T await_resume() { T await_resume() {
std::experimental::coroutine_handle<promise_type>::from_promise( folly::coro::coroutine_handle<promise_type>::from_promise(
*std::exchange(promise_, nullptr)) *std::exchange(promise_, nullptr))
.destroy(); .destroy();
T value = std::move(value_); T value = std::move(value_);
...@@ -105,24 +104,23 @@ class InlineTask { ...@@ -105,24 +104,23 @@ class InlineTask {
void unhandled_exception() { std::terminate(); } void unhandled_exception() { std::terminate(); }
std::experimental::suspend_always initial_suspend() { return {}; } folly::coro::suspend_always initial_suspend() { return {}; }
class FinalSuspender { class FinalSuspender {
public: public:
explicit FinalSuspender( explicit FinalSuspender(folly::coro::coroutine_handle<> awaiter) noexcept
std::experimental::coroutine_handle<> awaiter) noexcept
: awaiter_(std::move(awaiter)) {} : awaiter_(std::move(awaiter)) {}
bool await_ready() noexcept { return false; } bool await_ready() noexcept { return false; }
auto await_suspend(std::experimental::coroutine_handle<>) noexcept { auto await_suspend(folly::coro::coroutine_handle<>) noexcept {
return awaiter_; return awaiter_;
} }
void await_resume() noexcept {} void await_resume() noexcept {}
private: private:
std::experimental::coroutine_handle<> awaiter_; folly::coro::coroutine_handle<> awaiter_;
}; };
FinalSuspender final_suspend() noexcept { FinalSuspender final_suspend() noexcept {
...@@ -133,7 +131,7 @@ class InlineTask { ...@@ -133,7 +131,7 @@ class InlineTask {
friend class InlineTask; friend class InlineTask;
T* valuePtr_; T* valuePtr_;
std::experimental::coroutine_handle<> awaiter_; folly::coro::coroutine_handle<> awaiter_;
}; };
private: private:
......
...@@ -362,7 +362,7 @@ template <int value> ...@@ -362,7 +362,7 @@ template <int value>
struct AwaitableInt { struct AwaitableInt {
bool await_ready() const { return true; } bool await_ready() const { return true; }
bool await_suspend(std::experimental::coroutine_handle<>) { bool await_suspend(coro::coroutine_handle<>) {
LOG(FATAL) << "Should never be called."; LOG(FATAL) << "Should never be called.";
} }
......
...@@ -200,7 +200,7 @@ TEST_F(InlineTaskTest, ExceptionsPropagateFromVoidTask) { ...@@ -200,7 +200,7 @@ TEST_F(InlineTaskTest, ExceptionsPropagateFromVoidTask) {
struct MyException : std::exception {}; struct MyException : std::exception {};
auto f = []() -> InlineTask<void> { auto f = []() -> InlineTask<void> {
co_await std::experimental::suspend_never{}; co_await folly::coro::suspend_never{};
throw MyException{}; throw MyException{};
}; };
EXPECT_THROW(folly::coro::blockingWait(f()), MyException); EXPECT_THROW(folly::coro::blockingWait(f()), MyException);
...@@ -210,7 +210,7 @@ TEST_F(InlineTaskTest, ExceptionsPropagateFromValueTask) { ...@@ -210,7 +210,7 @@ TEST_F(InlineTaskTest, ExceptionsPropagateFromValueTask) {
struct MyException : std::exception {}; struct MyException : std::exception {};
auto f = []() -> InlineTask<int> { auto f = []() -> InlineTask<int> {
co_await std::experimental::suspend_never{}; co_await folly::coro::suspend_never{};
throw MyException{}; throw MyException{};
}; };
EXPECT_THROW(folly::coro::blockingWait(f()), MyException); EXPECT_THROW(folly::coro::blockingWait(f()), MyException);
...@@ -220,7 +220,7 @@ TEST_F(InlineTaskTest, ExceptionsPropagateFromRefTask) { ...@@ -220,7 +220,7 @@ TEST_F(InlineTaskTest, ExceptionsPropagateFromRefTask) {
struct MyException : std::exception {}; struct MyException : std::exception {};
auto f = []() -> InlineTask<int&> { auto f = []() -> InlineTask<int&> {
co_await std::experimental::suspend_never{}; co_await folly::coro::suspend_never{};
throw MyException{}; throw MyException{};
}; };
EXPECT_THROW(folly::coro::blockingWait(f()), MyException); EXPECT_THROW(folly::coro::blockingWait(f()), MyException);
......
...@@ -16,8 +16,8 @@ ...@@ -16,8 +16,8 @@
#include <folly/Portability.h> #include <folly/Portability.h>
#include <folly/experimental/coro/Coroutine.h>
#include <folly/experimental/coro/Traits.h> #include <folly/experimental/coro/Traits.h>
#include <experimental/coroutine>
#include <type_traits> #include <type_traits>
#if FOLLY_HAS_COROUTINES #if FOLLY_HAS_COROUTINES
...@@ -27,39 +27,38 @@ using namespace folly::coro; ...@@ -27,39 +27,38 @@ using namespace folly::coro;
template <typename T> template <typename T>
struct SomeAwaiter1 { struct SomeAwaiter1 {
bool await_ready(); bool await_ready();
void await_suspend(std::experimental::coroutine_handle<>); void await_suspend(coroutine_handle<>);
T await_resume(); T await_resume();
}; };
template <typename T> template <typename T>
struct SomeAwaiter2 { struct SomeAwaiter2 {
bool await_ready(); bool await_ready();
bool await_suspend(std::experimental::coroutine_handle<>); bool await_suspend(coroutine_handle<>);
T await_resume(); T await_resume();
}; };
template <typename T> template <typename T>
struct SomeAwaiter3 { struct SomeAwaiter3 {
bool await_ready(); bool await_ready();
std::experimental::coroutine_handle<> await_suspend( coroutine_handle<> await_suspend(coroutine_handle<>);
std::experimental::coroutine_handle<>);
T await_resume(); T await_resume();
}; };
struct MissingAwaitReady { struct MissingAwaitReady {
void await_suspend(std::experimental::coroutine_handle<>); void await_suspend(coroutine_handle<>);
int await_resume(); int await_resume();
}; };
struct WrongAwaitReadyReturnType { struct WrongAwaitReadyReturnType {
void* await_ready(); void* await_ready();
void await_suspend(std::experimental::coroutine_handle<>); void await_suspend(coroutine_handle<>);
int await_resume(); int await_resume();
}; };
struct MissingAwaitResume { struct MissingAwaitResume {
bool await_ready(); bool await_ready();
void await_suspend(std::experimental::coroutine_handle<void>); void await_suspend(coroutine_handle<void>);
}; };
struct MemberOperatorCoAwait { struct MemberOperatorCoAwait {
......
...@@ -274,14 +274,14 @@ class BatonAwaitableWaiter : public Baton::Waiter { ...@@ -274,14 +274,14 @@ class BatonAwaitableWaiter : public Baton::Waiter {
void await_resume() {} void await_resume() {}
void await_suspend(std::experimental::coroutine_handle<> h) { void await_suspend(coro::coroutine_handle<> h) {
assert(!h_); assert(!h_);
h_ = std::move(h); h_ = std::move(h);
baton_.setWaiter(*this); baton_.setWaiter(*this);
} }
private: private:
std::experimental::coroutine_handle<> h_; coro::coroutine_handle<> h_;
Baton& baton_; Baton& baton_;
}; };
} // namespace detail } // namespace detail
......
...@@ -2611,7 +2611,7 @@ class FutureAwaiter { ...@@ -2611,7 +2611,7 @@ class FutureAwaiter {
} }
FOLLY_CORO_AWAIT_SUSPEND_NONTRIVIAL_ATTRIBUTES void await_suspend( FOLLY_CORO_AWAIT_SUSPEND_NONTRIVIAL_ATTRIBUTES void await_suspend(
std::experimental::coroutine_handle<> h) { coro::coroutine_handle<> h) {
// FutureAwaiter may get destroyed as soon as the callback is executed. // FutureAwaiter may get destroyed as soon as the callback is executed.
// Make sure the future object doesn't get destroyed until setCallback_ // Make sure the future object doesn't get destroyed until setCallback_
// returns. // returns.
......
...@@ -71,7 +71,7 @@ inline void popAsyncStackFrameCallee( ...@@ -71,7 +71,7 @@ inline void popAsyncStackFrameCallee(
template <typename Promise> template <typename Promise>
void resumeCoroutineWithNewAsyncStackRoot( void resumeCoroutineWithNewAsyncStackRoot(
std::experimental::coroutine_handle<Promise> h) noexcept { coro::coroutine_handle<Promise> h) noexcept {
resumeCoroutineWithNewAsyncStackRoot(h, h.promise().getAsyncFrame()); resumeCoroutineWithNewAsyncStackRoot(h, h.promise().getAsyncFrame());
} }
......
...@@ -172,8 +172,7 @@ AsyncStackFrame& getDetachedRootAsyncStackFrame() noexcept { ...@@ -172,8 +172,7 @@ AsyncStackFrame& getDetachedRootAsyncStackFrame() noexcept {
#if FOLLY_HAS_COROUTINES #if FOLLY_HAS_COROUTINES
FOLLY_NOINLINE void resumeCoroutineWithNewAsyncStackRoot( FOLLY_NOINLINE void resumeCoroutineWithNewAsyncStackRoot(
std::experimental::coroutine_handle<> h, coro::coroutine_handle<> h, folly::AsyncStackFrame& frame) noexcept {
folly::AsyncStackFrame& frame) noexcept {
detail::ScopedAsyncStackRoot root; detail::ScopedAsyncStackRoot root;
root.activateFrame(frame); root.activateFrame(frame);
h.resume(); h.resume();
......
...@@ -257,7 +257,7 @@ AsyncStackFrame& getDetachedRootAsyncStackFrame() noexcept; ...@@ -257,7 +257,7 @@ AsyncStackFrame& getDetachedRootAsyncStackFrame() noexcept;
// on the current thread and setting the specified AsyncStackFrame as // on the current thread and setting the specified AsyncStackFrame as
// the current async frame. // the current async frame.
FOLLY_NOINLINE void resumeCoroutineWithNewAsyncStackRoot( FOLLY_NOINLINE void resumeCoroutineWithNewAsyncStackRoot(
std::experimental::coroutine_handle<> h, AsyncStackFrame& frame) noexcept; coro::coroutine_handle<> h, AsyncStackFrame& frame) noexcept;
// Resume the specified coroutine after installing a new AsyncStackRoot // Resume the specified coroutine after installing a new AsyncStackRoot
// on the current thread and setting the coroutine's associated // on the current thread and setting the coroutine's associated
...@@ -265,7 +265,7 @@ FOLLY_NOINLINE void resumeCoroutineWithNewAsyncStackRoot( ...@@ -265,7 +265,7 @@ FOLLY_NOINLINE void resumeCoroutineWithNewAsyncStackRoot(
// current async frame. // current async frame.
template <typename Promise> template <typename Promise>
void resumeCoroutineWithNewAsyncStackRoot( void resumeCoroutineWithNewAsyncStackRoot(
std::experimental::coroutine_handle<Promise> h) noexcept; coro::coroutine_handle<Promise> h) noexcept;
#endif // FOLLY_HAS_COROUTINES #endif // FOLLY_HAS_COROUTINES
......
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