Skip to content

Commit dca0f23

Browse files
committed
Merge bitcoin#29056: refactor: Print verbose serialize compiler error messages
fae5263 Allow std::byte C-style array serialization (MarcoFalke) fa898e6 refactor: Print verbose serialize compiler error messages (MarcoFalke) Pull request description: Currently, trying to serialize an object that can't be serialized will fail with a short error message. For example, the diff and the error message: ```diff diff --git a/src/test/serialize_tests.cpp b/src/test/serialize_tests.cpp index d75eb499b4..773f49845b 100644 --- a/src/test/serialize_tests.cpp +++ b/src/test/serialize_tests.cpp @@ -62,6 +62,8 @@ public: BOOST_AUTO_TEST_CASE(sizes) { + int b[4]; + DataStream{} << b << Span{b}; BOOST_CHECK_EQUAL(sizeof(unsigned char), GetSerializeSize((unsigned char)0)); BOOST_CHECK_EQUAL(sizeof(int8_t), GetSerializeSize(int8_t(0))); BOOST_CHECK_EQUAL(sizeof(uint8_t), GetSerializeSize(uint8_t(0))); ``` ``` ./serialize.h:765:6: error: member reference base type 'const int[4]' is not a structure or union 765 | a.Serialize(os); | ~^~~~~~~~~~ ``` ``` ./serialize.h:277:109: error: no matching function for call to 'UCharCast' 277 | template <typename Stream, typename B> void Serialize(Stream& s, Span<B> span) { (void)/* force byte-type */UCharCast(span.data()); s.write(AsBytes(span)); } | ^~~~~~~~~ ``` This is fine. However, it would be more helpful for developers and more accurate by the compiler to explain why each function is not selected. Fix this by using C++20 concepts where appropriate. ACKs for top commit: ajtowns: reACK fae5263 achow101: ACK fae5263 TheCharlatan: Re-ACK fae5263 Tree-SHA512: e03a684ccfcc5fbcad7f8a4899945a05989b555175fdcaebdb113aff46b52b4ee7b467192748edf99c5c348a620f8e52ab98bed3f3fca88280a64dbca458fe8a
2 parents eefe4ba + fae5263 commit dca0f23

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

src/.clang-format

+2
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,7 @@ SpacesInAngles: false
4343
SpacesInContainerLiterals: true
4444
SpacesInCStyleCastParentheses: false
4545
SpacesInParentheses: false
46+
BreakBeforeConceptDeclarations: Always
47+
RequiresExpressionIndentation: OuterScope
4648
Standard: c++20
4749
UseTab: Never

src/serialize.h

+16-13
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,9 @@ template<typename Stream> inline void Serialize(Stream& s, int32_t a ) { ser_wri
271271
template<typename Stream> inline void Serialize(Stream& s, uint32_t a) { ser_writedata32(s, a); }
272272
template<typename Stream> inline void Serialize(Stream& s, int64_t a ) { ser_writedata64(s, a); }
273273
template<typename Stream> inline void Serialize(Stream& s, uint64_t a) { ser_writedata64(s, a); }
274-
template<typename Stream, int N> inline void Serialize(Stream& s, const char (&a)[N]) { s.write(MakeByteSpan(a)); }
275-
template<typename Stream, int N> inline void Serialize(Stream& s, const unsigned char (&a)[N]) { s.write(MakeByteSpan(a)); }
276-
template <typename Stream, typename B, std::size_t N> void Serialize(Stream& s, const std::array<B, N>& a) { (void)/* force byte-type */UCharCast(a.data()); s.write(MakeByteSpan(a)); }
277-
template <typename Stream, typename B> void Serialize(Stream& s, Span<B> span) { (void)/* force byte-type */UCharCast(span.data()); s.write(AsBytes(span)); }
274+
template <typename Stream, BasicByte B, int N> void Serialize(Stream& s, const B (&a)[N]) { s.write(MakeByteSpan(a)); }
275+
template <typename Stream, BasicByte B, std::size_t N> void Serialize(Stream& s, const std::array<B, N>& a) { s.write(MakeByteSpan(a)); }
276+
template <typename Stream, BasicByte B> void Serialize(Stream& s, Span<B> span) { s.write(AsBytes(span)); }
278277

279278
#ifndef CHAR_EQUALS_INT8
280279
template <typename Stream> void Unserialize(Stream&, char) = delete; // char serialization forbidden. Use uint8_t or int8_t
@@ -288,10 +287,9 @@ template<typename Stream> inline void Unserialize(Stream& s, int32_t& a ) { a =
288287
template<typename Stream> inline void Unserialize(Stream& s, uint32_t& a) { a = ser_readdata32(s); }
289288
template<typename Stream> inline void Unserialize(Stream& s, int64_t& a ) { a = ser_readdata64(s); }
290289
template<typename Stream> inline void Unserialize(Stream& s, uint64_t& a) { a = ser_readdata64(s); }
291-
template<typename Stream, int N> inline void Unserialize(Stream& s, char (&a)[N]) { s.read(MakeWritableByteSpan(a)); }
292-
template<typename Stream, int N> inline void Unserialize(Stream& s, unsigned char (&a)[N]) { s.read(MakeWritableByteSpan(a)); }
293-
template <typename Stream, typename B, std::size_t N> void Unserialize(Stream& s, std::array<B, N>& a) { (void)/* force byte-type */UCharCast(a.data()); s.read(MakeWritableByteSpan(a)); }
294-
template <typename Stream, typename B> void Unserialize(Stream& s, Span<B> span) { (void)/* force byte-type */UCharCast(span.data()); s.read(AsWritableBytes(span)); }
290+
template <typename Stream, BasicByte B, int N> void Unserialize(Stream& s, B (&a)[N]) { s.read(MakeWritableByteSpan(a)); }
291+
template <typename Stream, BasicByte B, std::size_t N> void Unserialize(Stream& s, std::array<B, N>& a) { s.read(MakeWritableByteSpan(a)); }
292+
template <typename Stream, BasicByte B> void Unserialize(Stream& s, Span<B> span) { s.read(AsWritableBytes(span)); }
295293

296294
template <typename Stream> inline void Serialize(Stream& s, bool a) { uint8_t f = a; ser_writedata8(s, f); }
297295
template <typename Stream> inline void Unserialize(Stream& s, bool& a) { uint8_t f = ser_readdata8(s); a = f; }
@@ -755,18 +753,23 @@ template<typename Stream, typename T> void Serialize(Stream& os, const std::uniq
755753
template<typename Stream, typename T> void Unserialize(Stream& os, std::unique_ptr<const T>& p);
756754

757755

758-
759756
/**
760757
* If none of the specialized versions above matched, default to calling member function.
761758
*/
762-
template<typename Stream, typename T>
763-
inline void Serialize(Stream& os, const T& a)
759+
template <class T, class Stream>
760+
concept Serializable = requires(T a, Stream s) { a.Serialize(s); };
761+
template <typename Stream, typename T>
762+
requires Serializable<T, Stream>
763+
void Serialize(Stream& os, const T& a)
764764
{
765765
a.Serialize(os);
766766
}
767767

768-
template<typename Stream, typename T>
769-
inline void Unserialize(Stream& is, T&& a)
768+
template <class T, class Stream>
769+
concept Unserializable = requires(T a, Stream s) { a.Unserialize(s); };
770+
template <typename Stream, typename T>
771+
requires Unserializable<T, Stream>
772+
void Unserialize(Stream& is, T&& a)
770773
{
771774
a.Unserialize(is);
772775
}

src/span.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <algorithm>
99
#include <cassert>
1010
#include <cstddef>
11+
#include <span>
1112
#include <type_traits>
1213

1314
#ifdef DEBUG
@@ -283,13 +284,16 @@ Span<std::byte> MakeWritableByteSpan(V&& v) noexcept
283284
return AsWritableBytes(Span{std::forward<V>(v)});
284285
}
285286

286-
// Helper functions to safely cast to unsigned char pointers.
287+
// Helper functions to safely cast basic byte pointers to unsigned char pointers.
287288
inline unsigned char* UCharCast(char* c) { return reinterpret_cast<unsigned char*>(c); }
288289
inline unsigned char* UCharCast(unsigned char* c) { return c; }
289290
inline unsigned char* UCharCast(std::byte* c) { return reinterpret_cast<unsigned char*>(c); }
290291
inline const unsigned char* UCharCast(const char* c) { return reinterpret_cast<const unsigned char*>(c); }
291292
inline const unsigned char* UCharCast(const unsigned char* c) { return c; }
292293
inline const unsigned char* UCharCast(const std::byte* c) { return reinterpret_cast<const unsigned char*>(c); }
294+
// Helper concept for the basic byte types.
295+
template <typename B>
296+
concept BasicByte = requires { UCharCast(std::span<B>{}.data()); };
293297

294298
// Helper function to safely convert a Span to a Span<[const] unsigned char>.
295299
template <typename T> constexpr auto UCharSpanCast(Span<T> s) -> Span<typename std::remove_pointer<decltype(UCharCast(s.data()))>::type> { return {UCharCast(s.data()), s.size()}; }

0 commit comments

Comments
 (0)