From: Yedidya Feldblum Date: Tue, 17 May 2016 22:27:43 +0000 (-0700) Subject: Simplify Unit X-Git-Tag: 2016.07.26~227 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=e38ff539d4559d94c35e5f7a84f520d294566acc;p=folly.git Simplify Unit Summary: [Folly] Simplify `Unit`. Specifically: * Make `Unit::Lift` and `Unit::Drop` simply be aliases to `std::conditional<...>`. No need for anything more complicated. * Remove `is_void_or_unit`, which is unused and unnecessary. Reviewed By: djwatson Differential Revision: D3312481 fbshipit-source-id: 280d40aa8ef91c52f96a51b03e0a109d76c939ec --- diff --git a/folly/futures/Unit.h b/folly/futures/Unit.h index 0335b8df..cb6a6566 100644 --- a/folly/futures/Unit.h +++ b/folly/futures/Unit.h @@ -13,7 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + #pragma once + +#include + namespace folly { /// In functional programming, the degenerate case is often called "unit". In @@ -23,45 +27,15 @@ namespace folly { /// You can ignore the actual value, and we port some of the syntactic /// niceties like setValue() instead of setValue(Unit{}). struct Unit { - /// Lift type T into Unit. This is the definition for all non-void types. - template struct Lift : public std::false_type { - using type = T; - }; - template struct Drop : public std::false_type { - using type = T; - }; + template + using Lift = std::conditional::value, Unit, T>; + template + using Drop = std::conditional::value, void, T>; + bool operator==(const Unit& /*other*/) const { return true; } bool operator!=(const Unit& /*other*/) const { return false; } }; -// Lift void into Unit. -template <> -struct Unit::Lift : public std::true_type { - using type = Unit; -}; - -// Lift Unit into Unit (identity). -template <> -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 -{}; - constexpr Unit unit {}; } diff --git a/folly/futures/test/UnitTest.cpp b/folly/futures/test/UnitTest.cpp index ea2359d6..9eb74dc5 100644 --- a/folly/futures/test/UnitTest.cpp +++ b/folly/futures/test/UnitTest.cpp @@ -17,6 +17,7 @@ #include #include +#include using namespace folly; @@ -34,47 +35,45 @@ TEST(Unit, operatorNe) { EXPECT_FALSE(Unit{} != Unit{}); } -TEST(Unit, voidOrUnit) { - EXPECT_TRUE(is_void_or_unit::value); - EXPECT_TRUE(is_void_or_unit::value); - EXPECT_FALSE(is_void_or_unit::value); -} - TEST(Unit, promiseSetValue) { Promise p; p.setValue(); } TEST(Unit, liftInt) { - using Lifted = Unit::Lift; - EXPECT_FALSE(Lifted::value); - auto v = std::is_same::value; - EXPECT_TRUE(v); + using lifted = Unit::Lift; + using actual = std::is_same; + EXPECT_TRUE(actual::value); +} + +TEST(Unit, liftUnit) { + using lifted = Unit::Lift; + using actual = std::is_same; + EXPECT_TRUE(actual::value); } TEST(Unit, liftVoid) { - using Lifted = Unit::Lift; - EXPECT_TRUE(Lifted::value); - auto v = std::is_same::value; - EXPECT_TRUE(v); + using lifted = Unit::Lift; + using actual = std::is_same; + EXPECT_TRUE(actual::value); } TEST(Unit, dropInt) { - using dropped = typename Unit::Drop; - EXPECT_FALSE(dropped::value); - EXPECT_TRUE((std::is_same::value)); + using dropped = Unit::Drop; + using actual = std::is_same; + EXPECT_TRUE(actual::value); } TEST(Unit, dropUnit) { - using dropped = typename Unit::Drop; - EXPECT_TRUE(dropped::value); - EXPECT_TRUE((std::is_void::value)); + using dropped = Unit::Drop; + using actual = std::is_same; + EXPECT_TRUE(actual::value); } TEST(Unit, dropVoid) { - using dropped = typename Unit::Drop; - EXPECT_TRUE(dropped::value); - EXPECT_TRUE((std::is_void::value)); + using dropped = Unit::Drop; + using actual = std::is_same; + EXPECT_TRUE(actual::value); } TEST(Unit, futureToUnit) {