diff --git a/zbus/src/message/builder.rs b/zbus/src/message/builder.rs index 2c6bb97be..cfe348c6c 100644 --- a/zbus/src/message/builder.rs +++ b/zbus/src/message/builder.rs @@ -231,7 +231,7 @@ impl<'a> Builder<'a> { let ctxt = dbus_context!(self, 0); let mut header = self.header; - header.fields_mut().signature = signature; + header.fields_mut().signature = std::borrow::Cow::Owned(signature); let body_len_u32 = body_size.size().try_into().map_err(|_| Error::ExcessData)?; header.primary_mut().set_body_len(body_len_u32); @@ -285,7 +285,7 @@ impl<'m> From> for Builder<'m> { fn from(mut header: Header<'m>) -> Self { // Signature and Fds are added by body* methods. let fields = header.fields_mut(); - fields.signature = Signature::Unit; + fields.signature = std::borrow::Cow::Owned(Signature::Unit); fields.unix_fds = None; Self { header } diff --git a/zbus/src/message/fields.rs b/zbus/src/message/fields.rs index ec98571c5..d8addb077 100644 --- a/zbus/src/message/fields.rs +++ b/zbus/src/message/fields.rs @@ -5,6 +5,7 @@ use serde::{ }; use static_assertions::assert_impl_all; use std::num::NonZeroU32; +use std::borrow::Cow; use zbus_names::{BusName, ErrorName, InterfaceName, MemberName, UniqueName}; use zvariant::{ObjectPath, Signature, Type, Value}; @@ -23,7 +24,7 @@ pub(crate) struct Fields<'f> { pub reply_serial: Option, pub destination: Option>, pub sender: Option>, - pub signature: Signature, + pub signature: Cow<'f, Signature>, pub unix_fds: Option, } @@ -63,7 +64,7 @@ impl<'f> Serialize for Fields<'f> { if let Some(sender) = &self.sender { seq.serialize_element(&(FieldCode::Sender, Value::from(sender.as_str())))?; } - if !matches!(&self.signature, Signature::Unit) { + if self.signature != Cow::Borrowed(&Signature::Unit) { seq.serialize_element(&(FieldCode::Signature, SignatureSerializer(&self.signature)))?; } if let Some(unix_fds) = self.unix_fds { @@ -149,7 +150,7 @@ impl<'de> Visitor<'de> for FieldsVisitor { fields.sender = Some(UniqueName::try_from(value).map_err(V::Error::custom)?) } FieldCode::Signature => { - fields.signature = Signature::try_from(value).map_err(V::Error::custom)? + fields.signature = Cow::Owned(Signature::try_from(value).map_err(V::Error::custom)?) } FieldCode::UnixFDs => { fields.unix_fds = Some(u32::try_from(value).map_err(V::Error::custom)?) diff --git a/zbus/src/message/mod.rs b/zbus/src/message/mod.rs index 14df3a5c2..f02320363 100644 --- a/zbus/src/message/mod.rs +++ b/zbus/src/message/mod.rs @@ -68,6 +68,27 @@ pub(super) struct Inner { assert_impl_all!(Message: Send, Sync, Unpin); +impl Message { + pub fn into_body(self) -> Body { + Body::new(self.inner.bytes.slice(self.inner.body_offset..), self) + } + pub fn signature(&self) -> &zvariant::Signature { + self.quick_fields().signature() + } + pub fn interface(&self) -> Option> { + self.quick_fields().interface(self) + } + pub fn member(&self) -> Option> { + self.quick_fields().member(self) + } + pub fn path(&self) -> Option> { + self.quick_fields().path(self) + } + pub fn sender(&self) -> Option> { + self.quick_fields().sender(self) + } +} + impl Message { /// Create a builder for a message of type [`Type::MethodCall`]. pub fn method_call<'b, 'p: 'b, 'm: 'b, P, M>(path: P, method_name: M) -> Result> @@ -176,7 +197,7 @@ impl Message { reply_serial: quick_fields.reply_serial(), destination: quick_fields.destination(self), sender: quick_fields.sender(self), - signature: quick_fields.signature().clone(), + signature: std::borrow::Cow::Borrowed(quick_fields.signature()), unix_fds: quick_fields.unix_fds(), };