From 04824b639847ea2bcf4920b0d8b9eeb62f142245 Mon Sep 17 00:00:00 2001 From: Scott James Remnant Date: Thu, 28 Apr 2022 17:27:08 -0700 Subject: [PATCH] pw_protobuf: Allow copying of const callbacks 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 Commit-Queue: Auto-Submit Reviewed-by: Armando Montanez --- pw_protobuf/codegen_message_test.cc | 34 +++++++++++++++++++ .../public/pw_protobuf/internal/codegen.h | 2 +- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/pw_protobuf/codegen_message_test.cc b/pw_protobuf/codegen_message_test.cc index e39939ef7..f6d84bfd4 100644 --- a/pw_protobuf/codegen_message_test.cc +++ b/pw_protobuf/codegen_message_test.cc @@ -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) { diff --git a/pw_protobuf/public/pw_protobuf/internal/codegen.h b/pw_protobuf/public/pw_protobuf/internal/codegen.h index 5d4287c7b..3f6cf2dd5 100644 --- a/pw_protobuf/public/pw_protobuf/internal/codegen.h +++ b/pw_protobuf/public/pw_protobuf/internal/codegen.h @@ -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;