feat support build future from Constructor

This commit is contained in:
tqcq 2024-04-19 15:39:42 +08:00
parent 2268a7e4aa
commit c1ba9e5c3b
2 changed files with 32 additions and 0 deletions

View File

@ -82,6 +82,28 @@ public:
Future<T, FailureT> &operator=(Future<T, FailureT> &&) noexcept = default; Future<T, FailureT> &operator=(Future<T, FailureT> &&) noexcept = default;
~Future() = 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 data_ == other.data_; }
bool operator!=(const Future<T, FailureT> &other) const noexcept { return !operator==(other); } bool operator!=(const Future<T, FailureT> &other) const noexcept { return !operator==(other); }

View File

@ -102,4 +102,14 @@ TEST_SUITE("future")
CHECK_EQ(tid, f.Result()); CHECK_EQ(tid, f.Result());
CHECK_NE(self_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");
}
} }