Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions include/msgpack23/msgpack23.h
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,24 @@ namespace msgpack23 {
value.assign(src, src + bin_size);
increment(bin_size);
}

// Zero-copy alternative to the vector overload: returns a view into the
// internal buffer instead of copying. Valid only for the Unpacker's lifetime.
template<std::size_t E>
void unpack_type(std::span<B const, E> &value) {
std::size_t bin_size = 0;
if (read_conditional<FormatConstants::bin32, std::uint32_t>(bin_size)
or read_conditional<FormatConstants::bin16, std::uint16_t>(bin_size)
or read_conditional<FormatConstants::bin8, std::uint8_t>(bin_size)) {
} else {
throw std::logic_error("Unexpected value");
}
if (position_ + bin_size > data_.size()) {
throw std::out_of_range("Span position is out of range");
}
value = data_.subspan(position_, bin_size);
increment(bin_size);
}
};

template<typename T>
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ add_executable(
main.cpp
map_tests.cpp
object_packing_tests.cpp
span_tests.cpp
string_tests.cpp
type_packing_tests.cpp
uint8_tests.cpp
Expand Down
52 changes: 52 additions & 0 deletions tests/span_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// Created by Neara Software on 28/02/2026.
//

#include <gtest/gtest.h>
#include <msgpack23/msgpack23.h>

namespace {
class msgpack23_span : public testing::TestWithParam<std::size_t> {
};

TEST_P(msgpack23_span, binarySpanRoundTrip) {
std::vector<std::uint8_t> expected{};
for (std::size_t i = 0; i < GetParam(); ++i) {
expected.emplace_back(static_cast<std::uint8_t>(i));
}
std::vector<std::uint8_t> data{};
msgpack23::Packer packer{std::back_insert_iterator(data)};
packer(expected);

msgpack23::Unpacker unpacker{data};
std::span<const std::uint8_t> actual{};
unpacker(actual);

ASSERT_EQ(actual.size(), expected.size());
EXPECT_TRUE(std::equal(actual.begin(), actual.end(), expected.begin()));
}

TEST_P(msgpack23_span, binarySpanIsZeroCopy) {
std::vector<std::uint8_t> expected(GetParam(), 0x42);
std::vector<std::uint8_t> data{};
msgpack23::Packer packer{std::back_insert_iterator(data)};
packer(expected);

msgpack23::Unpacker unpacker{data};
std::span<const std::uint8_t> actual{};
unpacker(actual);

// Span must point into the packed buffer, not a separate allocation
EXPECT_GE(actual.data(), data.data());
EXPECT_LE(actual.data() + actual.size(), data.data() + data.size());
}

constexpr std::size_t span_sizes[] = {
1,
std::numeric_limits<std::uint8_t>::max() - 1, // bin8 near-max
std::numeric_limits<std::uint8_t>::max() + 1, // bin16 boundary
std::numeric_limits<std::uint16_t>::max() - 1, // bin16 near-max
std::numeric_limits<std::uint16_t>::max() + 1, // bin32 boundary
};
INSTANTIATE_TEST_SUITE_P(SomeValuesTest, msgpack23_span, testing::ValuesIn(span_sizes));
}