Skip to content

Commit fa257bc

Browse files
author
MarcoFalke
committed
util: Allow std::byte and char Span serialization
1 parent 679f825 commit fa257bc

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/serialize.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ template<typename X> const X& ReadWriteAsHelper(const X& x) { return x; }
188188
} \
189189
FORMATTER_METHODS(cls, obj)
190190

191+
// clang-format off
191192
#ifndef CHAR_EQUALS_INT8
192193
template <typename Stream> void Serialize(Stream&, char) = delete; // char serialization forbidden. Use uint8_t or int8_t
193194
#endif
@@ -201,8 +202,7 @@ template<typename Stream> inline void Serialize(Stream& s, int64_t a ) { ser_wri
201202
template<typename Stream> inline void Serialize(Stream& s, uint64_t a) { ser_writedata64(s, a); }
202203
template<typename Stream, int N> inline void Serialize(Stream& s, const char (&a)[N]) { s.write(MakeByteSpan(a)); }
203204
template<typename Stream, int N> inline void Serialize(Stream& s, const unsigned char (&a)[N]) { s.write(MakeByteSpan(a)); }
204-
template<typename Stream> inline void Serialize(Stream& s, const Span<const unsigned char>& span) { s.write(AsBytes(span)); }
205-
template<typename Stream> inline void Serialize(Stream& s, const Span<unsigned char>& span) { s.write(AsBytes(span)); }
205+
template <typename Stream, typename B> void Serialize(Stream& s, Span<B> span) { (void)/* force byte-type */UCharCast(span.data()); s.write(AsBytes(span)); }
206206

207207
#ifndef CHAR_EQUALS_INT8
208208
template <typename Stream> void Unserialize(Stream&, char) = delete; // char serialization forbidden. Use uint8_t or int8_t
@@ -217,10 +217,11 @@ template<typename Stream> inline void Unserialize(Stream& s, int64_t& a ) { a =
217217
template<typename Stream> inline void Unserialize(Stream& s, uint64_t& a) { a = ser_readdata64(s); }
218218
template<typename Stream, int N> inline void Unserialize(Stream& s, char (&a)[N]) { s.read(MakeWritableByteSpan(a)); }
219219
template<typename Stream, int N> inline void Unserialize(Stream& s, unsigned char (&a)[N]) { s.read(MakeWritableByteSpan(a)); }
220-
template<typename Stream> inline void Unserialize(Stream& s, Span<unsigned char>& span) { s.read(AsWritableBytes(span)); }
220+
template <typename Stream, typename B> void Unserialize(Stream& s, Span<B> span) { (void)/* force byte-type */UCharCast(span.data()); s.read(AsWritableBytes(span)); }
221221

222222
template <typename Stream> inline void Serialize(Stream& s, bool a) { uint8_t f = a; ser_writedata8(s, f); }
223223
template <typename Stream> inline void Unserialize(Stream& s, bool& a) { uint8_t f = ser_readdata8(s); a = f; }
224+
// clang-format on
224225

225226

226227
/**

src/test/serialize_tests.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,15 @@ BOOST_AUTO_TEST_CASE(class_methods)
241241
ss2 << intval << boolval << stringval << charstrval << txval;
242242
ss2 >> methodtest3;
243243
BOOST_CHECK(methodtest3 == methodtest4);
244+
{
245+
DataStream ds;
246+
const std::string in{"ab"};
247+
ds << Span{in};
248+
std::array<std::byte, 2> out;
249+
ds >> Span{out};
250+
BOOST_CHECK_EQUAL(out.at(0), std::byte{'a'});
251+
BOOST_CHECK_EQUAL(out.at(1), std::byte{'b'});
252+
}
244253
}
245254

246255
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)