From c1ba9e5c3b6e6f773c1497c4eeac66f02311f9ff Mon Sep 17 00:00:00 2001 From: tqcq <99722391+tqcq@users.noreply.github.com> Date: Fri, 19 Apr 2024 15:39:42 +0800 Subject: [PATCH] feat support build future from Constructor --- src/sled/futures/future.h | 22 ++++++++++++++++++++++ src/sled/futures/future_test.cc | 10 ++++++++++ 2 files changed, 32 insertions(+) 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"); + } }