Skip to content

Commit

Permalink
pw_protobuf: Allow copying of const callbacks
Browse files Browse the repository at this point in the history
This fixes a bug where it wasn't possible to copy a const message
structure, the callback was intended to be "copyable as empty" but was
missing a 'const' in the right place.

Change-Id: I109d14ec16a5dcc7845654113419036ee6007117
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/92760
Pigweed-Auto-Submit: Scott James Remnant <keybuk@google.com>
Commit-Queue: Auto-Submit <auto-submit@pigweed.google.com.iam.gserviceaccount.com>
Reviewed-by: Armando Montanez <amontanez@google.com>
  • Loading branch information
keybuk authored and CQ Bot Account committed Apr 29, 2022
1 parent b9e428c commit 04824b6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
34 changes: 34 additions & 0 deletions pw_protobuf/codegen_message_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,40 @@ TEST(CodegenMessage, Inequality) {
EXPECT_FALSE(one == two);
}

TEST(CodegenMessage, ConstCopyable) {
const Pigweed::Message one{
.magic_number = 0x49u,
.ziggy = -111,
.cycles = 0x40302010fecaaddeu,
.ratio = -1.42f,
.error_message = "not a typewriter",
.pigweed = {.status = Bool::FILE_NOT_FOUND},
.bin = Pigweed::Protobuf::Binary::ZERO,
.proto = {.bin = Proto::Binary::OFF,
.pigweed_pigweed_bin = Pigweed::Pigweed::Binary::ZERO,
.pigweed_protobuf_bin = Pigweed::Protobuf::Binary::ZERO,
.meta =
{
.file_name = "/etc/passwd",
.status = Pigweed::Protobuf::Compiler::Status::FUBAR,
.protobuf_bin = Pigweed::Protobuf::Binary::ONE,
.pigweed_bin = Pigweed::Pigweed::Binary::ONE,
}},
.data = {std::byte{0x10},
std::byte{0x20},
std::byte{0x30},
std::byte{0x40},
std::byte{0x50},
std::byte{0x60},
std::byte{0x70},
std::byte{0x80}},
.bungle = -111,
};
Pigweed::Message two = one;

EXPECT_TRUE(one == two);
}

PW_MODIFY_DIAGNOSTICS_POP();

TEST(CodegenMessage, Read) {
Expand Down
2 changes: 1 addition & 1 deletion pw_protobuf/public/pw_protobuf/internal/codegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ union Callback {
constexpr Callback& operator=(Callback&& other) = default;

// Copying a callback does not copy the functions.
constexpr Callback(Callback&) : encode_() {}
constexpr Callback(const Callback&) : encode_() {}
constexpr Callback& operator=(const Callback&) {
encode_ = nullptr;
return *this;
Expand Down

0 comments on commit 04824b6

Please sign in to comment.