cpp-libp2p icon indicating copy to clipboard operation
cpp-libp2p copied to clipboard

Add move-friendly buffer accessors

Open alienx5499 opened this issue 1 month ago • 0 comments

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/toVector returned const references only, forcing an extra copy when called on temporaries.
  • Multihash stored data in a const shared_ptr, blocking safe move-out even when uniquely owned.

Solution

  • Add const&/&& overloads for toBuffer and toVector on Multihash, UVarint, and PeerId.
  • Allow Multihash to 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 when Data is uniquely owned.
  • include/libp2p/multi/uvarint.hpp / src/multi/uvarint.cpp: add rvalue toVector to move backing bytes from temporaries.
  • include/libp2p/peer/peer_id.hpp / src/peer/peer_id.cpp: rvalue toVector delegates to moved Multihash.

Closes #95

alienx5499 avatar Dec 10 '25 09:12 alienx5499