From ad7584d8b60119ca3717117a1eb6a16d753c5d74 Mon Sep 17 00:00:00 2001 From: Cory Fields Date: Mon, 26 Feb 2024 19:52:14 +0000 Subject: serialization: replace char-is-int8_t autoconf detection with c++20 concept This removes the only remaining autoconf macro in our serialization code, so it can now be used trivially and safely out-of-tree. --- src/serialize.h | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'src/serialize.h') diff --git a/src/serialize.h b/src/serialize.h index 7b336ce1af..ab57376a57 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -6,10 +6,6 @@ #ifndef BITCOIN_SERIALIZE_H #define BITCOIN_SERIALIZE_H -#if defined(HAVE_CONFIG_H) -#include -#endif - #include #include // IWYU pragma: keep #include @@ -17,6 +13,7 @@ #include #include +#include #include #include #include @@ -263,9 +260,14 @@ const Out& AsBase(const In& x) // i.e. anything that supports .read(Span) and .write(Span) // // clang-format off -#ifndef CHAR_EQUALS_INT8 -template void Serialize(Stream&, char) = delete; // char serialization forbidden. Use uint8_t or int8_t -#endif + +// Typically int8_t and char are distinct types, but some systems may define int8_t +// in terms of char. Forbid serialization of char in the typical case, but allow it if +// it's the only way to describe an int8_t. +template +concept CharNotInt8 = std::same_as && !std::same_as; + +template void Serialize(Stream&, V) = delete; // char serialization forbidden. Use uint8_t or int8_t template void Serialize(Stream& s, std::byte a) { ser_writedata8(s, uint8_t(a)); } template inline void Serialize(Stream& s, int8_t a ) { ser_writedata8(s, a); } template inline void Serialize(Stream& s, uint8_t a ) { ser_writedata8(s, a); } @@ -279,9 +281,7 @@ template void Serialize(Stream& s, const B 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 -#endif +template void Unserialize(Stream&, V) = delete; // char serialization forbidden. Use uint8_t or int8_t template void Unserialize(Stream& s, std::byte& a) { a = std::byte{ser_readdata8(s)}; } template inline void Unserialize(Stream& s, int8_t& a ) { a = ser_readdata8(s); } template inline void Unserialize(Stream& s, uint8_t& a ) { a = ser_readdata8(s); } -- cgit v1.2.3