diff --git a/rust/cpp.rs b/rust/cpp.rs index 1a623ee947..44451db1cc 100644 --- a/rust/cpp.rs +++ b/rust/cpp.rs @@ -228,12 +228,17 @@ impl From for String { 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 753331c287..a4ba3fe37f 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 @@ google::protobuf::rust_internal::RustStringRawParts utf8_debug_string( 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 8ee8ebc77d..9345a01c3f 100644 --- a/rust/cpp_kernel/cpp_api.h +++ b/rust/cpp_kernel/cpp_api.h @@ -179,6 +179,8 @@ struct RustStringRawParts { }; 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 6fb45a539b..9da6289345 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 @@ fn test_debug() { 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"))); +}