[llvm] r264238 - [Support] Add conversions between Expected<T> and ErrorOr<T>.
Lang Hames via llvm-commits
llvm-commits at lists.llvm.org
Wed Mar 23 19:00:11 PDT 2016
Author: lhames
Date: Wed Mar 23 21:00:10 2016
New Revision: 264238
URL: http://llvm.org/viewvc/llvm-project?rev=264238&view=rev
Log:
[Support] Add conversions between Expected<T> and ErrorOr<T>.
More utilities to help with std::error_code -> Error transitions.
Modified:
llvm/trunk/include/llvm/Support/Error.h
llvm/trunk/unittests/Support/ErrorTest.cpp
Modified: llvm/trunk/include/llvm/Support/Error.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/Error.h?rev=264238&r1=264237&r2=264238&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/Error.h (original)
+++ llvm/trunk/include/llvm/Support/Error.h Wed Mar 23 21:00:10 2016
@@ -753,6 +753,22 @@ inline std::error_code errorToErrorCode(
return EC;
}
+/// Convert an ErrorOr<T> to an Expected<T>.
+template <typename T>
+Expected<T> errorOrToExpected(ErrorOr<T> &&EO) {
+ if (auto EC = EO.getError())
+ return errorCodeToError(EC);
+ return std::move(*EO);
+}
+
+/// Convert an Expected<T> to an ErrorOr<T>.
+template <typename T>
+ErrorOr<T> expectedToErrorOr(Expected<T> &&E) {
+ if (auto Err = E.takeError())
+ return errorToErrorCode(std::move(Err));
+ return std::move(*E);
+}
+
/// Helper for check-and-exit error handling.
///
/// For tool use only. NOT FOR USE IN LIBRARY CODE.
Modified: llvm/trunk/unittests/Support/ErrorTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/ErrorTest.cpp?rev=264238&r1=264237&r2=264238&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/ErrorTest.cpp (original)
+++ llvm/trunk/unittests/Support/ErrorTest.cpp Wed Mar 23 21:00:10 2016
@@ -416,7 +416,7 @@ TEST(Error, ExpectedCovariance) {
A2 = Expected<std::unique_ptr<D>>(nullptr);
}
-TEST(Error, ECError) {
+TEST(Error, ErrorCodeConversions) {
// Round-trip a success value to check that it converts correctly.
EXPECT_EQ(errorToErrorCode(errorCodeToError(std::error_code())),
std::error_code())
@@ -427,6 +427,29 @@ TEST(Error, ECError) {
errc::invalid_argument)
<< "std::error_code error value should round-trip via Error "
"conversions";
+
+ // Round-trip a success value through ErrorOr/Expected to check that it
+ // converts correctly.
+ {
+ auto Orig = ErrorOr<int>(42);
+ auto RoundTripped =
+ expectedToErrorOr(errorOrToExpected(ErrorOr<int>(42)));
+ EXPECT_EQ(*Orig, *RoundTripped)
+ << "ErrorOr<T> success value should round-trip via Expected<T> "
+ "conversions.";
+ }
+
+ // Round-trip a failure value through ErrorOr/Expected to check that it
+ // converts correctly.
+ {
+ auto Orig = ErrorOr<int>(errc::invalid_argument);
+ auto RoundTripped =
+ expectedToErrorOr(
+ errorOrToExpected(ErrorOr<int>(errc::invalid_argument)));
+ EXPECT_EQ(Orig.getError(), RoundTripped.getError())
+ << "ErrorOr<T> failure value should round-trip via Expected<T> "
+ "conversions.";
+ }
}
} // end anon namespace
More information about the llvm-commits
mailing list