From fa898e6836a8fc2c7b6c8c15ad21818b16a89863 Mon Sep 17 00:00:00 2001 From: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Date: Mon, 11 Dec 2023 22:33:56 +0100 Subject: [PATCH 1/2] refactor: Print verbose serialize compiler error messages --- src/.clang-format | 2 ++ src/serialize.h | 23 ++++++++++++++--------- src/span.h | 6 +++++- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/.clang-format b/src/.clang-format index 2e5d5c6449b12..f20e5ee2d4ca1 100644 --- a/src/.clang-format +++ b/src/.clang-format @@ -43,5 +43,7 @@ SpacesInAngles: false SpacesInContainerLiterals: true SpacesInCStyleCastParentheses: false SpacesInParentheses: false +BreakBeforeConceptDeclarations: Always +RequiresExpressionIndentation: OuterScope Standard: c++20 UseTab: Never diff --git a/src/serialize.h b/src/serialize.h index 263b781f21d0a..bbe6f5468292e 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -273,8 +273,8 @@ template inline void Serialize(Stream& s, int64_t a ) { ser_wri template inline void Serialize(Stream& s, uint64_t a) { ser_writedata64(s, a); } template inline void Serialize(Stream& s, const char (&a)[N]) { s.write(MakeByteSpan(a)); } template inline void Serialize(Stream& s, const unsigned char (&a)[N]) { s.write(MakeByteSpan(a)); } -template void Serialize(Stream& s, const std::array& a) { (void)/* force byte-type */UCharCast(a.data()); s.write(MakeByteSpan(a)); } -template void Serialize(Stream& s, Span span) { (void)/* force byte-type */UCharCast(span.data()); s.write(AsBytes(span)); } +template void Serialize(Stream& s, const std::array& a) { s.write(MakeByteSpan(a)); } +template void Serialize(Stream& s, Span span) { s.write(AsBytes(span)); } #ifndef CHAR_EQUALS_INT8 template void Unserialize(Stream&, char) = delete; // char serialization forbidden. Use uint8_t or int8_t @@ -290,8 +290,8 @@ template inline void Unserialize(Stream& s, int64_t& a ) { a = template inline void Unserialize(Stream& s, uint64_t& a) { a = ser_readdata64(s); } template inline void Unserialize(Stream& s, char (&a)[N]) { s.read(MakeWritableByteSpan(a)); } template inline void Unserialize(Stream& s, unsigned char (&a)[N]) { s.read(MakeWritableByteSpan(a)); } -template void Unserialize(Stream& s, std::array& a) { (void)/* force byte-type */UCharCast(a.data()); s.read(MakeWritableByteSpan(a)); } -template void Unserialize(Stream& s, Span span) { (void)/* force byte-type */UCharCast(span.data()); s.read(AsWritableBytes(span)); } +template void Unserialize(Stream& s, std::array& a) { s.read(MakeWritableByteSpan(a)); } +template void Unserialize(Stream& s, Span span) { s.read(AsWritableBytes(span)); } template inline void Serialize(Stream& s, bool a) { uint8_t f = a; ser_writedata8(s, f); } template inline void Unserialize(Stream& s, bool& a) { uint8_t f = ser_readdata8(s); a = f; } @@ -755,18 +755,23 @@ template void Serialize(Stream& os, const std::uniq template void Unserialize(Stream& os, std::unique_ptr& p); - /** * If none of the specialized versions above matched, default to calling member function. */ -template -inline void Serialize(Stream& os, const T& a) +template +concept Serializable = requires(T a, Stream s) { a.Serialize(s); }; +template + requires Serializable +void Serialize(Stream& os, const T& a) { a.Serialize(os); } -template -inline void Unserialize(Stream& is, T&& a) +template +concept Unserializable = requires(T a, Stream s) { a.Unserialize(s); }; +template + requires Unserializable +void Unserialize(Stream& is, T&& a) { a.Unserialize(is); } diff --git a/src/span.h b/src/span.h index 2e8da27cde268..2c27a54fc764a 100644 --- a/src/span.h +++ b/src/span.h @@ -8,6 +8,7 @@ #include #include #include +#include #include #ifdef DEBUG @@ -283,13 +284,16 @@ Span MakeWritableByteSpan(V&& v) noexcept return AsWritableBytes(Span{std::forward(v)}); } -// Helper functions to safely cast to unsigned char pointers. +// Helper functions to safely cast basic byte pointers to unsigned char pointers. inline unsigned char* UCharCast(char* c) { return reinterpret_cast(c); } inline unsigned char* UCharCast(unsigned char* c) { return c; } inline unsigned char* UCharCast(std::byte* c) { return reinterpret_cast(c); } inline const unsigned char* UCharCast(const char* c) { return reinterpret_cast(c); } inline const unsigned char* UCharCast(const unsigned char* c) { return c; } inline const unsigned char* UCharCast(const std::byte* c) { return reinterpret_cast(c); } +// Helper concept for the basic byte types. +template +concept BasicByte = requires { UCharCast(std::span{}.data()); }; // Helper function to safely convert a Span to a Span<[const] unsigned char>. template constexpr auto UCharSpanCast(Span s) -> Span::type> { return {UCharCast(s.data()), s.size()}; } From fae526345de539ab8f9b80100f6dfbe8e1d3284b Mon Sep 17 00:00:00 2001 From: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Date: Mon, 11 Dec 2023 22:31:10 +0100 Subject: [PATCH 2/2] Allow std::byte C-style array serialization --- src/serialize.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/serialize.h b/src/serialize.h index bbe6f5468292e..19585c630ab2a 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -271,8 +271,7 @@ template inline void Serialize(Stream& s, int32_t a ) { ser_wri template inline void Serialize(Stream& s, uint32_t a) { ser_writedata32(s, a); } template inline void Serialize(Stream& s, int64_t a ) { ser_writedata64(s, a); } template inline void Serialize(Stream& s, uint64_t a) { ser_writedata64(s, a); } -template inline void Serialize(Stream& s, const char (&a)[N]) { s.write(MakeByteSpan(a)); } -template inline void Serialize(Stream& s, const unsigned char (&a)[N]) { s.write(MakeByteSpan(a)); } +template void Serialize(Stream& s, const B (&a)[N]) { s.write(MakeByteSpan(a)); } template void Serialize(Stream& s, const std::array& a) { s.write(MakeByteSpan(a)); } template void Serialize(Stream& s, Span span) { s.write(AsBytes(span)); } @@ -288,8 +287,7 @@ template inline void Unserialize(Stream& s, int32_t& a ) { a = template inline void Unserialize(Stream& s, uint32_t& a) { a = ser_readdata32(s); } template inline void Unserialize(Stream& s, int64_t& a ) { a = ser_readdata64(s); } template inline void Unserialize(Stream& s, uint64_t& a) { a = ser_readdata64(s); } -template inline void Unserialize(Stream& s, char (&a)[N]) { s.read(MakeWritableByteSpan(a)); } -template inline void Unserialize(Stream& s, unsigned char (&a)[N]) { s.read(MakeWritableByteSpan(a)); } +template void Unserialize(Stream& s, B (&a)[N]) { s.read(MakeWritableByteSpan(a)); } template void Unserialize(Stream& s, std::array& a) { s.read(MakeWritableByteSpan(a)); } template void Unserialize(Stream& s, Span span) { s.read(AsWritableBytes(span)); }