diff --git a/src/random.cpp b/src/random.cpp index da56430920..8b6ea488bf 100644 --- a/src/random.cpp +++ b/src/random.cpp @@ -630,15 +630,18 @@ uint256 FastRandomContext::rand256() noexcept return ret; } -std::vector FastRandomContext::randbytes(size_t len) +template +std::vector FastRandomContext::randbytes(size_t len) { if (requires_seed) RandomSeed(); - std::vector ret(len); + std::vector ret(len); if (len > 0) { - rng.Keystream(ret.data(), len); + rng.Keystream(UCharCast(ret.data()), len); } return ret; } +template std::vector FastRandomContext::randbytes(size_t); +template std::vector FastRandomContext::randbytes(size_t); void FastRandomContext::fillrand(Span output) { diff --git a/src/random.h b/src/random.h index f1b31aae66..6b096b4776 100644 --- a/src/random.h +++ b/src/random.h @@ -200,7 +200,8 @@ public: } /** Generate random bytes. */ - std::vector randbytes(size_t len); + template + std::vector randbytes(size_t len); /** Fill a byte Span with random bytes. */ void fillrand(Span output); diff --git a/src/serialize.h b/src/serialize.h index c07cc81073..ef3053b717 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -196,6 +196,7 @@ template const X& ReadWriteAsHelper(const X& x) { return x; } #ifndef CHAR_EQUALS_INT8 template void Serialize(Stream&, char) = delete; // char serialization forbidden. Use uint8_t or int8_t #endif +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); } template inline void Serialize(Stream& s, int16_t a ) { ser_writedata16(s, a); } @@ -211,6 +212,7 @@ template void Serialize(Stream& s, Span 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& 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); } template inline void Unserialize(Stream& s, int16_t& a ) { a = ser_readdata16(s); } diff --git a/src/test/serialize_tests.cpp b/src/test/serialize_tests.cpp index 0dcf9f4ec4..a91fa9297e 100644 --- a/src/test/serialize_tests.cpp +++ b/src/test/serialize_tests.cpp @@ -295,11 +295,13 @@ BOOST_AUTO_TEST_CASE(class_methods) { DataStream ds; const std::string in{"ab"}; - ds << Span{in}; + ds << Span{in} << std::byte{'c'}; std::array out; - ds >> Span{out}; + std::byte out_3; + ds >> Span{out} >> out_3; BOOST_CHECK_EQUAL(out.at(0), std::byte{'a'}); BOOST_CHECK_EQUAL(out.at(1), std::byte{'b'}); + BOOST_CHECK_EQUAL(out_3, std::byte{'c'}); } }