diff --git a/rust/internal.rs b/rust/internal.rs index 5f62b15afc..1e0f536b19 100644 --- a/rust/internal.rs +++ b/rust/internal.rs @@ -97,3 +97,9 @@ impl PtrAndLen { } } } + +impl From<&[u8]> for PtrAndLen { + fn from(slice: &[u8]) -> Self { + Self { ptr: slice.as_ptr(), len: slice.len() } + } +} diff --git a/rust/vtable.rs b/rust/vtable.rs index 3c54dc1a44..6d6e4e96ba 100644 --- a/rust/vtable.rs +++ b/rust/vtable.rs @@ -299,7 +299,7 @@ impl_raw_vtable_mutator_get_set!(bool, f32, f64, i32, i64, u32, u64); #[doc(hidden)] #[derive(Debug)] 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, } @@ -316,7 +316,7 @@ impl BytesMutVTable { pub const fn new( _private: Private, 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 { getter, setter } } @@ -330,7 +330,7 @@ impl BytesOptionalMutVTable { pub const unsafe fn new( _private: Private, 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), default: &'static [u8], ) -> Self { @@ -355,7 +355,7 @@ impl<'msg> RawVTableMutator<'msg, [u8]> { let val = copy_bytes_in_arena_if_needed_by_runtime(self.msg_ref, val); // SAFETY: // - `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) { @@ -385,7 +385,7 @@ impl<'msg> RawVTableOptionalMutatorData<'msg, [u8]> { let val = copy_bytes_in_arena_if_needed_by_runtime(self.msg_ref, val); // SAFETY: // - `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 } diff --git a/src/google/protobuf/compiler/rust/accessors/singular_string.cc b/src/google/protobuf/compiler/rust/accessors/singular_string.cc index 0db32fa138..2ba36f6a7d 100644 --- a/src/google/protobuf/compiler/rust/accessors/singular_string.cc +++ b/src/google/protobuf/compiler/rust/accessors/singular_string.cc @@ -171,7 +171,7 @@ void SingularString::InExternC(Context field) const { R"rs( $hazzer$ 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); )rs"); } @@ -199,10 +199,10 @@ void SingularString::InThunkCc(Context field) const { $hazzer$; ::google::protobuf::rust_internal::PtrAndLen $getter_thunk$($QualifiedMsg$* msg) { 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) { - msg->set_$field$(absl::string_view(ptr, size)); + void $setter_thunk$($QualifiedMsg$* msg, ::google::protobuf::rust_internal::PtrAndLen s) { + msg->set_$field$(absl::string_view(s.ptr, s.len)); } )cc"); }