Change the Rust > C++ interop fns to be consuming-self, add a 'leak' to the name and a ManuallyDrop(self) on the owned message case.

PiperOrigin-RevId: 622159280
pull/16412/head
Protobuf Team Bot 10 months ago committed by Copybara-Service
parent f6bcf9c78f
commit f421dc42b5
  1. 28
      rust/test/cpp/interop/main.rs
  2. 8
      rust/test/cpp/interop/test_utils.cc
  3. 9
      src/google/protobuf/compiler/rust/message.cc

@ -23,6 +23,7 @@ macro_rules! proto_assert_eq {
// Helper functions invoking C++ Protobuf APIs directly in C++.
// Defined in `test_utils.cc`.
extern "C" {
fn TakeOwnershipAndGetOptionalInt32(msg: RawMessage) -> i32;
fn DeserializeTestAllTypes(data: *const u8, len: usize) -> RawMessage;
fn MutateTestAllTypes(msg: RawMessage);
fn SerializeTestAllTypes(msg: RawMessage) -> protobuf_cpp::__runtime::SerializedData;
@ -33,26 +34,20 @@ extern "C" {
}
#[test]
fn mutate_message_in_cpp() {
fn send_to_cpp() {
let mut msg1 = TestAllTypes::new();
unsafe {
MutateTestAllTypes(msg1.__unstable_cpp_repr_grant_permission_to_break());
}
let mut msg2 = TestAllTypes::new();
msg2.set_optional_int64(42);
msg2.set_optional_bytes(b"something mysterious");
msg2.set_optional_bool(false);
proto_assert_eq!(msg1, msg2);
msg1.set_optional_int32(7);
let i = unsafe {
TakeOwnershipAndGetOptionalInt32(msg1.__unstable_leak_cpp_repr_grant_permission_to_break())
};
assert_eq!(i, 7);
}
#[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());
MutateTestAllTypes(msg1.as_mut().__unstable_cpp_repr_grant_permission_to_break());
}
let mut msg2 = TestAllTypes::new();
@ -139,7 +134,8 @@ fn smuggle_extension() {
let data = msg1.serialize();
let mut msg2 = TestAllExtensions::parse(&data).unwrap();
let bytes =
unsafe { GetBytesExtension(msg2.__unstable_cpp_repr_grant_permission_to_break()).as_ref() };
assert_eq!(&*bytes, b"smuggled");
let bytes = unsafe {
GetBytesExtension(msg2.as_mut().__unstable_cpp_repr_grant_permission_to_break()).as_ref()
};
assert_eq!(bytes, b"smuggled");
}

@ -6,6 +6,7 @@
// https://developers.google.com/open-source/licenses/bsd
#include <cstddef>
#include <cstdint>
#include "absl/strings/string_view.h"
#include "rust/cpp_kernel/cpp_api.h"
@ -44,3 +45,10 @@ extern "C" google::protobuf::rust_internal::PtrAndLen GetBytesExtension(
proto->GetExtension(protobuf_unittest::optional_bytes_extension);
return {bytes.data(), bytes.size()};
}
extern "C" int32_t TakeOwnershipAndGetOptionalInt32(
protobuf_unittest::TestAllTypes* msg) {
int32_t i = msg->optional_int32();
delete msg;
return i;
}

@ -1102,8 +1102,9 @@ void GenerateRs(Context& ctx, const Descriptor& msg) {
pub fn __unstable_wrap_cpp_grant_permission_to_break(msg: $pbi$::RawMessage) -> Self {
Self { inner: $pbr$::MessageInner { msg } }
}
pub fn __unstable_cpp_repr_grant_permission_to_break(&mut self) -> $pbi$::RawMessage {
self.raw_msg()
pub fn __unstable_leak_cpp_repr_grant_permission_to_break(self) -> $pbi$::RawMessage {
let s = std::mem::ManuallyDrop::new(self);
s.raw_msg()
}
}
@ -1116,7 +1117,7 @@ void GenerateRs(Context& ctx, const Descriptor& msg) {
inner: $pbr$::MutatorMessageRef::from_raw_msg($pbi$::Private, msg)
}
}
pub fn __unstable_cpp_repr_grant_permission_to_break(&mut self) -> $pbi$::RawMessage {
pub fn __unstable_cpp_repr_grant_permission_to_break(self) -> $pbi$::RawMessage {
self.raw_msg()
}
}
@ -1128,7 +1129,7 @@ void GenerateRs(Context& ctx, const Descriptor& msg) {
msg: &'a $pbi$::RawMessage) -> Self {
Self::new($pbi$::Private, *msg)
}
pub fn __unstable_cpp_repr_grant_permission_to_break(&self) -> $pbi$::RawMessage {
pub fn __unstable_cpp_repr_grant_permission_to_break(self) -> $pbi$::RawMessage {
self.msg
}
}

Loading…
Cancel
Save