Skip to content

Commit fac6af1

Browse files
author
MarcoFalke
committed
Allow std::byte serialization
1 parent fade43e commit fac6af1

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

src/serialize.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ template<typename X> const X& ReadWriteAsHelper(const X& x) { return x; }
192192
#ifndef CHAR_EQUALS_INT8
193193
template <typename Stream> void Serialize(Stream&, char) = delete; // char serialization forbidden. Use uint8_t or int8_t
194194
#endif
195+
template <typename Stream> void Serialize(Stream& s, std::byte a) { ser_writedata8(s, uint8_t(a)); }
195196
template<typename Stream> inline void Serialize(Stream& s, int8_t a ) { ser_writedata8(s, a); }
196197
template<typename Stream> inline void Serialize(Stream& s, uint8_t a ) { ser_writedata8(s, a); }
197198
template<typename Stream> inline void Serialize(Stream& s, int16_t a ) { ser_writedata16(s, a); }
@@ -207,6 +208,7 @@ template <typename Stream, typename B> void Serialize(Stream& s, Span<B> span) {
207208
#ifndef CHAR_EQUALS_INT8
208209
template <typename Stream> void Unserialize(Stream&, char) = delete; // char serialization forbidden. Use uint8_t or int8_t
209210
#endif
211+
template <typename Stream> void Unserialize(Stream& s, std::byte& a) { a = std::byte{ser_readdata8(s)}; }
210212
template<typename Stream> inline void Unserialize(Stream& s, int8_t& a ) { a = ser_readdata8(s); }
211213
template<typename Stream> inline void Unserialize(Stream& s, uint8_t& a ) { a = ser_readdata8(s); }
212214
template<typename Stream> inline void Unserialize(Stream& s, int16_t& a ) { a = ser_readdata16(s); }

src/test/serialize_tests.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,13 @@ BOOST_AUTO_TEST_CASE(class_methods)
244244
{
245245
DataStream ds;
246246
const std::string in{"ab"};
247-
ds << Span{in};
247+
ds << Span{in} << std::byte{'c'};
248248
std::array<std::byte, 2> out;
249-
ds >> Span{out};
249+
std::byte out_3;
250+
ds >> Span{out} >> out_3;
250251
BOOST_CHECK_EQUAL(out.at(0), std::byte{'a'});
251252
BOOST_CHECK_EQUAL(out.at(1), std::byte{'b'});
253+
BOOST_CHECK_EQUAL(out_3, std::byte{'c'});
252254
}
253255
}
254256

0 commit comments

Comments
 (0)