Unit::Lift<T>
authorHans Fugal <fugalh@fb.com>
Thu, 30 Apr 2015 22:18:15 +0000 (15:18 -0700)
committerPraveen Kumar Ramakrishnan <praveenr@fb.com>
Tue, 12 May 2015 00:01:44 +0000 (17:01 -0700)
Summary: Lift void into the unit monad and pass other types through unscathed.

Test Plan: new unit tests

Reviewed By: yfeldblum@fb.com

Subscribers: exa, folly-diffs@, jsedgwick, yfeldblum, chalfant

FB internal diff: D2029785

Signature: t1:2029785:1430333928:ef2fbb2e3d94518a732f6818a06c32481120bd4f

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

index cb0b92c1c0eda9d3aa1f6feb725fbd32cbbd56f2..a03d5202002857332234ac58158d18812c189bc1 100644 (file)
 #pragma once
 namespace folly {
 
-struct Unit {};
+struct Unit {
+  template <class T> struct Lift : public std::false_type {
+    using type = T;
+  };
+};
+
+template <>
+struct Unit::Lift<void> : public std::true_type {
+  using type = Unit;
+};
 
 template <class T>
 struct is_void_or_unit : public std::conditional<
index 030573932ba80b718f6f1b85de1888256a4064cd..930b1231ae1edf5ce48cea9fbe0743d613978e7f 100644 (file)
@@ -33,3 +33,17 @@ TEST(Unit, PromiseSetValue) {
   Promise<Unit> p;
   p.setValue();
 }
+
+TEST(Unit, LiftInt) {
+  using Lifted = Unit::Lift<int>;
+  EXPECT_FALSE(Lifted::value);
+  auto v = std::is_same<int, Lifted::type>::value;
+  EXPECT_TRUE(v);
+}
+
+TEST(Unit, LiftVoid) {
+  using Lifted = Unit::Lift<void>;
+  EXPECT_TRUE(Lifted::value);
+  auto v = std::is_same<Unit, Lifted::type>::value;
+  EXPECT_TRUE(v);
+}