Skip to content

Commit add474f

Browse files
Extend CID copy/move operations (#78)
Signed-off-by: Sergey Kaprovich <[email protected]>
1 parent dcb4d17 commit add474f

File tree

6 files changed

+65
-8
lines changed

6 files changed

+65
-8
lines changed

core/codec/cbor/cbor_decode_stream.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ namespace fc::codec::cbor {
8181
if (maybe_cid.has_error()) {
8282
outcome::raise(CborDecodeError::INVALID_CID);
8383
}
84-
cid = maybe_cid.value();
84+
cid = std::move(maybe_cid.value());
8585
return *this;
8686
}
8787

core/codec/cbor/cbor_encode_stream.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ namespace fc::codec::cbor {
4040
return *this;
4141
}
4242

43-
CborEncodeStream &CborEncodeStream::operator<<(const CID &cid) {
43+
CborEncodeStream &CborEncodeStream::operator<<(const libp2p::multi::ContentIdentifier &cid) {
4444
auto maybe_cid_bytes = libp2p::multi::ContentIdentifierCodec::encode(cid);
4545
if (maybe_cid_bytes.has_error()) {
4646
outcome::raise(CborEncodeError::INVALID_CID);

core/codec/cbor/cbor_encode_stream.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ namespace fc::codec::cbor {
8080
/** Encodes string */
8181
CborEncodeStream &operator<<(const std::string &str);
8282
/** Encodes CID */
83-
CborEncodeStream &operator<<(const CID &cid);
83+
CborEncodeStream &operator<<(const libp2p::multi::ContentIdentifier &cid);
8484
/** Encodes list container encode substream */
8585
CborEncodeStream &operator<<(const CborEncodeStream &other);
8686
/** Encodes map container encode substream map */

core/primitives/cid/cid.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,40 @@ namespace fc {
1515

1616
CID::CID(const ContentIdentifier &cid) : ContentIdentifier(cid) {}
1717

18+
CID::CID(ContentIdentifier &&cid) noexcept
19+
: ContentIdentifier(
20+
cid.version, cid.content_type, std::move(cid.content_address)) {}
21+
22+
CID::CID(Version version,
23+
libp2p::multi::MulticodecType::Code content_type,
24+
libp2p::multi::Multihash content_address)
25+
: ContentIdentifier(version, content_type, std::move(content_address)) {}
26+
27+
CID &CID::operator=(CID &&cid) noexcept {
28+
version = cid.version;
29+
content_type = cid.content_type;
30+
content_address = std::move(cid.content_address);
31+
return *this;
32+
}
33+
34+
CID &CID::operator=(const ContentIdentifier &cid) {
35+
version = cid.version;
36+
content_type = cid.content_type;
37+
content_address = cid.content_address;
38+
return *this;
39+
}
40+
41+
CID::CID(CID &&cid) noexcept
42+
: ContentIdentifier(
43+
cid.version, cid.content_type, std::move(cid.content_address)) {}
44+
45+
CID &CID::operator=(ContentIdentifier &&cid) {
46+
version = cid.version;
47+
content_type = cid.content_type;
48+
content_address = std::move(cid.content_address);
49+
return *this;
50+
}
51+
1852
outcome::result<std::string> CID::toString() const {
1953
return libp2p::multi::ContentIdentifierCodec::toString(*this);
2054
}

core/primitives/cid/cid.hpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,35 @@ namespace fc {
1616
using ContentIdentifier::ContentIdentifier;
1717

1818
/**
19-
* ContentIdentifier is not default-constructible, but in some cases we need
19+
* ContentIdentifier is not default-constructable, but in some cases we need
2020
* default value. This value can be used to initialize class member or local
2121
* variable. Trying to CBOR encode this value will yield error, to ensure
2222
* proper initialization.
2323
*/
2424
CID();
25-
CID(const ContentIdentifier &cid);
25+
26+
explicit CID(const ContentIdentifier &cid);
27+
28+
explicit CID(ContentIdentifier &&cid) noexcept;
29+
30+
CID(CID &&cid) noexcept;
31+
32+
CID(const CID &cid) = default;
33+
34+
CID(Version version,
35+
libp2p::multi::MulticodecType::Code content_type,
36+
libp2p::multi::Multihash content_address);
37+
38+
~CID() = default;
39+
40+
CID &operator=(const CID&) = default;
41+
42+
CID &operator=(CID &&cid) noexcept;
43+
44+
CID &operator=(const ContentIdentifier &cid);
45+
46+
CID &operator=(ContentIdentifier &&cid);
47+
2648
/**
2749
* @brief string-encodes cid
2850
* @return encoded value or error

test/testutil/cbor.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ void expectEncodeAndReencode(const T &value,
2323
}
2424

2525
inline auto operator""_cid(const char *c, size_t s) {
26-
return libp2p::multi::ContentIdentifierCodec::decode(
27-
fc::common::unhex(std::string_view(c, s)).value())
28-
.value();
26+
auto cid = libp2p::multi::ContentIdentifierCodec::decode(
27+
fc::common::unhex(std::string_view(c, s)).value())
28+
.value();
29+
return fc::CID{std::move(cid)};
2930
}
3031

3132
#endif // CPP_FILECOIN_TEST_TESTUTIL_CBOR_HPP

0 commit comments

Comments
 (0)