From: Yedidya Feldblum Date: Wed, 18 May 2016 03:38:11 +0000 (-0700) Subject: Extract Unit to top-level X-Git-Tag: 2016.07.26~223 X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=a3323738a37ad3e8cae4a70931728e82fb5aeb6f;p=folly.git Extract Unit to top-level Summary: [Folly] Extract `Unit` to top-level. It was in `folly/futures/`, but this diff moves it to `folly/`. It is needed for futures, but it is more general than futures and can be used separately. Users must replace `folly/futures/Unit.h` with `folly/Unit.h`. Also codemods existing usage sites: ``` hg grep -lw folly/futures/Unit.h | xargs perl -pi -e 's,\bfolly/futures/Unit\.h\b,folly/Unit.h,g' ``` Reviewed By: igorsugak Differential Revision: D3314280 fbshipit-source-id: 16279b76b1d24529bec49077ccb36cd7d39f23b8 --- diff --git a/folly/Makefile.am b/folly/Makefile.am index 05058807..bc7e42d4 100644 --- a/folly/Makefile.am +++ b/folly/Makefile.am @@ -175,7 +175,6 @@ nobase_follyinclude_HEADERS = \ futures/Timekeeper.h \ futures/Try-inl.h \ futures/Try.h \ - futures/Unit.h \ futures/detail/Core.h \ futures/detail/FSM.h \ futures/detail/Types.h \ @@ -348,6 +347,7 @@ nobase_follyinclude_HEADERS = \ Traits.h \ Unicode.h \ Function.h \ + Unit.h \ Uri.h \ Uri-inl.h \ Varint.h \ diff --git a/folly/Unit.h b/folly/Unit.h new file mode 100644 index 00000000..cb6a6566 --- /dev/null +++ b/folly/Unit.h @@ -0,0 +1,41 @@ +/* + * Copyright 2016 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * 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 +/// C++, "void" is often the best analogue, however because of the syntactic +/// special-casing required for void it is a liability for template +/// metaprogramming. So, instead of e.g. Future, we have Future. +/// You can ignore the actual value, and we port some of the syntactic +/// niceties like setValue() instead of setValue(Unit{}). +struct Unit { + 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; } +}; + +constexpr Unit unit {}; + +} diff --git a/folly/futures/Timekeeper.h b/folly/futures/Timekeeper.h index f8dc51c2..bdf978a8 100644 --- a/folly/futures/Timekeeper.h +++ b/folly/futures/Timekeeper.h @@ -17,7 +17,7 @@ #pragma once #include -#include +#include namespace folly { diff --git a/folly/futures/Try.h b/folly/futures/Try.h index 5fae57f5..8d112a60 100644 --- a/folly/futures/Try.h +++ b/folly/futures/Try.h @@ -24,7 +24,7 @@ #include #include #include -#include +#include namespace folly { diff --git a/folly/futures/Unit.h b/folly/futures/Unit.h deleted file mode 100644 index cb6a6566..00000000 --- a/folly/futures/Unit.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 2016 Facebook, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * 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 -/// C++, "void" is often the best analogue, however because of the syntactic -/// special-casing required for void it is a liability for template -/// metaprogramming. So, instead of e.g. Future, we have Future. -/// You can ignore the actual value, and we port some of the syntactic -/// niceties like setValue() instead of setValue(Unit{}). -struct Unit { - 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; } -}; - -constexpr Unit unit {}; - -} diff --git a/folly/futures/test/FutureTest.cpp b/folly/futures/test/FutureTest.cpp index 2f1eaf12..8414370c 100644 --- a/folly/futures/test/FutureTest.cpp +++ b/folly/futures/test/FutureTest.cpp @@ -17,7 +17,7 @@ #include #include -#include +#include #include #include #include diff --git a/folly/futures/test/UnitTest.cpp b/folly/futures/test/UnitTest.cpp deleted file mode 100644 index ba372fbb..00000000 --- a/folly/futures/test/UnitTest.cpp +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright 2016 Facebook, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include - -#include - -using namespace folly; - -TEST(Unit, operatorEq) { - EXPECT_TRUE(Unit{} == Unit{}); -} - -TEST(Unit, operatorNe) { - EXPECT_FALSE(Unit{} != Unit{}); -} - -TEST(Unit, liftInt) { - 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; - using actual = std::is_same; - EXPECT_TRUE(actual::value); -} - -TEST(Unit, dropInt) { - using dropped = Unit::Drop; - using actual = std::is_same; - EXPECT_TRUE(actual::value); -} - -TEST(Unit, dropUnit) { - using dropped = Unit::Drop; - using actual = std::is_same; - EXPECT_TRUE(actual::value); -} - -TEST(Unit, dropVoid) { - using dropped = Unit::Drop; - using actual = std::is_same; - EXPECT_TRUE(actual::value); -} diff --git a/folly/test/Makefile.am b/folly/test/Makefile.am index 229efaf5..9932fe61 100644 --- a/folly/test/Makefile.am +++ b/folly/test/Makefile.am @@ -215,6 +215,9 @@ indestructible_test_SOURCES = IndestructibleTest.cpp indestructible_test_LDADD = libfollytestmain.la TESTS += indestructible_test +unit_test_SOURCES = UnitTest.cpp +unit_test_LDADD = libfollytestmain.la +TESTS += unit_test futures_test_SOURCES = \ ../futures/test/CollectTest.cpp \ @@ -242,7 +245,6 @@ futures_test_SOURCES = \ ../futures/test/TimekeeperTest.cpp \ ../futures/test/TimesTest.cpp \ ../futures/test/TryTest.cpp \ - ../futures/test/UnitTest.cpp \ ../futures/test/UnwrapTest.cpp \ ../futures/test/ViaTest.cpp \ ../futures/test/WaitTest.cpp \ diff --git a/folly/test/UnitTest.cpp b/folly/test/UnitTest.cpp new file mode 100644 index 00000000..18ff4ab5 --- /dev/null +++ b/folly/test/UnitTest.cpp @@ -0,0 +1,65 @@ +/* + * Copyright 2016 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include + +using namespace folly; + +TEST(Unit, operatorEq) { + EXPECT_TRUE(Unit{} == Unit{}); +} + +TEST(Unit, operatorNe) { + EXPECT_FALSE(Unit{} != Unit{}); +} + +TEST(Unit, liftInt) { + 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; + using actual = std::is_same; + EXPECT_TRUE(actual::value); +} + +TEST(Unit, dropInt) { + using dropped = Unit::Drop; + using actual = std::is_same; + EXPECT_TRUE(actual::value); +} + +TEST(Unit, dropUnit) { + using dropped = Unit::Drop; + using actual = std::is_same; + EXPECT_TRUE(actual::value); +} + +TEST(Unit, dropVoid) { + using dropped = Unit::Drop; + using actual = std::is_same; + EXPECT_TRUE(actual::value); +}