mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-04-20 11:49:07 +02:00
Merge bitcoin/bitcoin#32983: rpc: refactor: use string_view in Arg/MaybeArg
b63428ac9crpc: refactor: use more (Maybe)Arg<std::string_view> (stickies-v)037830ca0drefactor: increase string_view usage (stickies-v)b3bf18f0barpc: refactor: use string_view in Arg/MaybeArg (stickies-v) Pull request description: The `RPCHelpMan::{Arg,MaybeArg}` helpers avoid copying (potentially) large strings by returning them as `const std::string*` (`MaybeArg`) or `const std::string&` (`Arg`). For `MaybeArg`, this has the not-so-nice effect that users need to deal with raw pointers, potentially also requiring new functions (e.g. [`EnsureUniqueWalletName` ](d127b25199 (diff-d8bfcfbdd5fa7d5c52d38c1fe5eeac9ce5c5a794cdfaf683585140fa70a32374R32))) with raw pointers being implemented. This PR aims to improve on this by returning a trivially copyable `std::string_view` (`Arg`) or `std::optional<std::string_view>` (`MaybeArg`), modernizing the interface without introducing any additional copying overhead. In doing so, it also generalizes whether we return by value or by pointer/reference using `std::is_trivially_copyable_v` instead of defining the types manually. In cases where functions currently take a `const std::string&` and it would be too much work / touching consensus logic to update them (`signmessage.cpp`), a `std::string` copy is made (which was already happening anyway). The last 2 commits increase usage of the `{Arg,MaybeArg}<std::string_view>` helpers, and could be dropped/pruned if anything turns out to be controversial - I just think it's a nice little cleanup. ACKs for top commit: maflcko: re-ACKb63428ac9c🎉 achow101: ACKb63428ac9cpablomartin4btc: re-ACK [b63428a](b63428ac9c) w0xlt: reACKb63428ac9cTree-SHA512: b4942c353a1658c22a88d8c9b402c288ad35265a3b88aa2072b1f9b6d921cd073194ed4b00b807cb48ca440f47c87ef3d8e0dd1a5d814be58fc7743f26288277
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
#include <util/time.h>
|
||||
|
||||
#include <any>
|
||||
#include <string_view>
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
@@ -621,9 +622,9 @@ BOOST_AUTO_TEST_CASE(rpc_arg_helper)
|
||||
//! Check that `self.Arg` returns the same value as the `request.params` accessors
|
||||
RPCHelpMan::RPCMethodImpl check_positional = [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue {
|
||||
BOOST_CHECK_EQUAL(self.Arg<int>("req_int"), request.params[0].getInt<int>());
|
||||
BOOST_CHECK_EQUAL(self.Arg<std::string>("req_str"), request.params[1].get_str());
|
||||
BOOST_CHECK_EQUAL(self.Arg<std::string_view>("req_str"), request.params[1].get_str());
|
||||
BOOST_CHECK_EQUAL(self.Arg<uint64_t>("def_uint64_t"), request.params[2].isNull() ? DEFAULT_UINT64_T : request.params[2].getInt<uint64_t>());
|
||||
BOOST_CHECK_EQUAL(self.Arg<std::string>("def_string"), request.params[3].isNull() ? DEFAULT_STRING : request.params[3].get_str());
|
||||
BOOST_CHECK_EQUAL(self.Arg<std::string_view>("def_string"), request.params[3].isNull() ? DEFAULT_STRING : request.params[3].get_str());
|
||||
BOOST_CHECK_EQUAL(self.Arg<bool>("def_bool"), request.params[4].isNull() ? DEFAULT_BOOL : request.params[4].get_bool());
|
||||
if (!request.params[5].isNull()) {
|
||||
BOOST_CHECK_EQUAL(self.MaybeArg<double>("opt_double").value(), request.params[5].get_real());
|
||||
@@ -631,10 +632,9 @@ BOOST_AUTO_TEST_CASE(rpc_arg_helper)
|
||||
BOOST_CHECK(!self.MaybeArg<double>("opt_double"));
|
||||
}
|
||||
if (!request.params[6].isNull()) {
|
||||
BOOST_CHECK(self.MaybeArg<std::string>("opt_string"));
|
||||
BOOST_CHECK_EQUAL(*self.MaybeArg<std::string>("opt_string"), request.params[6].get_str());
|
||||
BOOST_CHECK_EQUAL(self.MaybeArg<std::string_view>("opt_string"), request.params[6].get_str());
|
||||
} else {
|
||||
BOOST_CHECK(!self.MaybeArg<std::string>("opt_string"));
|
||||
BOOST_CHECK(!self.MaybeArg<std::string_view>("opt_string"));
|
||||
}
|
||||
return UniValue{};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user