From f36d7f7b976fb5f0ef5702901ef92d59f0181c1f Mon Sep 17 00:00:00 2001 From: yperbasis Date: Tue, 28 Mar 2023 17:10:37 +0200 Subject: [PATCH] Make Result accessible via evmc_result reference Add Result::raw() method for accessing the result object via reference to evmc_result. --- include/evmc/evmc.hpp | 6 ++++++ test/unittests/cpp_test.cpp | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/include/evmc/evmc.hpp b/include/evmc/evmc.hpp index 3d336894f..a29f802d2 100644 --- a/include/evmc/evmc.hpp +++ b/include/evmc/evmc.hpp @@ -414,6 +414,12 @@ class Result : private evmc_result return *this; } + /// Access the result object as a referenced to ::evmc_result. + evmc_result& raw() noexcept { return *this; } + + /// Access the result object as a const referenced to ::evmc_result. + const evmc_result& raw() const noexcept { return *this; } + /// Releases the ownership and returns the raw copy of evmc_result. /// /// This method drops the ownership of the result diff --git a/test/unittests/cpp_test.cpp b/test/unittests/cpp_test.cpp index 435613f88..6a5e4dd0b 100644 --- a/test/unittests/cpp_test.cpp +++ b/test/unittests/cpp_test.cpp @@ -973,3 +973,25 @@ TEST(cpp, revision_to_string_invalid) EXPECT_EQ(os.str(), ""); } } + +TEST(cpp, result_c_const_access) +{ + static constexpr auto get_status = [](const evmc_result& c_result) noexcept { + return c_result.status_code; + }; + + const evmc::Result r{EVMC_REVERT}; + EXPECT_EQ(get_status(r.raw()), EVMC_REVERT); +} + +TEST(cpp, result_c_nonconst_access) +{ + static constexpr auto set_status = [](evmc_result& c_result) noexcept { + c_result.status_code = EVMC_SUCCESS; + }; + + evmc::Result r; + EXPECT_EQ(r.status_code, EVMC_INTERNAL_ERROR); + set_status(r.raw()); + EXPECT_EQ(r.status_code, EVMC_SUCCESS); +}