From b7a982a8f0e04cd4940ed31fa31529b1a94ad1c6 Mon Sep 17 00:00:00 2001 From: Yedidya Feldblum Date: Wed, 1 Jul 2015 18:39:41 -0700 Subject: [PATCH] folly::Unit::Drop. Summary: [Folly] folly::Unit::Drop. Antisymmetric to folly::Unit::Lift. Reviewed By: @fugalh Differential Revision: D2211725 --- folly/futures/Unit.h | 15 +++++++++++++++ folly/futures/test/UnitTest.cpp | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/folly/futures/Unit.h b/folly/futures/Unit.h index d4d99d67..8a8b932c 100644 --- a/folly/futures/Unit.h +++ b/folly/futures/Unit.h @@ -27,6 +27,9 @@ struct Unit { template struct Lift : public std::false_type { using type = T; }; + template struct Drop : public std::false_type { + using type = T; + }; bool operator==(const Unit& other) const { return true; } bool operator!=(const Unit& other) const { return false; } }; @@ -43,6 +46,18 @@ struct Unit::Lift : public std::true_type { using type = Unit; }; +// Drop Unit into void. +template <> +struct Unit::Drop : public std::true_type { + using type = void; +}; + +// Drop void into void (identity). +template <> +struct Unit::Drop : public std::true_type { + using type = void; +}; + template struct is_void_or_unit : public Unit::Lift {}; diff --git a/folly/futures/test/UnitTest.cpp b/folly/futures/test/UnitTest.cpp index e4ef1eb9..0151f691 100644 --- a/folly/futures/test/UnitTest.cpp +++ b/folly/futures/test/UnitTest.cpp @@ -59,6 +59,24 @@ TEST(Unit, liftVoid) { EXPECT_TRUE(v); } +TEST(Unit, dropInt) { + using dropped = typename Unit::Drop; + EXPECT_FALSE(dropped::value); + EXPECT_TRUE((std::is_same::value)); +} + +TEST(Unit, dropUnit) { + using dropped = typename Unit::Drop; + EXPECT_TRUE(dropped::value); + EXPECT_TRUE((std::is_void::value)); +} + +TEST(Unit, dropVoid) { + using dropped = typename Unit::Drop; + EXPECT_TRUE(dropped::value); + EXPECT_TRUE((std::is_void::value)); +} + TEST(Unit, futureToUnit) { Future fu = makeFuture(42).unit(); fu.value(); -- 2.34.1