PiperOrigin-RevId: 607329878
pull/15842/head
Protobuf Team Bot 11 months ago committed by Copybara-Service
parent 0ce48b30b5
commit 8d9fb756be
  1. 6
      rust/cpp.rs
  2. 64
      rust/test/cpp/interop/main.rs
  3. 6
      rust/test/cpp/interop/test_utils.cc
  4. 26
      src/google/protobuf/compiler/rust/message.cc

@ -213,12 +213,16 @@ impl<'msg> MutatorMessageRef<'msg> {
_parent_msg: MutatorMessageRef<'msg>,
message_field_ptr: RawMessage,
) -> Self {
MutatorMessageRef { msg: message_field_ptr, _phantom: PhantomData }
Self { msg: message_field_ptr, _phantom: PhantomData }
}
pub fn msg(&self) -> RawMessage {
self.msg
}
pub fn from_raw_msg(_private: Private, msg: &RawMessage) -> Self {
Self { msg: *msg, _phantom: PhantomData }
}
}
pub fn copy_bytes_in_arena_if_needed_by_runtime<'msg>(

@ -6,10 +6,8 @@
// https://developers.google.com/open-source/licenses/bsd
use googletest::prelude::*;
use protobuf_cpp::__internal::PtrAndLen;
use protobuf_cpp::__internal::RawMessage;
use unittest_proto::TestAllExtensions;
use unittest_proto::TestAllTypes;
use protobuf_cpp::__internal::{PtrAndLen, RawMessage};
use unittest_proto::{TestAllExtensions, TestAllTypes, TestAllTypesMut, TestAllTypesView};
macro_rules! proto_assert_eq {
($lhs:expr, $rhs:expr) => {{
@ -28,6 +26,7 @@ extern "C" {
fn DeserializeTestAllTypes(data: *const u8, len: usize) -> RawMessage;
fn MutateTestAllTypes(msg: RawMessage);
fn SerializeTestAllTypes(msg: RawMessage) -> protobuf_cpp::__runtime::SerializedData;
fn DeleteTestAllTypes(msg: RawMessage);
fn NewWithExtension() -> RawMessage;
fn GetBytesExtension(msg: RawMessage) -> PtrAndLen;
@ -48,13 +47,30 @@ fn mutate_message_in_cpp() {
proto_assert_eq!(msg1, msg2);
}
#[test]
fn mutate_message_mut_in_cpp() {
let mut msg1 = TestAllTypes::new();
let mut msg_mut = msg1.as_mut();
unsafe {
MutateTestAllTypes(msg_mut.__unstable_cpp_repr_grant_permission_to_break());
}
let mut msg2 = TestAllTypes::new();
msg2.optional_int64_mut().set(42);
msg2.optional_bytes_mut().set(b"something mysterious");
msg2.optional_bool_mut().set(false);
proto_assert_eq!(msg1, msg2);
}
#[test]
fn deserialize_in_rust() {
let mut msg1 = TestAllTypes::new();
msg1.optional_int64_mut().set(-1);
msg1.optional_bytes_mut().set(b"some cool data I guess");
let serialized =
unsafe { SerializeTestAllTypes(msg1.__unstable_cpp_repr_grant_permission_to_break()) };
let serialized = unsafe {
SerializeTestAllTypes(msg1.as_view().__unstable_cpp_repr_grant_permission_to_break())
};
let mut msg2 = TestAllTypes::new();
msg2.deserialize(&serialized).unwrap();
@ -78,6 +94,42 @@ fn deserialize_in_cpp() {
proto_assert_eq!(msg1, msg2);
}
#[test]
fn deserialize_in_cpp_into_mut() {
let mut msg1 = TestAllTypes::new();
msg1.optional_int64_mut().set(-1);
msg1.optional_bytes_mut().set(b"some cool data I guess");
let data = msg1.serialize();
let mut raw_msg = unsafe { DeserializeTestAllTypes((*data).as_ptr(), data.len()) };
let msg2 = TestAllTypesMut::__unstable_wrap_cpp_grant_permission_to_break(&mut raw_msg);
proto_assert_eq!(msg1, msg2);
// The C++ still owns the message here and needs to delete it.
unsafe {
DeleteTestAllTypes(raw_msg);
}
}
#[test]
fn deserialize_in_cpp_into_view() {
let mut msg1 = TestAllTypes::new();
msg1.optional_int64_mut().set(-1);
msg1.optional_bytes_mut().set(b"some cool data I guess");
let data = msg1.serialize();
let raw_msg = unsafe { DeserializeTestAllTypes((*data).as_ptr(), data.len()) };
let msg2 = TestAllTypesView::__unstable_wrap_cpp_grant_permission_to_break(&raw_msg);
proto_assert_eq!(msg1, msg2);
// The C++ still owns the message here and needs to delete it.
unsafe {
DeleteTestAllTypes(raw_msg);
}
}
// This test ensures that random fields we (Rust) don't know about don't
// accidentally get destroyed by Rust.
#[test]

@ -22,9 +22,13 @@ extern "C" google::protobuf::rust_internal::SerializedData SerializeTestAllTypes
return google::protobuf::rust_internal::SerializeMsg(msg);
}
extern "C" void DeleteTestAllTypes(protobuf_unittest::TestAllTypes* msg) {
delete msg;
}
extern "C" void* DeserializeTestAllTypes(const void* data, size_t size) {
auto* proto = new protobuf_unittest::TestAllTypes;
proto->ParseFromArray(data, size);
proto->ParseFromArray(data, static_cast<int>(size));
return proto;
}

@ -1126,6 +1126,32 @@ void GenerateRs(Context& ctx, const Descriptor& msg) {
self.raw_msg()
}
}
impl<'a> $Msg$Mut<'a> {
//~ msg is a &mut so that the borrow checker enforces exclusivity to
//~ prevent constructing multiple Muts/Views from the same RawMessage.
pub fn __unstable_wrap_cpp_grant_permission_to_break(
msg: &'a mut $pbi$::RawMessage) -> Self {
Self {
inner: $pbr$::MutatorMessageRef::from_raw_msg($pbi$::Private, msg)
}
}
pub fn __unstable_cpp_repr_grant_permission_to_break(&mut self) -> $pbi$::RawMessage {
self.raw_msg()
}
}
impl<'a> $Msg$View<'a> {
//~ msg is a & so that the caller can claim the message is live for the
//~ corresponding lifetime.
pub fn __unstable_wrap_cpp_grant_permission_to_break(
msg: &'a $pbi$::RawMessage) -> Self {
Self::new($pbi$::Private, *msg)
}
pub fn __unstable_cpp_repr_grant_permission_to_break(&self) -> $pbi$::RawMessage {
self.msg
}
}
)rs");
}
}

Loading…
Cancel
Save