folly::Unit::Drop.
authorYedidya Feldblum <yfeldblum@fb.com>
Thu, 2 Jul 2015 01:39:41 +0000 (18:39 -0700)
committerSara Golemon <sgolemon@fb.com>
Thu, 2 Jul 2015 18:03:13 +0000 (11:03 -0700)
Summary: [Folly] folly::Unit::Drop.

Antisymmetric to folly::Unit::Lift.

Reviewed By: @fugalh

Differential Revision: D2211725

folly/futures/Unit.h
folly/futures/test/UnitTest.cpp

index d4d99d67ae87279448f3dc6412274e0abb20b93b..8a8b932c6906e19bf756f171f69ae31cbd972766 100644 (file)
@@ -27,6 +27,9 @@ struct Unit {
   template <class T> struct Lift : public std::false_type {
     using type = T;
   };
+  template <class T> 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<Unit> : public std::true_type {
   using type = Unit;
 };
 
+// Drop Unit into void.
+template <>
+struct Unit::Drop<Unit> : public std::true_type {
+  using type = void;
+};
+
+// Drop void into void (identity).
+template <>
+struct Unit::Drop<void> : public std::true_type {
+  using type = void;
+};
+
 template <class T>
 struct is_void_or_unit : public Unit::Lift<T>
 {};
index e4ef1eb9bbbffb6fa53e394a445852cf8c395df5..0151f691ee87873e281ec609ca3891aa593742e6 100644 (file)
@@ -59,6 +59,24 @@ TEST(Unit, liftVoid) {
   EXPECT_TRUE(v);
 }
 
+TEST(Unit, dropInt) {
+  using dropped = typename Unit::Drop<int>;
+  EXPECT_FALSE(dropped::value);
+  EXPECT_TRUE((std::is_same<int, dropped::type>::value));
+}
+
+TEST(Unit, dropUnit) {
+  using dropped = typename Unit::Drop<Unit>;
+  EXPECT_TRUE(dropped::value);
+  EXPECT_TRUE((std::is_void<dropped::type>::value));
+}
+
+TEST(Unit, dropVoid) {
+  using dropped = typename Unit::Drop<void>;
+  EXPECT_TRUE(dropped::value);
+  EXPECT_TRUE((std::is_void<dropped::type>::value));
+}
+
 TEST(Unit, futureToUnit) {
   Future<Unit> fu = makeFuture(42).unit();
   fu.value();