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
union {
F func_;
};
- Promise<T> promise_{detail::EmptyConstruct{}};
+ Promise<T> promise_{Promise<T>::makeEmpty()};
};
template <typename T, typename F>
}
}
+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;
core_->raise(std::move(exception));
}
+template <class T>
+Future<T>::Future(detail::EmptyConstruct) noexcept
+ : core_(nullptr) {}
+
// makeFuture
template <class T>
public:
typedef T value_type;
+ static Future<T> makeEmpty(); // equivalent to moved-from
+
// not copyable
Future(Future const&) = delete;
Future& operator=(Future const&) = delete;
explicit
Future(corePtr obj) : core_(obj) {}
+ explicit Future(detail::EmptyConstruct) noexcept;
+
void detach();
void throwIfInvalid() const;
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>())
{}
template <class T>
class Promise {
public:
+ static Promise<T> makeEmpty() noexcept; // equivalent to moved-from
+
Promise();
~Promise();
// Future
+TEST(Future, makeEmpty) {
+ auto f = Future<int>::makeEmpty();
+ EXPECT_THROW(f.isReady(), NoState);
+}
+
TEST(Future, futureDefaultCtor) {
Future<Unit>();
}
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);