Follow-up to #21969.
Serialize doesn’t care whether one of the bits in a char
is a sign bit or not, but having the possibility is slightly confusing to calling code. int8_t
and uint8_t
can be used as replacement to char
.
Follow-up to #21969.
Serialize doesn’t care whether one of the bits in a char
is a sign bit or not, but having the possibility is slightly confusing to calling code. int8_t
and uint8_t
can be used as replacement to char
.
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
Reviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.
61 * Lowest-level serialization and conversion.
62 * @note Sizes of these types are verified in the tests
63 */
64 template<typename Stream> inline void ser_writedata8(Stream &s, uint8_t obj)
65 {
66- s.write((char*)&obj, 1);
Uint8Ptr
unnecessary as this is already uint8_t*
16@@ -17,7 +17,7 @@
17 static void DeserializeBlockTest(benchmark::Bench& bench)
18 {
19 CDataStream stream(benchmark::data::block413567, SER_NETWORK, PROTOCOL_VERSION);
20- char a = '\0';
21+ uint8_t a{'\0'};
uint8_t a{0};
) more naturally looking than a char literal.
194@@ -196,7 +195,7 @@ template<typename X> const X& ReadWriteAsHelper(const X& x) { return x; }
195 FORMATTER_METHODS(cls, obj)
196
197 #ifndef CHAR_EQUALS_INT8
198-template<typename Stream> inline void Serialize(Stream& s, char a ) { ser_writedata8(s, a); } // TODO Get rid of bare char
199+template<typename Stream> void Serialize(Stream&, char) { static_assert(ALWAYS_FALSE<Stream>, "char serialization forbidden use uint8_t or int8_t"); }
template<typename Stream> void Serialize(Stream&, char) = delete;
(though perhaps with a slightly less clear error message).
Concept ACK, though I wonder if this alternative approach isn’t better:
C++20 std::span
has std::as_bytes
and std::as_writable_bytes
functions to convert spans to equivalent spans-to-byte-representation. While we don’t have std::byte
yet, we could introduce equivalent operations that use unsigned char
instead.
If the stream write/read functions were changed to take Span<(const) unsigned char>
, many of the cases here could very naturally be written as s.write(AsWritableUChar(Span{arg}));
for example.
That needs more invasive changes than what you’re aiming for here, but perhaps ones we want to aim for anyway?
If the stream write/read functions were changed to take Span<(const) unsigned char>, many of the cases here could very naturally be written as s.write(AsWritableUChar(Span{arg})); for example.
Ok, will do that instead. It will require changing twice as many lines of code, but given that we are starting to use spans consistently, it will likely happen anyway at some point. Combining this Span change with std::byte is “free” (doesn’t require a larger diff).