Fix debug_test to work with C++ lite
This change adds a cfg attribute 'cpp_lite' to the C++ kernel of Protobuf Rust. If C++ lite is selected on the command line, the cfg attribute 'cpp_lite' is set. The root cause of the test failure was that the Debug implementation for full C++ protos uses text proto which is not available in C++ lite. The fix uses the 'cpp_lite' cfg attribute to select a different Debug implementation that doesn't rely on text proto
PiperOrigin-RevId: 640552701
diff --git a/rust/cpp.rs b/rust/cpp.rs
index 1a623ee..44451db 100644
--- a/rust/cpp.rs
+++ b/rust/cpp.rs
@@ -228,12 +228,17 @@
extern "C" {
fn utf8_debug_string(msg: RawMessage) -> RustStringRawParts;
+ fn utf8_debug_string_lite(msg: RawMessage) -> RustStringRawParts;
}
pub fn debug_string(_private: Private, msg: RawMessage, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// SAFETY:
// - `msg` is a valid protobuf message.
+ #[cfg(not(lite_runtime))]
let dbg_str: String = unsafe { utf8_debug_string(msg) }.into();
+ #[cfg(lite_runtime)]
+ let dbg_str: String = unsafe { utf8_debug_string_lite(msg) }.into();
+
write!(f, "{dbg_str}")
}
diff --git a/rust/cpp_kernel/cpp_api.cc b/rust/cpp_kernel/cpp_api.cc
index 753331c..a4ba3fe 100644
--- a/rust/cpp_kernel/cpp_api.cc
+++ b/rust/cpp_kernel/cpp_api.cc
@@ -7,6 +7,7 @@
#include "google/protobuf/map.h"
#include "google/protobuf/message.h"
+#include "google/protobuf/message_lite.h"
#include "google/protobuf/repeated_field.h"
#include "google/protobuf/repeated_ptr_field.h"
@@ -141,6 +142,12 @@
std::string text = google::protobuf::Utf8Format(*msg);
return google::protobuf::rust_internal::RustStringRawParts(text);
}
+
+google::protobuf::rust_internal::RustStringRawParts utf8_debug_string_lite(
+ const google::protobuf::MessageLite* msg) {
+ std::string text = google::protobuf::Utf8Format(*msg);
+ return google::protobuf::rust_internal::RustStringRawParts(text);
+}
}
namespace google {
diff --git a/rust/cpp_kernel/cpp_api.h b/rust/cpp_kernel/cpp_api.h
index 8ee8ebc..9345a01 100644
--- a/rust/cpp_kernel/cpp_api.h
+++ b/rust/cpp_kernel/cpp_api.h
@@ -179,6 +179,8 @@
};
extern "C" RustStringRawParts utf8_debug_string(const google::protobuf::Message* msg);
+extern "C" RustStringRawParts utf8_debug_string_lite(
+ const google::protobuf::MessageLite* msg);
} // namespace rust_internal
} // namespace protobuf
} // namespace google
diff --git a/rust/test/cpp/debug_test.rs b/rust/test/cpp/debug_test.rs
index 6fb45a5..9da6289 100644
--- a/rust/test/cpp/debug_test.rs
+++ b/rust/test/cpp/debug_test.rs
@@ -1,6 +1,7 @@
use debug_rust_proto::DebugMsg;
use googletest::prelude::*;
+#[cfg(not(lite_runtime))]
#[test]
fn test_debug() {
let mut msg = DebugMsg::new();
@@ -10,3 +11,14 @@
assert_that!(format!("{msg:?}"), contains_substring("id: 1"));
assert_that!(format!("{msg:?}"), not(contains_substring("password")));
}
+
+#[cfg(lite_runtime)]
+#[test]
+fn test_debug_lite() {
+ let mut msg = DebugMsg::new();
+ msg.set_id(1);
+ msg.set_secret_user_data("password");
+
+ assert_that!(format!("{msg:?}"), contains_substring("MessageLite"));
+ assert_that!(format!("{msg:?}"), not(contains_substring("password")));
+}