feat support build future from Constructor
This commit is contained in:
parent
2268a7e4aa
commit
c1ba9e5c3b
@ -82,6 +82,28 @@ public:
|
||||
Future<T, FailureT> &operator=(Future<T, FailureT> &&) noexcept = default;
|
||||
~Future() = default;
|
||||
|
||||
Future(const T &value) noexcept
|
||||
{
|
||||
static_assert(!std::is_same<T, FailureT>::value, "T and FailureT must be different types");
|
||||
data_ = Future<T, FailureT>::Create().data_;
|
||||
FillSuccess(value);
|
||||
}
|
||||
|
||||
Future(T &&value) noexcept
|
||||
{
|
||||
static_assert(!std::is_same<T, FailureT>::value, "T and FailureT must be different types");
|
||||
data_ = Future<T, FailureT>::Create().data_;
|
||||
FillSuccess(std::move(value));
|
||||
}
|
||||
|
||||
template<typename = typename std::enable_if<!std::is_same<T, FailureT>::value>>
|
||||
Future(const FailureT &failure) noexcept
|
||||
{
|
||||
static_assert(!std::is_same<T, FailureT>::value, "T and FailureT must be different types");
|
||||
data_ = Future<T, FailureT>::Create().data_;
|
||||
FillFailure(failure);
|
||||
}
|
||||
|
||||
bool operator==(const Future<T, FailureT> &other) const noexcept { return data_ == other.data_; }
|
||||
|
||||
bool operator!=(const Future<T, FailureT> &other) const noexcept { return !operator==(other); }
|
||||
|
@ -102,4 +102,14 @@ TEST_SUITE("future")
|
||||
CHECK_EQ(tid, f.Result());
|
||||
CHECK_NE(self_tid, f.Result());
|
||||
}
|
||||
|
||||
TEST_CASE("Constructor")
|
||||
{
|
||||
sled::Future<int, std::string> f1 = 1;
|
||||
sled::Future<int, std::string> f2 = std::string("1");
|
||||
REQUIRE(f1.IsCompleted());
|
||||
REQUIRE(f2.IsFailed());
|
||||
REQUIRE_EQ(f1.Result(), 1);
|
||||
REQUIRE_EQ(f2.FailureReason(), "1");
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user