Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions include/libp2p/multi/multihash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#pragma once

#include <memory>
#include <utility>

#include <libp2p/common/types.hpp>
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -129,7 +136,7 @@ namespace libp2p::multi {

const Data &data() const;

std::shared_ptr<const Data> data_;
std::shared_ptr<Data> data_;
};

} // namespace libp2p::multi
Expand Down
3 changes: 2 additions & 1 deletion include/libp2p/multi/uvarint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ namespace libp2p::multi {
/// Disable to return span to inner data of temporary object
BytesIn toBytes() const && = delete;

const std::vector<uint8_t> &toVector() const;
const std::vector<uint8_t> &toVector() const &;
std::vector<uint8_t> toVector() &&;

/**
* Assigns the varint to an unsigned integer, encoding the latter
Expand Down
3 changes: 2 additions & 1 deletion include/libp2p/peer/peer_id.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ namespace libp2p::peer {
/**
* Creates a vector representation of PeerId.
*/
const std::vector<uint8_t> &toVector() const;
const std::vector<uint8_t> &toVector() const &;
std::vector<uint8_t> toVector() &&;

/**
* Get a SHA256 multihash of the peer's ID
Expand Down
13 changes: 11 additions & 2 deletions src/multi/multihash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const Data>(type, hash)) {}
: data_(std::make_shared<Data>(type, hash)) {}

namespace {
template <typename Buffer>
Expand Down Expand Up @@ -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;
}

Expand Down
6 changes: 5 additions & 1 deletion src/multi/uvarint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,14 @@ namespace libp2p::multi {
return bytes_;
}

const std::vector<uint8_t> &UVarint::toVector() const {
const std::vector<uint8_t> &UVarint::toVector() const & {
return bytes_;
}

std::vector<uint8_t> UVarint::toVector() && {
return std::move(bytes_);
}

size_t UVarint::size() const {
return bytes_.size();
}
Expand Down
6 changes: 5 additions & 1 deletion src/peer/peer_id.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,14 @@ namespace libp2p::peer {
return encodeBase58(hash_.toBuffer());
}

const std::vector<uint8_t> &PeerId::toVector() const {
const std::vector<uint8_t> &PeerId::toVector() const & {
return hash_.toBuffer();
}

std::vector<uint8_t> PeerId::toVector() && {
return std::move(hash_).toBuffer();
}

std::string PeerId::toHex() const {
return hash_.toHex();
}
Expand Down
Loading