Add SpanWriter class for zero-allocation stream writing

Co-authored-by: stickies-v <stickies-v@protonmail.com>
This commit is contained in:
yuvicc
2026-04-01 20:11:57 +05:30
parent d4bc620ad8
commit 86662623ec
2 changed files with 54 additions and 0 deletions

View File

@@ -207,6 +207,28 @@ BOOST_AUTO_TEST_CASE(streams_vector_writer)
vch.clear();
}
BOOST_AUTO_TEST_CASE(streams_span_writer)
{
unsigned char a(1);
unsigned char b(2);
unsigned char bytes[] = {3, 4, 5, 6};
std::array<std::byte, 8> arr{};
// Test operator<<
SpanWriter writer{arr};
writer << a << b;
BOOST_CHECK_EQUAL(HexStr(arr), "0102000000000000");
// Use variadic constructor and write to subspan.
SpanWriter{std::span{arr}.subspan(2), a, bytes, b};
BOOST_CHECK_EQUAL(HexStr(arr), "0102010304050602");
// Writing past the end throws
std::array<std::byte, 1> small{};
BOOST_CHECK_THROW(SpanWriter(std::span{small}, a, b), std::ios_base::failure);
BOOST_CHECK_THROW(SpanWriter(std::span{small}) << a << b, std::ios_base::failure);
}
BOOST_AUTO_TEST_CASE(streams_vector_reader)
{
std::vector<unsigned char> vch = {1, 255, 3, 4, 5, 6};