diff --git a/cpp/src/arrow/result.h b/cpp/src/arrow/result.h index 2b25de694864..a5e4f55db0f7 100644 --- a/cpp/src/arrow/result.h +++ b/cpp/src/arrow/result.h @@ -518,4 +518,9 @@ struct EnsureResult> { using type = Result; }; +template <> +struct EnsureResult { + using type = Status; +}; + } // namespace arrow diff --git a/cpp/src/arrow/result_test.cc b/cpp/src/arrow/result_test.cc index 794ef9b5dc9b..ad92841a6e70 100644 --- a/cpp/src/arrow/result_test.cc +++ b/cpp/src/arrow/result_test.cc @@ -636,6 +636,28 @@ TEST(ResultTest, MapFunctionToRrefError) { EXPECT_EQ(move_error.status(), error); // error is *not* replaced by other_error } +TEST(ResultTest, MapFunctionToStatus) { + static auto error = Status::Invalid("some error message"); + + const Result const_result(MoveOnlyDataType{kIntElement}); + auto const_mapped = + const_result.Map([](const MoveOnlyDataType& m) -> Status { return Status::OK(); }); + static_assert(std::is_same_v); + EXPECT_TRUE(const_mapped.ok()); + + auto move_mapped = Result(MoveOnlyDataType{kIntElement}) + .Map([](MoveOnlyDataType m) -> Status { return Status::OK(); }); + static_assert(std::is_same_v); + EXPECT_TRUE(move_mapped.ok()); + + const Result error_result(error); + auto error_mapped = + error_result.Map([](const MoveOnlyDataType& m) -> Status { return Status::OK(); }); + static_assert(std::is_same_v); + EXPECT_FALSE(error_mapped.ok()); + EXPECT_EQ(error_mapped, error); +} + // Verify that a Result is assignable to a Result, where T // is a type which has an implicit constructor taking a const U &. TEST(ResultTest, TemplateCopyAssign) {