From 2c17727b4113037115ebf048c7b68105bd704fd3 Mon Sep 17 00:00:00 2001 From: alienx5499 Date: Wed, 10 Dec 2025 15:25:23 +0530 Subject: [PATCH] Add move-friendly toBuffer/toVector overloads --- include/libp2p/multi/multihash.hpp | 11 +++++++++-- include/libp2p/multi/uvarint.hpp | 3 ++- include/libp2p/peer/peer_id.hpp | 3 ++- src/multi/multihash.cpp | 13 +++++++++++-- src/multi/uvarint.cpp | 6 +++++- src/peer/peer_id.cpp | 6 +++++- 6 files changed, 34 insertions(+), 8 deletions(-) diff --git a/include/libp2p/multi/multihash.hpp b/include/libp2p/multi/multihash.hpp index 11443805a..600d7e623 100644 --- a/include/libp2p/multi/multihash.hpp +++ b/include/libp2p/multi/multihash.hpp @@ -6,6 +6,7 @@ #pragma once +#include #include #include @@ -85,7 +86,13 @@ namespace libp2p::multi { /** * @return a buffer with the multihash, including its type, length and hash */ - const Buffer &toBuffer() const; + const Buffer &toBuffer() const &; + + /** + * @return a buffer with the multihash, including its type, length and hash + * (moved out of a temporary when unique) + */ + Buffer toBuffer() &&; /** * @return Pre-calculated hash for std containers @@ -129,7 +136,7 @@ namespace libp2p::multi { const Data &data() const; - std::shared_ptr data_; + std::shared_ptr data_; }; } // namespace libp2p::multi diff --git a/include/libp2p/multi/uvarint.hpp b/include/libp2p/multi/uvarint.hpp index 61224ce8d..ebb25e9bd 100644 --- a/include/libp2p/multi/uvarint.hpp +++ b/include/libp2p/multi/uvarint.hpp @@ -55,7 +55,8 @@ namespace libp2p::multi { /// Disable to return span to inner data of temporary object BytesIn toBytes() const && = delete; - const std::vector &toVector() const; + const std::vector &toVector() const &; + std::vector toVector() &&; /** * Assigns the varint to an unsigned integer, encoding the latter diff --git a/include/libp2p/peer/peer_id.hpp b/include/libp2p/peer/peer_id.hpp index 8c0b50d20..a7430bf05 100644 --- a/include/libp2p/peer/peer_id.hpp +++ b/include/libp2p/peer/peer_id.hpp @@ -71,7 +71,8 @@ namespace libp2p::peer { /** * Creates a vector representation of PeerId. */ - const std::vector &toVector() const; + const std::vector &toVector() const &; + std::vector toVector() &&; /** * Get a SHA256 multihash of the peer's ID diff --git a/src/multi/multihash.cpp b/src/multi/multihash.cpp index b20ee5b46..2f688d593 100644 --- a/src/multi/multihash.cpp +++ b/src/multi/multihash.cpp @@ -35,7 +35,7 @@ OUTCOME_CPP_DEFINE_CATEGORY(libp2p::multi, Multihash::Error, e) { namespace libp2p::multi { Multihash::Multihash(HashType type, BytesIn hash) - : data_(std::make_shared(type, hash)) {} + : data_(std::make_shared(type, hash)) {} namespace { template @@ -132,7 +132,16 @@ namespace libp2p::multi { return fmt::format("{:X}", data().bytes); } - const Bytes &Multihash::toBuffer() const { + const Bytes &Multihash::toBuffer() const & { + return data().bytes; + } + + Bytes Multihash::toBuffer() && { + if (data_ && data_.use_count() == 1) { + auto data = std::move(data_); + data_ = nullptr; + return std::move(data->bytes); + } return data().bytes; } diff --git a/src/multi/uvarint.cpp b/src/multi/uvarint.cpp index ced35a70a..86a9508ed 100644 --- a/src/multi/uvarint.cpp +++ b/src/multi/uvarint.cpp @@ -50,10 +50,14 @@ namespace libp2p::multi { return bytes_; } - const std::vector &UVarint::toVector() const { + const std::vector &UVarint::toVector() const & { return bytes_; } + std::vector UVarint::toVector() && { + return std::move(bytes_); + } + size_t UVarint::size() const { return bytes_.size(); } diff --git a/src/peer/peer_id.cpp b/src/peer/peer_id.cpp index 931c5a61c..ad3365f86 100644 --- a/src/peer/peer_id.cpp +++ b/src/peer/peer_id.cpp @@ -81,10 +81,14 @@ namespace libp2p::peer { return encodeBase58(hash_.toBuffer()); } - const std::vector &PeerId::toVector() const { + const std::vector &PeerId::toVector() const & { return hash_.toBuffer(); } + std::vector PeerId::toVector() && { + return std::move(hash_).toBuffer(); + } + std::string PeerId::toHex() const { return hash_.toHex(); }