diff --git a/src/sled/futures/future.h b/src/sled/futures/future.h index d16181e..593e423 100644 --- a/src/sled/futures/future.h +++ b/src/sled/futures/future.h @@ -82,6 +82,28 @@ public: Future &operator=(Future &&) noexcept = default; ~Future() = default; + Future(const T &value) noexcept + { + static_assert(!std::is_same::value, "T and FailureT must be different types"); + data_ = Future::Create().data_; + FillSuccess(value); + } + + Future(T &&value) noexcept + { + static_assert(!std::is_same::value, "T and FailureT must be different types"); + data_ = Future::Create().data_; + FillSuccess(std::move(value)); + } + + template::value>> + Future(const FailureT &failure) noexcept + { + static_assert(!std::is_same::value, "T and FailureT must be different types"); + data_ = Future::Create().data_; + FillFailure(failure); + } + bool operator==(const Future &other) const noexcept { return data_ == other.data_; } bool operator!=(const Future &other) const noexcept { return !operator==(other); } diff --git a/src/sled/futures/future_test.cc b/src/sled/futures/future_test.cc index 82b1290..2d94706 100644 --- a/src/sled/futures/future_test.cc +++ b/src/sled/futures/future_test.cc @@ -102,4 +102,14 @@ TEST_SUITE("future") CHECK_EQ(tid, f.Result()); CHECK_NE(self_tid, f.Result()); } + + TEST_CASE("Constructor") + { + sled::Future f1 = 1; + sled::Future f2 = std::string("1"); + REQUIRE(f1.IsCompleted()); + REQUIRE(f2.IsFailed()); + REQUIRE_EQ(f1.Result(), 1); + REQUIRE_EQ(f2.FailureReason(), "1"); + } }