Fix string setter thunk to use PtrAndLen instead of two args.

PiperOrigin-RevId: 565118836
pull/14069/head
Protobuf Team Bot 1 year ago committed by Copybara-Service
parent c92e490ed7
commit 78d71cac9f
  1. 6
      rust/internal.rs
  2. 10
      rust/vtable.rs
  3. 8
      src/google/protobuf/compiler/rust/accessors/singular_string.cc

@ -97,3 +97,9 @@ impl PtrAndLen {
} }
} }
} }
impl From<&[u8]> for PtrAndLen {
fn from(slice: &[u8]) -> Self {
Self { ptr: slice.as_ptr(), len: slice.len() }
}
}

@ -299,7 +299,7 @@ impl_raw_vtable_mutator_get_set!(bool, f32, f64, i32, i64, u32, u64);
#[doc(hidden)] #[doc(hidden)]
#[derive(Debug)] #[derive(Debug)]
pub struct BytesMutVTable { pub struct BytesMutVTable {
pub(crate) setter: unsafe extern "C" fn(msg: RawMessage, val: *const u8, len: usize), pub(crate) setter: unsafe extern "C" fn(msg: RawMessage, val: PtrAndLen),
pub(crate) getter: unsafe extern "C" fn(msg: RawMessage) -> PtrAndLen, pub(crate) getter: unsafe extern "C" fn(msg: RawMessage) -> PtrAndLen,
} }
@ -316,7 +316,7 @@ impl BytesMutVTable {
pub const fn new( pub const fn new(
_private: Private, _private: Private,
getter: unsafe extern "C" fn(msg: RawMessage) -> PtrAndLen, getter: unsafe extern "C" fn(msg: RawMessage) -> PtrAndLen,
setter: unsafe extern "C" fn(msg: RawMessage, val: *const u8, len: usize), setter: unsafe extern "C" fn(msg: RawMessage, val: PtrAndLen),
) -> Self { ) -> Self {
Self { getter, setter } Self { getter, setter }
} }
@ -330,7 +330,7 @@ impl BytesOptionalMutVTable {
pub const unsafe fn new( pub const unsafe fn new(
_private: Private, _private: Private,
getter: unsafe extern "C" fn(msg: RawMessage) -> PtrAndLen, getter: unsafe extern "C" fn(msg: RawMessage) -> PtrAndLen,
setter: unsafe extern "C" fn(msg: RawMessage, val: *const u8, len: usize), setter: unsafe extern "C" fn(msg: RawMessage, val: PtrAndLen),
clearer: unsafe extern "C" fn(msg: RawMessage), clearer: unsafe extern "C" fn(msg: RawMessage),
default: &'static [u8], default: &'static [u8],
) -> Self { ) -> Self {
@ -355,7 +355,7 @@ impl<'msg> RawVTableMutator<'msg, [u8]> {
let val = copy_bytes_in_arena_if_needed_by_runtime(self.msg_ref, val); let val = copy_bytes_in_arena_if_needed_by_runtime(self.msg_ref, val);
// SAFETY: // SAFETY:
// - `msg_ref` is valid for `'msg` as promised by the caller of `new`. // - `msg_ref` is valid for `'msg` as promised by the caller of `new`.
unsafe { (self.vtable.setter)(self.msg_ref.msg(), val.as_ptr(), val.len()) } unsafe { (self.vtable.setter)(self.msg_ref.msg(), val.into()) }
} }
pub(crate) fn truncate(&self, len: usize) { pub(crate) fn truncate(&self, len: usize) {
@ -385,7 +385,7 @@ impl<'msg> RawVTableOptionalMutatorData<'msg, [u8]> {
let val = copy_bytes_in_arena_if_needed_by_runtime(self.msg_ref, val); let val = copy_bytes_in_arena_if_needed_by_runtime(self.msg_ref, val);
// SAFETY: // SAFETY:
// - `msg_ref` is valid for `'msg` as promised by the caller. // - `msg_ref` is valid for `'msg` as promised by the caller.
unsafe { (self.vtable.base.setter)(self.msg_ref.msg(), val.as_ptr(), val.len()) } unsafe { (self.vtable.base.setter)(self.msg_ref.msg(), val.into()) }
self self
} }

@ -171,7 +171,7 @@ void SingularString::InExternC(Context<FieldDescriptor> field) const {
R"rs( R"rs(
$hazzer$ $hazzer$
fn $getter_thunk$(raw_msg: $pbi$::RawMessage) -> $pbi$::PtrAndLen; fn $getter_thunk$(raw_msg: $pbi$::RawMessage) -> $pbi$::PtrAndLen;
fn $setter_thunk$(raw_msg: $pbi$::RawMessage, val: *const u8, len: usize); fn $setter_thunk$(raw_msg: $pbi$::RawMessage, val: $pbi$::PtrAndLen);
fn $clearer_thunk$(raw_msg: $pbi$::RawMessage); fn $clearer_thunk$(raw_msg: $pbi$::RawMessage);
)rs"); )rs");
} }
@ -199,10 +199,10 @@ void SingularString::InThunkCc(Context<FieldDescriptor> field) const {
$hazzer$; $hazzer$;
::google::protobuf::rust_internal::PtrAndLen $getter_thunk$($QualifiedMsg$* msg) { ::google::protobuf::rust_internal::PtrAndLen $getter_thunk$($QualifiedMsg$* msg) {
absl::string_view val = msg->$field$(); absl::string_view val = msg->$field$();
return google::protobuf::rust_internal::PtrAndLen(val.data(), val.size()); return ::google::protobuf::rust_internal::PtrAndLen(val.data(), val.size());
} }
void $setter_thunk$($QualifiedMsg$* msg, const char* ptr, ::std::size_t size) { void $setter_thunk$($QualifiedMsg$* msg, ::google::protobuf::rust_internal::PtrAndLen s) {
msg->set_$field$(absl::string_view(ptr, size)); msg->set_$field$(absl::string_view(s.ptr, s.len));
} }
)cc"); )cc");
} }

Loading…
Cancel
Save