Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[struct_pack][fix] fix mod zero error when struct only contain compat… #769

Merged
merged 2 commits into from
Sep 11, 2024
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
2 changes: 1 addition & 1 deletion include/ylt/struct_pack/alignment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ constexpr auto calculate_padding_size() {
std::array<std::size_t, struct_pack::members_count<T> + 1> padding_size{};
std::size_t offset = 0;
for_each<T, calculate_padding_size_impl>(offset, padding_size);
if (offset % align::alignment_v<T>) {
if (align::alignment_v < T >> 0 && offset % align::alignment_v<T>) {
padding_size[struct_pack::members_count<T>] =
align::alignment_v<T> - offset % align::alignment_v<T>;
}
Expand Down
14 changes: 13 additions & 1 deletion src/struct_pack/tests/test_compatible.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "doctest.h"
#include "test_struct.hpp"
#include "ylt/struct_pack/compatible.hpp"
#include "ylt/struct_pack/endian_wrapper.hpp"

using namespace struct_pack;
Expand Down Expand Up @@ -1307,4 +1308,15 @@ TEST_CASE("test nested trival_serialzable_obj_with_compatible") {
CHECK(result.has_value());
CHECK(test_equal(result.value(), a_v1));
}
}
}

struct only_compatible {
struct_pack::compatible<int> hi;
};
TEST_CASE("test only_compatible") {
only_compatible o{0};
auto buffer = struct_pack::serialize(o);
auto result = struct_pack::deserialize<only_compatible>(buffer);
CHECK(result.has_value());
CHECK(result->hi == o.hi);
}
Loading