Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zb: allow direct access to message properties without allocating #1158

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions zbus/src/message/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -285,7 +285,7 @@ impl<'m> From<Header<'m>> 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 }
Expand Down
7 changes: 4 additions & 3 deletions zbus/src/message/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -23,7 +24,7 @@ pub(crate) struct Fields<'f> {
pub reply_serial: Option<NonZeroU32>,
pub destination: Option<BusName<'f>>,
pub sender: Option<UniqueName<'f>>,
pub signature: Signature,
pub signature: Cow<'f, Signature>,
pub unix_fds: Option<u32>,
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)?)
Expand Down
23 changes: 22 additions & 1 deletion zbus/src/message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<InterfaceName<'_>> {
self.quick_fields().interface(self)
}
pub fn member(&self) -> Option<MemberName<'_>> {
self.quick_fields().member(self)
}
pub fn path(&self) -> Option<zvariant::ObjectPath<'_>> {
self.quick_fields().path(self)
}
pub fn sender(&self) -> Option<zbus_names::UniqueName<'_>> {
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<Builder<'b>>
Expand Down Expand Up @@ -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(),
};

Expand Down
Loading