diff --git a/rust/cpp.rs b/rust/cpp.rs index aafb799dba..8c1e04c8a9 100644 --- a/rust/cpp.rs +++ b/rust/cpp.rs @@ -9,7 +9,7 @@ use crate::ProtoStr; use crate::__internal::{Private, PtrAndLen, RawArena, RawMap, RawMessage, RawRepeatedField}; -use crate::{Mut, Proxied, ProxiedInRepeated, Repeated, View}; +use crate::{Mut, Proxied, ProxiedInRepeated, Repeated, SettableValue, View}; use core::fmt::Debug; use paste::paste; use std::alloc::Layout; @@ -139,6 +139,15 @@ impl fmt::Debug for SerializedData { } } +impl SettableValue<[u8]> for SerializedData { + fn set_on<'msg>(self, _private: Private, mut mutator: Mut<'msg, [u8]>) + where + [u8]: 'msg, + { + mutator.set(self.as_ref()) + } +} + pub type BytesPresentMutData<'msg> = crate::vtable::RawVTableOptionalMutatorData<'msg, [u8]>; pub type BytesAbsentMutData<'msg> = crate::vtable::RawVTableOptionalMutatorData<'msg, [u8]>; pub type InnerBytesMut<'msg> = crate::vtable::RawVTableMutator<'msg, [u8]>; diff --git a/rust/test/shared/serialization_test.rs b/rust/test/shared/serialization_test.rs index 0515223fb7..2ac49b923d 100644 --- a/rust/test/shared/serialization_test.rs +++ b/rust/test/shared/serialization_test.rs @@ -37,3 +37,13 @@ fn deserialize_error() { let data = b"not a serialized proto"; assert!(msg.deserialize(&*data).is_err()); } + +#[test] +fn set_bytes_with_serialized_data() { + let mut msg = TestAllTypes::new(); + msg.optional_int64_mut().set(42); + msg.optional_bool_mut().set(true); + let mut msg2 = TestAllTypes::new(); + msg2.optional_bytes_mut().set(msg.serialize()); + assert_that!(msg2.optional_bytes(), eq(msg.serialize().as_ref())); +} diff --git a/rust/upb.rs b/rust/upb.rs index 8a3e0b905f..7895bee95c 100644 --- a/rust/upb.rs +++ b/rust/upb.rs @@ -7,10 +7,10 @@ //! UPB FFI wrapper code for use by Rust Protobuf. -use crate::ProtoStr; use crate::__internal::{Private, PtrAndLen, RawArena, RawMap, RawMessage, RawRepeatedField}; use crate::{ - Mut, Proxied, ProxiedInRepeated, Repeated, RepeatedMut, RepeatedView, View, ViewProxy, + Mut, ProtoStr, Proxied, ProxiedInRepeated, Repeated, RepeatedMut, RepeatedView, SettableValue, + View, ViewProxy, }; use core::fmt::Debug; use paste::paste; @@ -217,6 +217,15 @@ impl fmt::Debug for SerializedData { } } +impl SettableValue<[u8]> for SerializedData { + fn set_on<'msg>(self, _private: Private, mut mutator: Mut<'msg, [u8]>) + where + [u8]: 'msg, + { + mutator.set(self.as_ref()) + } +} + // TODO: Investigate replacing this with direct access to UPB bits. pub type BytesPresentMutData<'msg> = crate::vtable::RawVTableOptionalMutatorData<'msg, [u8]>; pub type BytesAbsentMutData<'msg> = crate::vtable::RawVTableOptionalMutatorData<'msg, [u8]>;