From: Hans Fugal Date: Thu, 30 Apr 2015 22:18:15 +0000 (-0700) Subject: Unit::Lift X-Git-Tag: v0.38.0~25 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=0b46bcd5eceaedb51c84bc16f5804481b41cb965;p=folly.git Unit::Lift 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 --- diff --git a/folly/futures/Unit.h b/folly/futures/Unit.h index cb0b92c1..a03d5202 100644 --- a/folly/futures/Unit.h +++ b/folly/futures/Unit.h @@ -16,7 +16,16 @@ #pragma once namespace folly { -struct Unit {}; +struct Unit { + template struct Lift : public std::false_type { + using type = T; + }; +}; + +template <> +struct Unit::Lift : public std::true_type { + using type = Unit; +}; template struct is_void_or_unit : public std::conditional< diff --git a/folly/futures/test/UnitTest.cpp b/folly/futures/test/UnitTest.cpp index 03057393..930b1231 100644 --- a/folly/futures/test/UnitTest.cpp +++ b/folly/futures/test/UnitTest.cpp @@ -33,3 +33,17 @@ 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); +} + +TEST(Unit, LiftVoid) { + using Lifted = Unit::Lift; + EXPECT_TRUE(Lifted::value); + auto v = std::is_same::value; + EXPECT_TRUE(v); +}