Commit fbeb95ac authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Promise<T>::makeEmpty() and Future<T>::makeEmpty()

Summary:
[Folly] `Promise<T>::makeEmpty()` and `Future<T>::makeEmpty()`.

These can currently be done by creating, and then moving away from, regular promises and futures. But that allocates; this is semantically equivalent but more efficient.

Differential Revision: D5368339

fbshipit-source-id: de054cfc75c701c5d39aac64d9a2949cd34b1896
parent 70f35fe3
......@@ -111,7 +111,7 @@ class CoreCallbackState {
union {
F func_;
};
Promise<T> promise_{detail::EmptyConstruct{}};
Promise<T> promise_{Promise<T>::makeEmpty()};
};
template <typename T, typename F>
......@@ -124,6 +124,11 @@ inline auto makeCoreCallbackState(Promise<T>&& p, F&& f) noexcept(
}
}
template <class T>
Future<T> Future<T>::makeEmpty() {
return Future<T>(detail::EmptyConstruct{});
}
template <class T>
Future<T>::Future(Future<T>&& other) noexcept : core_(other.core_) {
other.core_ = nullptr;
......@@ -559,6 +564,10 @@ void Future<T>::raise(exception_wrapper exception) {
core_->raise(std::move(exception));
}
template <class T>
Future<T>::Future(detail::EmptyConstruct) noexcept
: core_(nullptr) {}
// makeFuture
template <class T>
......
......@@ -45,6 +45,8 @@ class Future {
public:
typedef T value_type;
static Future<T> makeEmpty(); // equivalent to moved-from
// not copyable
Future(Future const&) = delete;
Future& operator=(Future const&) = delete;
......@@ -481,6 +483,8 @@ class Future {
explicit
Future(corePtr obj) : core_(obj) {}
explicit Future(detail::EmptyConstruct) noexcept;
void detach();
void throwIfInvalid() const;
......
......@@ -24,6 +24,11 @@
namespace folly {
template <class T>
Promise<T> Promise<T>::makeEmpty() noexcept {
return Promise<T>(detail::EmptyConstruct{});
}
template <class T>
Promise<T>::Promise() : retrieved_(false), core_(new detail::Core<T>())
{}
......
......@@ -34,6 +34,8 @@ class CoreCallbackState;
template <class T>
class Promise {
public:
static Promise<T> makeEmpty() noexcept; // equivalent to moved-from
Promise();
~Promise();
......
......@@ -40,6 +40,11 @@ static eggs_t eggs("eggs");
// Future
TEST(Future, makeEmpty) {
auto f = Future<int>::makeEmpty();
EXPECT_THROW(f.isReady(), NoState);
}
TEST(Future, futureDefaultCtor) {
Future<Unit>();
}
......
......@@ -24,6 +24,11 @@ using std::string;
typedef FutureException eggs_t;
static eggs_t eggs("eggs");
TEST(Promise, makeEmpty) {
auto p = Promise<int>::makeEmpty();
EXPECT_TRUE(p.isFulfilled());
}
TEST(Promise, special) {
EXPECT_FALSE(std::is_copy_constructible<Promise<int>>::value);
EXPECT_FALSE(std::is_copy_assignable<Promise<int>>::value);
......
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