cpp-libp2p
cpp-libp2p copied to clipboard
Add move-friendly buffer accessors
Overview
Implements the move-friendly accessor improvements requested in issue #95. Adds ref-qualified and rvalue overloads so temporaries can return buffers by move, eliminating unnecessary copies while keeping lvalue behavior unchanged.
Problem
-
toBuffer/toVectorreturned const references only, forcing an extra copy when called on temporaries. -
Multihashstored data in a const shared_ptr, blocking safe move-out even when uniquely owned.
Solution
- Add const&/&& overloads for
toBufferandtoVectoronMultihash,UVarint, andPeerId. - Allow
Multihashto move out its byte buffer when uniquely owned; fall back to const& otherwise. - Preserve existing API surface for lvalues; only move semantics change for rvalues.
Key Changes
-
include/libp2p/multi/multihash.hpp/src/multi/multihash.cpp: ref-qualified accessors; move-out path whenDatais uniquely owned. -
include/libp2p/multi/uvarint.hpp/src/multi/uvarint.cpp: add rvaluetoVectorto move backing bytes from temporaries. -
include/libp2p/peer/peer_id.hpp/src/peer/peer_id.cpp: rvaluetoVectordelegates to movedMultihash.
Closes #95