diff --git a/base/include/android-base/result.h b/base/include/android-base/result.h index 8f0071051..897c48f30 100644 --- a/base/include/android-base/result.h +++ b/base/include/android-base/result.h @@ -28,9 +28,8 @@ // from when the error occurred. ResultError can be used in an ostream directly to print its // string value. // -// Success is a typedef that aids in creating Result that do not contain a return value. -// Result is the correct return type for a function that either returns successfully or -// returns an error value. Returning Success() from a function that returns Result is the +// Result is the correct return type for a function that either returns successfully or +// returns an error value. Returning {} from a function that returns Result is the // correct way to indicate that a function without a return type has completed successfully. // // A successful Result is constructed implicitly from any type that can be implicitly converted @@ -164,9 +163,5 @@ inline Error ErrnoError() { template using Result = android::base::expected; -// Usage: `Result` as a result type that doesn't contain a value. -// Use `return {}` or `return Success()` to return with success. -using Success = std::monostate; - } // namespace base } // namespace android diff --git a/base/result_test.cpp b/base/result_test.cpp index d31e775e5..72f97f4b6 100644 --- a/base/result_test.cpp +++ b/base/result_test.cpp @@ -49,27 +49,6 @@ TEST(result, result_accessors_rvalue) { EXPECT_EQ('s', Result("success")->data()[0]); } -TEST(result, result_success) { - Result result = Success(); - ASSERT_TRUE(result); - ASSERT_TRUE(result.has_value()); - - EXPECT_EQ(Success(), *result); - EXPECT_EQ(Success(), result.value()); -} - -TEST(result, result_success_rvalue) { - // Success() doesn't actually create a Result object, but rather an object that can be - // implicitly constructed into a Result object. - - auto MakeRvalueSuccessResult = []() -> Result { return Success(); }; - ASSERT_TRUE(MakeRvalueSuccessResult()); - ASSERT_TRUE(MakeRvalueSuccessResult().has_value()); - - EXPECT_EQ(Success(), *MakeRvalueSuccessResult()); - EXPECT_EQ(Success(), MakeRvalueSuccessResult().value()); -} - TEST(result, result_void) { Result ok = {}; EXPECT_TRUE(ok); @@ -96,7 +75,7 @@ TEST(result, result_void) { } TEST(result, result_error) { - Result result = Error() << "failure" << 1; + Result result = Error() << "failure" << 1; ASSERT_FALSE(result); ASSERT_FALSE(result.has_value()); @@ -105,7 +84,7 @@ TEST(result, result_error) { } TEST(result, result_error_empty) { - Result result = Error(); + Result result = Error(); ASSERT_FALSE(result); ASSERT_FALSE(result.has_value()); @@ -120,7 +99,7 @@ TEST(result, result_error_rvalue) { // definition will not know what the type, T, of the underlying Result object that it would // create is. - auto MakeRvalueErrorResult = []() -> Result { return Error() << "failure" << 1; }; + auto MakeRvalueErrorResult = []() -> Result { return Error() << "failure" << 1; }; ASSERT_FALSE(MakeRvalueErrorResult()); ASSERT_FALSE(MakeRvalueErrorResult().has_value()); @@ -131,7 +110,7 @@ TEST(result, result_error_rvalue) { TEST(result, result_errno_error) { constexpr int test_errno = 6; errno = test_errno; - Result result = ErrnoError() << "failure" << 1; + Result result = ErrnoError() << "failure" << 1; ASSERT_FALSE(result); ASSERT_FALSE(result.has_value()); @@ -143,7 +122,7 @@ TEST(result, result_errno_error) { TEST(result, result_errno_error_no_text) { constexpr int test_errno = 6; errno = test_errno; - Result result = ErrnoError(); + Result result = ErrnoError(); ASSERT_FALSE(result); ASSERT_FALSE(result.has_value()); @@ -154,7 +133,7 @@ TEST(result, result_errno_error_no_text) { TEST(result, result_error_from_other_result) { auto error_text = "test error"s; - Result result = Error() << error_text; + Result result = Error() << error_text; ASSERT_FALSE(result); ASSERT_FALSE(result.has_value()); @@ -170,7 +149,7 @@ TEST(result, result_error_from_other_result) { TEST(result, result_error_through_ostream) { auto error_text = "test error"s; - Result result = Error() << error_text; + Result result = Error() << error_text; ASSERT_FALSE(result); ASSERT_FALSE(result.has_value()); @@ -188,7 +167,7 @@ TEST(result, result_errno_error_through_ostream) { auto error_text = "test error"s; constexpr int test_errno = 6; errno = 6; - Result result = ErrnoError() << error_text; + Result result = ErrnoError() << error_text; errno = 0; @@ -306,9 +285,7 @@ TEST(result, no_copy_on_return) { // constructor. This is done with by disabling the forwarding reference constructor if its first // and only type is Result. TEST(result, result_result_with_success) { - auto return_result_result_with_success = []() -> Result> { - return Result(); - }; + auto return_result_result_with_success = []() -> Result> { return Result(); }; auto result = return_result_result_with_success(); ASSERT_TRUE(result); ASSERT_TRUE(*result); @@ -318,8 +295,8 @@ TEST(result, result_result_with_success) { } TEST(result, result_result_with_failure) { - auto return_result_result_with_error = []() -> Result> { - return Result(ResultError("failure string", 6)); + auto return_result_result_with_error = []() -> Result> { + return Result(ResultError("failure string", 6)); }; auto result = return_result_result_with_error(); ASSERT_TRUE(result);