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
pull/17018/head
Jakob Buchgraber 10 months ago committed by Copybara-Service
parent e207f27a35
commit ec61d65a23
  1. 5
      rust/cpp.rs
  2. 7
      rust/cpp_kernel/cpp_api.cc
  3. 2
      rust/cpp_kernel/cpp_api.h
  4. 12
      rust/test/cpp/debug_test.rs

@ -228,12 +228,17 @@ impl From<RustStringRawParts> 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}")
}

@ -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 {

@ -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

@ -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")));
}

Loading…
Cancel
Save