From 314e9482a24ca4048fad4ca5618f849fb54b44dd Mon Sep 17 00:00:00 2001 From: Zeeshan Ali Khan Date: Mon, 31 Jul 2023 19:20:57 +0300 Subject: [PATCH 01/21] =?UTF-8?q?=F0=9F=94=A5=20zb:=20Drop=20some=20unneed?= =?UTF-8?q?ed=20lifetime=20specifiers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- zbus/src/message/header.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/zbus/src/message/header.rs b/zbus/src/message/header.rs index 5a4617649..1f40e47ab 100644 --- a/zbus/src/message/header.rs +++ b/zbus/src/message/header.rs @@ -269,12 +269,12 @@ impl<'m> Header<'m> { } /// Get a reference to the message fields. - pub fn fields<'s>(&'s self) -> &'s Fields<'m> { + pub fn fields(&self) -> &Fields<'m> { &self.fields } /// Get a mutable reference to the message fields. - pub fn fields_mut<'s>(&'s mut self) -> &'s mut Fields<'m> { + pub fn fields_mut(&mut self) -> &mut Fields<'m> { &mut self.fields } @@ -289,22 +289,22 @@ impl<'m> Header<'m> { } /// The object to send a call to, or the object a signal is emitted from. - pub fn path<'s>(&'s self) -> Result>, Error> { + pub fn path(&self) -> Result>, Error> { get_field!(self, Path) } /// The interface to invoke a method call on, or that a signal is emitted from. - pub fn interface<'s>(&'s self) -> Result>, Error> { + pub fn interface(&self) -> Result>, Error> { get_field!(self, Interface) } /// The member, either the method name or signal name. - pub fn member<'s>(&'s self) -> Result>, Error> { + pub fn member(&self) -> Result>, Error> { get_field!(self, Member) } /// The name of the error that occurred, for errors. - pub fn error_name<'s>(&'s self) -> Result>, Error> { + pub fn error_name(&self) -> Result>, Error> { get_field!(self, ErrorName) } @@ -318,12 +318,12 @@ impl<'m> Header<'m> { } /// The name of the connection this message is intended for. - pub fn destination<'s>(&'s self) -> Result>, Error> { + pub fn destination(&self) -> Result>, Error> { get_field!(self, Destination) } /// Unique name of the sending connection. - pub fn sender<'s>(&'s self) -> Result>, Error> { + pub fn sender(&self) -> Result>, Error> { get_field!(self, Sender) } From 2ef0d557e9fe5e01231829f678c1fb70170d49d1 Mon Sep 17 00:00:00 2001 From: Zeeshan Ali Khan Date: Mon, 31 Jul 2023 13:40:04 +0300 Subject: [PATCH 02/21] =?UTF-8?q?=F0=9F=94=A5=20zb:=20Drop=20message::Fiel?= =?UTF-8?q?d::Invalid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This variant is never constructed by us. When we encounter invalid field code, we throw an error. --- zbus/src/message/field.rs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/zbus/src/message/field.rs b/zbus/src/message/field.rs index e53089f83..c89744a1e 100644 --- a/zbus/src/message/field.rs +++ b/zbus/src/message/field.rs @@ -76,7 +76,6 @@ impl<'f> Field<'f> { Field::Sender(_) => FieldCode::Sender, Field::Signature(_) => FieldCode::Signature, Field::UnixFDs(_) => FieldCode::UnixFDs, - Field::Invalid => FieldCode::Invalid, } } } @@ -94,8 +93,6 @@ impl<'f> Field<'f> { /// [Message Format]: https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-messages #[derive(Clone, Debug, PartialEq, Eq)] pub enum Field<'f> { - /// Not a valid field. - Invalid, /// The object to send a call to, or the object a signal is emitted from. Path(ObjectPath<'f>), /// The interface to invoke a method call on, or that a signal is emitted from. @@ -139,8 +136,6 @@ impl<'f> Serialize for Field<'f> { Field::Sender(value) => (FieldCode::Sender, value.as_str().into()), Field::Signature(value) => (FieldCode::Signature, value.as_ref().into()), Field::UnixFDs(value) => (FieldCode::UnixFDs, (*value).into()), - // This is a programmer error - Field::Invalid => panic!("Attempt to serialize invalid Field"), }; tuple.serialize(serializer) From 546f061d1d4ed3fb1f05f85345785845f436dce0 Mon Sep 17 00:00:00 2001 From: Zeeshan Ali Khan Date: Fri, 11 Aug 2023 19:08:50 +0200 Subject: [PATCH 03/21] =?UTF-8?q?=F0=9F=94=A5=20zb:=20Drop=20unneeded=20Fr?= =?UTF-8?q?om=20for=20message::FieldCode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Seriously doubt anyone uses this and this will also allow us to drop `FieldCode::Invalid` in the following commit. --- zbus/src/message/field.rs | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/zbus/src/message/field.rs b/zbus/src/message/field.rs index c89744a1e..341273d22 100644 --- a/zbus/src/message/field.rs +++ b/zbus/src/message/field.rs @@ -46,23 +46,6 @@ pub enum FieldCode { assert_impl_all!(FieldCode: Send, Sync, Unpin); -impl From for FieldCode { - fn from(val: u8) -> FieldCode { - match val { - 1 => FieldCode::Path, - 2 => FieldCode::Interface, - 3 => FieldCode::Member, - 4 => FieldCode::ErrorName, - 5 => FieldCode::ReplySerial, - 6 => FieldCode::Destination, - 7 => FieldCode::Sender, - 8 => FieldCode::Signature, - 9 => FieldCode::UnixFDs, - _ => FieldCode::Invalid, - } - } -} - impl<'f> Field<'f> { /// Get the associated code for this field. pub fn code(&self) -> FieldCode { From 53cda6c26d260b7bb955c72e75b5e92378bb2133 Mon Sep 17 00:00:00 2001 From: Zeeshan Ali Khan Date: Fri, 11 Aug 2023 19:11:59 +0200 Subject: [PATCH 04/21] =?UTF-8?q?=F0=9F=94=A5=20zb:=20Drop=20now=20unneede?= =?UTF-8?q?d=20message::FieldCode::Invalid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A message with an invalid field code will just fail to deserialize so we don't need to represent this state. --- zbus/src/message/field.rs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/zbus/src/message/field.rs b/zbus/src/message/field.rs index 341273d22..e61d7ea23 100644 --- a/zbus/src/message/field.rs +++ b/zbus/src/message/field.rs @@ -22,8 +22,6 @@ use zvariant::{ObjectPath, Signature, Type, Value}; #[repr(u8)] #[derive(Copy, Clone, Debug, Deserialize_repr, PartialEq, Eq, Serialize_repr, Type)] pub enum FieldCode { - /// Code for [`Field::Invalid`](enum.Field.html#variant.Invalid) - Invalid = 0, /// Code for [`Field::Path`](enum.Field.html#variant.Path) Path = 1, /// Code for [`Field::Interface`](enum.Field.html#variant.Interface) @@ -164,12 +162,6 @@ impl<'de: 'f, 'f> Deserialize<'de> for Field<'f> { Field::Signature(Signature::try_from(value).map_err(D::Error::custom)?) } FieldCode::UnixFDs => Field::UnixFDs(u32::try_from(value).map_err(D::Error::custom)?), - FieldCode::Invalid => { - return Err(Error::invalid_value( - serde::de::Unexpected::Unsigned(code as u64), - &"A valid D-Bus message field code", - )); - } }) } } From 4c91b4e44abef05db6c4dff87ff28421051281a5 Mon Sep 17 00:00:00 2001 From: Zeeshan Ali Khan Date: Fri, 11 Aug 2023 19:08:50 +0200 Subject: [PATCH 05/21] =?UTF-8?q?=F0=9F=94=A5=20zb:=20Drop=20unneeded=20Fr?= =?UTF-8?q?om=20for=20message::Type?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Seriously doubt anyone uses this and this will also allow us to drop `message::Type::Invalid` in the following commit. --- zbus/src/message/header.rs | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/zbus/src/message/header.rs b/zbus/src/message/header.rs index 1f40e47ab..90b550f9b 100644 --- a/zbus/src/message/header.rs +++ b/zbus/src/message/header.rs @@ -70,19 +70,6 @@ pub enum Type { assert_impl_all!(Type: Send, Sync, Unpin); -// Such a shame I've to do this manually -impl From for Type { - fn from(val: u8) -> Type { - match val { - 1 => Type::MethodCall, - 2 => Type::MethodReturn, - 3 => Type::Error, - 4 => Type::Signal, - _ => Type::Invalid, - } - } -} - /// Pre-defined flags that can be passed in Message header. #[bitflags] #[repr(u8)] From fb7c2a1973821b1703467bb2edb917b685c2ba07 Mon Sep 17 00:00:00 2001 From: Zeeshan Ali Khan Date: Fri, 11 Aug 2023 19:11:59 +0200 Subject: [PATCH 06/21] =?UTF-8?q?=F0=9F=94=A5=20zb:=20Drop=20now=20unneede?= =?UTF-8?q?d=20message::Type::Invalid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit An invalid message will just fail to deserialize so we don't need to represent this state. --- zbus/src/match_rule/mod.rs | 2 -- zbus/src/message/header.rs | 2 -- 2 files changed, 4 deletions(-) diff --git a/zbus/src/match_rule/mod.rs b/zbus/src/match_rule/mod.rs index 9a60d525d..6abd84277 100644 --- a/zbus/src/match_rule/mod.rs +++ b/zbus/src/match_rule/mod.rs @@ -1,6 +1,5 @@ //! Bus match rule API. -use core::panic; use std::ops::Deref; use serde::{de, Deserialize, Serialize}; @@ -330,7 +329,6 @@ impl ToString for MatchRule<'_> { if let Some(msg_type) = self.msg_type() { let type_str = match msg_type { Type::Error => "error", - Type::Invalid => panic!("invalid message type"), Type::MethodCall => "method_call", Type::MethodReturn => "method_return", Type::Signal => "signal", diff --git a/zbus/src/message/header.rs b/zbus/src/message/header.rs index 90b550f9b..f0de26ecd 100644 --- a/zbus/src/message/header.rs +++ b/zbus/src/message/header.rs @@ -56,8 +56,6 @@ pub const NATIVE_ENDIAN_SIG: EndianSig = EndianSig::Little; Debug, Copy, Clone, Deserialize_repr, PartialEq, Eq, Hash, Serialize_repr, VariantType, )] pub enum Type { - /// Invalid message type. All unknown types on received messages are treated as invalid. - Invalid = 0, /// Method call. This message type may prompt a reply (and typically does). MethodCall = 1, /// A reply to a method call. From 793e4df76280631169d93b5404e8dfd04366a970 Mon Sep 17 00:00:00 2001 From: Zeeshan Ali Khan Date: Fri, 11 Aug 2023 00:07:37 +0200 Subject: [PATCH 07/21] =?UTF-8?q?=F0=9F=8F=97=EF=B8=8F=20zb:=20Cache=20all?= =?UTF-8?q?=20message=20header=20fields?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We'll be using these to avoid having to deserialize header and individual fields repeatedly. --- zbus/src/message/fields.rs | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/zbus/src/message/fields.rs b/zbus/src/message/fields.rs index 87374e237..5b23d40a3 100644 --- a/zbus/src/message/fields.rs +++ b/zbus/src/message/fields.rs @@ -1,8 +1,8 @@ use serde::{Deserialize, Serialize}; use static_assertions::assert_impl_all; use std::num::NonZeroU32; -use zbus_names::{InterfaceName, MemberName}; -use zvariant::{ObjectPath, Type}; +use zbus_names::{BusName, ErrorName, InterfaceName, MemberName, UniqueName}; +use zvariant::{ObjectPath, Signature, Type}; use crate::{ message::{Field, FieldCode, Header, Message}, @@ -149,13 +149,18 @@ impl FieldPos { } } -/// A cache of some commonly-used fields of the header of a Message. +/// A cache of the Message header fields. #[derive(Debug, Default, Copy, Clone)] pub(crate) struct QuickFields { path: FieldPos, interface: FieldPos, member: FieldPos, + error_name: FieldPos, reply_serial: Option, + destination: FieldPos, + sender: FieldPos, + signature: FieldPos, + unix_fds: Option, } impl QuickFields { @@ -164,7 +169,12 @@ impl QuickFields { path: FieldPos::new(buf, header.path()?), interface: FieldPos::new(buf, header.interface()?), member: FieldPos::new(buf, header.member()?), + error_name: FieldPos::new(buf, header.error_name()?), reply_serial: header.reply_serial()?, + destination: FieldPos::new(buf, header.destination()?), + sender: FieldPos::new(buf, header.sender()?), + signature: FieldPos::new(buf, header.signature()?), + unix_fds: header.unix_fds()?, }) } @@ -180,9 +190,29 @@ impl QuickFields { self.member.read(msg.as_bytes()) } + pub fn error_name<'m>(&self, msg: &'m Message) -> Option> { + self.error_name.read(msg.as_bytes()) + } + pub fn reply_serial(&self) -> Option { self.reply_serial } + + pub fn destination<'m>(&self, msg: &'m Message) -> Option> { + self.destination.read(msg.as_bytes()) + } + + pub fn sender<'m>(&self, msg: &'m Message) -> Option> { + self.sender.read(msg.as_bytes()) + } + + pub fn signature<'m>(&self, msg: &'m Message) -> Option> { + self.signature.read(msg.as_bytes()) + } + + pub fn unix_fds(&self) -> Option { + self.unix_fds + } } impl<'m> Default for Fields<'m> { From 6d318a7a3f6dc59cba533db281fcbe405e913d68 Mon Sep 17 00:00:00 2001 From: Zeeshan Ali Khan Date: Fri, 11 Aug 2023 00:13:44 +0200 Subject: [PATCH 08/21] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20=20zb:=20Message::bo?= =?UTF-8?q?dy=5Fsignature=20now=20infallible=20and=20more=20efficient?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As it is now based on the internal fields cache and doesn't involve deserializing the header. --- zbus/src/message/mod.rs | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/zbus/src/message/mod.rs b/zbus/src/message/mod.rs index 41876f89f..e426ce14c 100644 --- a/zbus/src/message/mod.rs +++ b/zbus/src/message/mod.rs @@ -287,16 +287,8 @@ impl Message { /// syntax), D-Bus does not. Since this method gives you the signature expected on the wire by /// D-Bus, the trailing and leading STRUCT signature parenthesis will not be present in case of /// multiple arguments. - pub fn body_signature(&self) -> Result> { - match self - .header()? - .into_fields() - .into_field(FieldCode::Signature) - .ok_or(Error::NoBodySignature)? - { - Field::Signature(signature) => Ok(signature), - _ => Err(Error::InvalidField), - } + pub fn body_signature(&self) -> Option> { + self.quick_fields.signature(self) } pub fn primary_header(&self) -> &PrimaryHeader { @@ -417,11 +409,9 @@ impl Message { where B: zvariant::DynamicDeserialize<'d>, { - let body_sig = match self.body_signature() { - Ok(sig) => sig, - Err(Error::NoBodySignature) => Signature::from_static_str_unchecked(""), - Err(e) => return Err(e), - }; + let body_sig = self + .body_signature() + .unwrap_or_else(|| Signature::from_static_str_unchecked("")); { #[cfg(unix)] @@ -509,7 +499,7 @@ impl fmt::Debug for Message { msg.field("member", &member); } }); - if let Ok(s) = self.body_signature() { + if let Some(s) = self.body_signature() { msg.field("body", &s); } #[cfg(unix)] From 8c45ee0a5fd7afc57f56124382d831b30ee2370f Mon Sep 17 00:00:00 2001 From: Zeeshan Ali Khan Date: Fri, 11 Aug 2023 00:29:16 +0200 Subject: [PATCH 09/21] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20zb:=20Message::heade?= =?UTF-8?q?r=20doesn't=20deserialize=20anymore?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We now construct the Header from fields cache and primary header clone. --- zbus/src/message/mod.rs | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/zbus/src/message/mod.rs b/zbus/src/message/mod.rs index e426ce14c..dbd216efc 100644 --- a/zbus/src/message/mod.rs +++ b/zbus/src/message/mod.rs @@ -311,9 +311,37 @@ impl Message { /// /// Note: prefer using the direct access methods if possible; they are more efficient. pub fn header(&self) -> Result> { - zvariant::from_slice(&self.bytes, dbus_context!(0)) - .map_err(Error::from) - .map(|h| h.0) + let mut fields = Fields::new(); + let quick_fields = &self.quick_fields; + if let Some(p) = quick_fields.path(self) { + fields.add(Field::Path(p)); + } + if let Some(i) = quick_fields.interface(self) { + fields.add(Field::Interface(i)); + } + if let Some(m) = quick_fields.member(self) { + fields.add(Field::Member(m)); + } + if let Some(e) = quick_fields.error_name(self) { + fields.add(Field::ErrorName(e)); + } + if let Some(r) = quick_fields.reply_serial() { + fields.add(Field::ReplySerial(r)); + } + if let Some(d) = quick_fields.destination(self) { + fields.add(Field::Destination(d)); + } + if let Some(s) = quick_fields.sender(self) { + fields.add(Field::Sender(s)); + } + if let Some(s) = quick_fields.signature(self) { + fields.add(Field::Signature(s)); + } + if let Some(u) = quick_fields.unix_fds() { + fields.add(Field::UnixFDs(u)); + } + + Ok(Header::new(self.primary_header.clone(), fields)) } /// Deserialize the fields. From e42584c9fc250886d730cb83af3b29858cda4534 Mon Sep 17 00:00:00 2001 From: Zeeshan Ali Khan Date: Fri, 11 Aug 2023 00:40:02 +0200 Subject: [PATCH 10/21] =?UTF-8?q?=F0=9F=94=A5=20zb:=20Drop=20Error::NoBody?= =?UTF-8?q?Signature=20variant?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This variant is never constructed anymore and hence useless. We could just deprecate it only but the chances of someone using this in client code are very low so let's not bother. --- zbus/src/error.rs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/zbus/src/error.rs b/zbus/src/error.rs index f01537314..e500c736e 100644 --- a/zbus/src/error.rs +++ b/zbus/src/error.rs @@ -52,7 +52,6 @@ pub enum Error { #[cfg(feature = "xml")] /// An XML error from quick_xml QuickXml(DeError), - NoBodySignature, /// The requested name was already claimed by another peer. NameTaken, /// Invalid [match rule][MR] string. @@ -84,7 +83,6 @@ impl PartialEq for Error { (Self::InvalidSerial, Self::InvalidSerial) => true, (Self::Unsupported, Self::Unsupported) => true, (Self::FDO(s), Self::FDO(o)) => s == o, - (Self::NoBodySignature, Self::NoBodySignature) => true, (Self::InvalidField, Self::InvalidField) => true, (Self::InvalidMatchRule, Self::InvalidMatchRule) => true, (Self::Variant(s), Self::Variant(o)) => s == o, @@ -117,7 +115,6 @@ impl error::Error for Error { Error::FDO(e) => Some(e), #[cfg(feature = "xml")] Error::QuickXml(e) => Some(e), - Error::NoBodySignature => None, Error::InvalidField => None, Error::MissingField => None, Error::NameTaken => None, @@ -154,7 +151,6 @@ impl fmt::Display for Error { Error::FDO(e) => write!(f, "{e}"), #[cfg(feature = "xml")] Error::QuickXml(e) => write!(f, "XML error: {e}"), - Error::NoBodySignature => write!(f, "missing body signature in the message"), Error::NameTaken => write!(f, "name already taken on the bus"), Error::InvalidMatchRule => write!(f, "Invalid match rule string"), Error::Failure(e) => write!(f, "{e}"), @@ -188,7 +184,6 @@ impl Clone for Error { Error::FDO(e) => Error::FDO(e.clone()), #[cfg(feature = "xml")] Error::QuickXml(e) => Error::QuickXml(e.clone()), - Error::NoBodySignature => Error::NoBodySignature, Error::NameTaken => Error::NameTaken, Error::InvalidMatchRule => Error::InvalidMatchRule, Error::Failure(e) => Error::Failure(e.clone()), From 510f3d3eed41318adcf2cfbfb4f4f20e7436a0cd Mon Sep 17 00:00:00 2001 From: Zeeshan Ali Khan Date: Fri, 11 Aug 2023 00:29:16 +0200 Subject: [PATCH 11/21] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20zb:=20Message::field?= =?UTF-8?q?s=20doesn't=20deserialize=20anymore?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We now construct the fields from the cache. --- zbus/src/message/mod.rs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/zbus/src/message/mod.rs b/zbus/src/message/mod.rs index dbd216efc..d486d7785 100644 --- a/zbus/src/message/mod.rs +++ b/zbus/src/message/mod.rs @@ -311,6 +311,13 @@ impl Message { /// /// Note: prefer using the direct access methods if possible; they are more efficient. pub fn header(&self) -> Result> { + Ok(Header::new(self.primary_header.clone(), self.fields()?)) + } + + /// Deserialize the fields. + /// + /// Note: prefer using the direct access methods if possible; they are more efficient. + pub fn fields(&self) -> Result> { let mut fields = Fields::new(); let quick_fields = &self.quick_fields; if let Some(p) = quick_fields.path(self) { @@ -341,17 +348,7 @@ impl Message { fields.add(Field::UnixFDs(u)); } - Ok(Header::new(self.primary_header.clone(), fields)) - } - - /// Deserialize the fields. - /// - /// Note: prefer using the direct access methods if possible; they are more efficient. - pub fn fields(&self) -> Result> { - let ctxt = dbus_context!(header::PRIMARY_HEADER_SIZE); - zvariant::from_slice(&self.bytes[header::PRIMARY_HEADER_SIZE..], ctxt) - .map_err(Error::from) - .map(|f| f.0) + Ok(fields) } /// The message type. From 539cab4b05ef4a7e462725868620252402e7a872 Mon Sep 17 00:00:00 2001 From: Zeeshan Ali Khan Date: Fri, 11 Aug 2023 18:07:06 +0200 Subject: [PATCH 12/21] =?UTF-8?q?=F0=9F=93=9D=20zb:=20Update=20docs=20abou?= =?UTF-8?q?t=20Message::{header,=20fields}=20being=20slow?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit They are not anymore. --- zbus/src/message/mod.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/zbus/src/message/mod.rs b/zbus/src/message/mod.rs index d486d7785..b70d7d40f 100644 --- a/zbus/src/message/mod.rs +++ b/zbus/src/message/mod.rs @@ -307,16 +307,18 @@ impl Message { .map_err(Error::from) } - /// Deserialize the header. + /// The message header. /// - /// Note: prefer using the direct access methods if possible; they are more efficient. + /// Note: This method does not deserialize the header but it does currently allocate so its not + /// zero-cost. While the allocation is small and will hopefully be removed in the future, it's + /// best to keep the header around if you need to access it a lot. pub fn header(&self) -> Result> { Ok(Header::new(self.primary_header.clone(), self.fields()?)) } - /// Deserialize the fields. + /// The message header fields. /// - /// Note: prefer using the direct access methods if possible; they are more efficient. + /// The note on [`Message::header`] applies here too. pub fn fields(&self) -> Result> { let mut fields = Fields::new(); let quick_fields = &self.quick_fields; From 7fb2d50e931226f4b87831403f296ca210ecd082 Mon Sep 17 00:00:00 2001 From: Zeeshan Ali Khan Date: Fri, 11 Aug 2023 18:17:02 +0200 Subject: [PATCH 13/21] =?UTF-8?q?=F0=9F=94=A5=20zb:=20Message::fields=20no?= =?UTF-8?q?w=20infallible?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since we construct the fields from the cache. --- zbus/src/message/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/zbus/src/message/mod.rs b/zbus/src/message/mod.rs index b70d7d40f..95d87445e 100644 --- a/zbus/src/message/mod.rs +++ b/zbus/src/message/mod.rs @@ -313,13 +313,13 @@ impl Message { /// zero-cost. While the allocation is small and will hopefully be removed in the future, it's /// best to keep the header around if you need to access it a lot. pub fn header(&self) -> Result> { - Ok(Header::new(self.primary_header.clone(), self.fields()?)) + Ok(Header::new(self.primary_header.clone(), self.fields())) } /// The message header fields. /// /// The note on [`Message::header`] applies here too. - pub fn fields(&self) -> Result> { + pub fn fields(&self) -> Fields<'_> { let mut fields = Fields::new(); let quick_fields = &self.quick_fields; if let Some(p) = quick_fields.path(self) { @@ -350,7 +350,7 @@ impl Message { fields.add(Field::UnixFDs(u)); } - Ok(fields) + fields } /// The message type. From 8aaca4230a40eae4655e042c097b2bc5367c524e Mon Sep 17 00:00:00 2001 From: Zeeshan Ali Khan Date: Fri, 11 Aug 2023 18:17:02 +0200 Subject: [PATCH 14/21] =?UTF-8?q?=F0=9F=94=A5=20zb:=20Message::header=20no?= =?UTF-8?q?w=20infallible?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since we construct the header from the cache now. --- book/src/server.md | 2 +- zbus/src/connection/mod.rs | 9 +---- zbus/src/error.rs | 7 +--- zbus/src/lib.rs | 4 +- zbus/src/match_rule/mod.rs | 2 +- zbus/src/message/mod.rs | 63 +++++++++++++---------------- zbus/src/object_server/interface.rs | 2 +- zbus/src/object_server/mod.rs | 2 +- zbus/src/proxy/mod.rs | 2 +- zbus/tests/e2e.rs | 2 +- zbus_macros/src/iface.rs | 8 ++-- 11 files changed, 43 insertions(+), 60 deletions(-) diff --git a/book/src/server.md b/book/src/server.md index 7eb82748b..abbe0e221 100644 --- a/book/src/server.md +++ b/book/src/server.md @@ -66,7 +66,7 @@ let mut stream = zbus::MessageStream::from(&connection); # .await?; # while let Some(msg) = stream.try_next().await? { - let msg_header = msg.header()?; + let msg_header = msg.header(); dbg!(&msg); match msg_header.message_type()? { diff --git a/zbus/src/connection/mod.rs b/zbus/src/connection/mod.rs index 2c9a030b1..f087ee9dd 100644 --- a/zbus/src/connection/mod.rs +++ b/zbus/src/connection/mod.rs @@ -996,14 +996,7 @@ impl Connection { m.ok() }) { if let Some(conn) = weak_conn.upgrade() { - let hdr = match msg.header() { - Ok(hdr) => hdr, - Err(e) => { - warn!("Failed to parse header: {}", e); - - continue; - } - }; + let hdr = msg.header(); match hdr.destination() { // Unique name is already checked by the match rule. Ok(Some(BusName::Unique(_))) | Ok(None) => (), diff --git a/zbus/src/error.rs b/zbus/src/error.rs index e500c736e..526da6530 100644 --- a/zbus/src/error.rs +++ b/zbus/src/error.rs @@ -254,12 +254,7 @@ impl From> for Error { fn from(message: Arc) -> Error { // FIXME: Instead of checking this, we should have Method as trait and specific types for // each message type. - let header = match message.header() { - Ok(header) => header, - Err(e) => { - return e; - } - }; + let header = message.header(); if header.primary().msg_type() != Type::Error { return Error::InvalidReply; } diff --git a/zbus/src/lib.rs b/zbus/src/lib.rs index a7af481e7..c7bd06a7e 100644 --- a/zbus/src/lib.rs +++ b/zbus/src/lib.rs @@ -720,7 +720,7 @@ mod tests { for m in stream { let msg = m.unwrap(); - let hdr = msg.header().unwrap(); + let hdr = msg.header(); if hdr.member().unwrap().map(|m| m.as_str()) == Some("ZBusIssue122") { break; @@ -950,7 +950,7 @@ mod tests { let mut stream = zbus::MessageStream::from(connection); while let Some(msg) = stream.try_next().await? { - let msg_header = msg.header()?; + let msg_header = msg.header(); match msg_header.message_type()? { zbus::message::Type::MethodCall => { diff --git a/zbus/src/match_rule/mod.rs b/zbus/src/match_rule/mod.rs index 6abd84277..20bfc589c 100644 --- a/zbus/src/match_rule/mod.rs +++ b/zbus/src/match_rule/mod.rs @@ -206,7 +206,7 @@ impl<'m> MatchRule<'m> { /// * `destination` in the rule when `destination` on the `msg` is a well-known name. The /// `destination` on match rule is always a unique name. pub fn matches(&self, msg: &zbus::message::Message) -> Result { - let hdr = msg.header()?; + let hdr = msg.header(); // Start with message type. if let Some(msg_type) = self.msg_type() { diff --git a/zbus/src/message/mod.rs b/zbus/src/message/mod.rs index 95d87445e..27cbe7b8b 100644 --- a/zbus/src/message/mod.rs +++ b/zbus/src/message/mod.rs @@ -175,7 +175,7 @@ impl Message { S::Error: Into, B: serde::ser::Serialize + DynamicType, { - let mut b = Builder::method_return(&call.header()?)?; + let mut b = Builder::method_return(&call.header())?; if let Some(sender) = sender { b = b.sender(sender)?; } @@ -198,7 +198,7 @@ impl Message { E::Error: Into, B: serde::ser::Serialize + DynamicType, { - let mut b = Builder::error(&call.header()?, name)?; + let mut b = Builder::error(&call.header(), name)?; if let Some(sender) = sender { b = b.sender(sender)?; } @@ -312,8 +312,8 @@ impl Message { /// Note: This method does not deserialize the header but it does currently allocate so its not /// zero-cost. While the allocation is small and will hopefully be removed in the future, it's /// best to keep the header around if you need to access it a lot. - pub fn header(&self) -> Result> { - Ok(Header::new(self.primary_header.clone(), self.fields())) + pub fn header(&self) -> Header<'_> { + Header::new(self.primary_header.clone(), self.fields()) } /// The message header fields. @@ -506,26 +506,25 @@ impl Message { impl fmt::Debug for Message { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let mut msg = f.debug_struct("Msg"); - let _ = self.header().map(|h| { - if let Ok(t) = h.message_type() { - msg.field("type", &t); - } - if let Ok(Some(sender)) = h.sender() { - msg.field("sender", &sender); - } - if let Ok(Some(serial)) = h.reply_serial() { - msg.field("reply-serial", &serial); - } - if let Ok(Some(path)) = h.path() { - msg.field("path", &path); - } - if let Ok(Some(iface)) = h.interface() { - msg.field("iface", &iface); - } - if let Ok(Some(member)) = h.member() { - msg.field("member", &member); - } - }); + let h = self.header(); + if let Ok(t) = h.message_type() { + msg.field("type", &t); + } + if let Ok(Some(sender)) = h.sender() { + msg.field("sender", &sender); + } + if let Ok(Some(serial)) = h.reply_serial() { + msg.field("reply-serial", &serial); + } + if let Ok(Some(path)) = h.path() { + msg.field("path", &path); + } + if let Ok(Some(iface)) = h.interface() { + msg.field("iface", &iface); + } + if let Ok(Some(member)) = h.member() { + msg.field("member", &member); + } if let Some(s) = self.body_signature() { msg.field("body", &s); } @@ -543,16 +542,12 @@ impl fmt::Debug for Message { impl fmt::Display for Message { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let header = self.header(); - let (ty, error_name, sender, member) = if let Ok(h) = header.as_ref() { - ( - h.message_type().ok(), - h.error_name().ok().flatten(), - h.sender().ok().flatten(), - h.member().ok().flatten(), - ) - } else { - (None, None, None, None) - }; + let (ty, error_name, sender, member) = ( + header.message_type().ok(), + header.error_name().ok().flatten(), + header.sender().ok().flatten(), + header.member().ok().flatten(), + ); match ty { Some(Type::MethodCall) => { diff --git a/zbus/src/object_server/interface.rs b/zbus/src/object_server/interface.rs index 2c345ad89..65dfe4229 100644 --- a/zbus/src/object_server/interface.rs +++ b/zbus/src/object_server/interface.rs @@ -39,7 +39,7 @@ impl<'a> DispatchResult<'a> { E: zbus::DBusError + Send, { DispatchResult::Async(Box::pin(async move { - let hdr = msg.header()?; + let hdr = msg.header(); let ret = f.await; if !hdr.primary().flags().contains(Flags::NoReplyExpected) { match ret { diff --git a/zbus/src/object_server/mod.rs b/zbus/src/object_server/mod.rs index bca59490e..075b6b61b 100644 --- a/zbus/src/object_server/mod.rs +++ b/zbus/src/object_server/mod.rs @@ -710,7 +710,7 @@ impl ObjectServer { async fn dispatch_method_call(&self, connection: &Connection, msg: &Message) -> Result<()> { match self.dispatch_method_call_try(connection, msg).await { Err(e) => { - let hdr = msg.header()?; + let hdr = msg.header(); debug!("Returning error: {}", e); connection.reply_dbus_error(&hdr, e).await?; Ok(()) diff --git a/zbus/src/proxy/mod.rs b/zbus/src/proxy/mod.rs index 4605317ca..489bd2b2a 100644 --- a/zbus/src/proxy/mod.rs +++ b/zbus/src/proxy/mod.rs @@ -1232,7 +1232,7 @@ impl<'a> SignalStream<'a> { } fn filter(&mut self, msg: &Arc) -> Result { - let header = msg.header()?; + let header = msg.header(); let sender = header.sender()?; if sender == self.src_unique_name.as_ref() { return Ok(true); diff --git a/zbus/tests/e2e.rs b/zbus/tests/e2e.rs index be21c01a7..e63166621 100644 --- a/zbus/tests/e2e.rs +++ b/zbus/tests/e2e.rs @@ -456,7 +456,7 @@ async fn my_iface_test(conn: Connection, event: Event) -> zbus::Result { while !method_returned && !signal_received { let msg = stream.try_next().await?.unwrap(); - let hdr = msg.header()?; + let hdr = msg.header(); if hdr.message_type()? == message::Type::MethodReturn && hdr.reply_serial()? == Some(serial) { assert!(!signal_received); diff --git a/zbus_macros/src/iface.rs b/zbus_macros/src/iface.rs index 3f340c31f..5db95a748 100644 --- a/zbus_macros/src/iface.rs +++ b/zbus_macros/src/iface.rs @@ -197,7 +197,7 @@ pub fn expand(args: AttributeArgs, mut input: ItemImpl) -> syn::Result c.reply(m, &#ret).await, ::std::result::Result::Err(e) => { - let hdr = m.header()?; + let hdr = m.header(); c.reply_dbus_error(&hdr, e).await } }) @@ -612,7 +612,7 @@ fn get_args_from_inputs( let header_arg = &input.pat; header_arg_decl = Some(quote! { - let #header_arg = m.header()?; + let #header_arg = m.header(); }); } else if attrs.signal_context { if signal_context_arg_decl.is_some() { @@ -630,7 +630,7 @@ fn get_args_from_inputs( #zbus::object_server::SignalContext::new(c, p).expect("Infallible conversion failed") } ::std::option::Option::None => { - let hdr = m.header()?; + let hdr = m.header(); let err = #zbus::fdo::Error::UnknownObject("Path Required".into()); return c.reply_dbus_error(&hdr, err).await; } @@ -655,7 +655,7 @@ fn get_args_from_inputs( match m.body() { ::std::result::Result::Ok(r) => r, ::std::result::Result::Err(e) => { - let hdr = m.header()?; + let hdr = m.header(); let err = <#zbus::fdo::Error as ::std::convert::From<_>>::from(e); return c.reply_dbus_error(&hdr, err).await; } From fb88092cca54bc08bf2d69a9c3318648516a022d Mon Sep 17 00:00:00 2001 From: Zeeshan Ali Khan Date: Fri, 11 Aug 2023 18:17:02 +0200 Subject: [PATCH 15/21] =?UTF-8?q?=F0=9F=94=A5=20zb:=20message::Header=20ge?= =?UTF-8?q?tters=20now=20infallible?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since we either construct the header from the cache now or deserialize from raw bytes and the deserialization will fail if the message has fields with incorrect codes (the only possible issue here). --- book/src/server.md | 2 +- zbus/src/connection/mod.rs | 9 +---- zbus/src/error.rs | 2 +- zbus/src/fdo.rs | 10 ++--- zbus/src/lib.rs | 4 +- zbus/src/match_rule/mod.rs | 4 +- zbus/src/message/builder.rs | 4 +- zbus/src/message/fields.rs | 18 ++++----- zbus/src/message/header.rs | 76 +++++++++++++++++++------------------ zbus/src/message/mod.rs | 33 +++++++--------- zbus/src/proxy/mod.rs | 2 +- zbus/tests/e2e.rs | 30 +++++---------- zbus_macros/src/lib.rs | 2 +- 13 files changed, 89 insertions(+), 107 deletions(-) diff --git a/book/src/server.md b/book/src/server.md index abbe0e221..24234bbeb 100644 --- a/book/src/server.md +++ b/book/src/server.md @@ -69,7 +69,7 @@ while let Some(msg) = stream.try_next().await? { let msg_header = msg.header(); dbg!(&msg); - match msg_header.message_type()? { + match msg_header.message_type() { zbus::message::Type::MethodCall => { // real code would check msg_header path(), interface() and member() // handle invalid calls, introspection, errors etc diff --git a/zbus/src/connection/mod.rs b/zbus/src/connection/mod.rs index f087ee9dd..d82688d97 100644 --- a/zbus/src/connection/mod.rs +++ b/zbus/src/connection/mod.rs @@ -999,8 +999,8 @@ impl Connection { let hdr = msg.header(); match hdr.destination() { // Unique name is already checked by the match rule. - Ok(Some(BusName::Unique(_))) | Ok(None) => (), - Ok(Some(BusName::WellKnown(dest))) => { + Some(BusName::Unique(_)) | None => (), + Some(BusName::WellKnown(dest)) => { let names = conn.inner.registered_names.lock().await; // destination doesn't matter if no name has been registered // (probably means name it's registered through external means). @@ -1010,11 +1010,6 @@ impl Connection { continue; } } - Err(e) => { - warn!("Failed to parse destination: {}", e); - - continue; - } } let member = match msg.member() { Some(member) => member, diff --git a/zbus/src/error.rs b/zbus/src/error.rs index 526da6530..819925265 100644 --- a/zbus/src/error.rs +++ b/zbus/src/error.rs @@ -259,7 +259,7 @@ impl From> for Error { return Error::InvalidReply; } - if let Ok(Some(name)) = header.error_name() { + if let Some(name) = header.error_name() { let name = name.to_owned().into(); match message.body_unchecked::<&str>() { Ok(detail) => Error::MethodError(name, Some(String::from(detail)), message), diff --git a/zbus/src/fdo.rs b/zbus/src/fdo.rs index 633f0df89..8c07e82d0 100644 --- a/zbus/src/fdo.rs +++ b/zbus/src/fdo.rs @@ -54,7 +54,7 @@ impl Introspectable { #[zbus(object_server)] server: &ObjectServer, #[zbus(header)] header: Header<'_>, ) -> Result { - let path = header.path()?.ok_or(crate::Error::MissingField)?; + let path = header.path().ok_or(crate::Error::MissingField)?; let root = server.root().read().await; let node = root .get_child(path) @@ -126,7 +126,7 @@ impl Properties { #[zbus(object_server)] server: &ObjectServer, #[zbus(header)] header: Header<'_>, ) -> Result { - let path = header.path()?.ok_or(crate::Error::MissingField)?; + let path = header.path().ok_or(crate::Error::MissingField)?; let root = server.root().read().await; let iface = root .get_child(path) @@ -152,7 +152,7 @@ impl Properties { #[zbus(header)] header: Header<'_>, #[zbus(signal_context)] ctxt: SignalContext<'_>, ) -> Result<()> { - let path = header.path()?.ok_or(crate::Error::MissingField)?; + let path = header.path().ok_or(crate::Error::MissingField)?; let root = server.root().read().await; let iface = root .get_child(path) @@ -190,7 +190,7 @@ impl Properties { #[zbus(object_server)] server: &ObjectServer, #[zbus(header)] header: Header<'_>, ) -> Result> { - let path = header.path()?.ok_or(crate::Error::MissingField)?; + let path = header.path().ok_or(crate::Error::MissingField)?; let root = server.root().read().await; let iface = root .get_child(path) @@ -292,7 +292,7 @@ impl ObjectManager { #[zbus(object_server)] server: &ObjectServer, #[zbus(header)] header: Header<'_>, ) -> Result { - let path = header.path()?.ok_or(crate::Error::MissingField)?; + let path = header.path().ok_or(crate::Error::MissingField)?; let root = server.root().read().await; let node = root .get_child(path) diff --git a/zbus/src/lib.rs b/zbus/src/lib.rs index c7bd06a7e..06482cce5 100644 --- a/zbus/src/lib.rs +++ b/zbus/src/lib.rs @@ -722,7 +722,7 @@ mod tests { let msg = m.unwrap(); let hdr = msg.header(); - if hdr.member().unwrap().map(|m| m.as_str()) == Some("ZBusIssue122") { + if hdr.member().map(|m| m.as_str()) == Some("ZBusIssue122") { break; } } @@ -952,7 +952,7 @@ mod tests { while let Some(msg) = stream.try_next().await? { let msg_header = msg.header(); - match msg_header.message_type()? { + match msg_header.message_type() { zbus::message::Type::MethodCall => { connection.reply(&msg, &()).await?; diff --git a/zbus/src/match_rule/mod.rs b/zbus/src/match_rule/mod.rs index 20bfc589c..4822efb5d 100644 --- a/zbus/src/match_rule/mod.rs +++ b/zbus/src/match_rule/mod.rs @@ -218,7 +218,7 @@ impl<'m> MatchRule<'m> { // Then check sender. if let Some(sender) = self.sender() { match sender { - BusName::Unique(name) if Some(name) != hdr.sender()? => { + BusName::Unique(name) if Some(name) != hdr.sender() => { return Ok(false); } BusName::Unique(_) => (), @@ -247,7 +247,7 @@ impl<'m> MatchRule<'m> { // The destination. if let Some(destination) = self.destination() { - match hdr.destination()? { + match hdr.destination() { Some(BusName::Unique(name)) if destination != name => { return Ok(false); } diff --git a/zbus/src/message/builder.rs b/zbus/src/message/builder.rs index f36d74db9..25958ffa2 100644 --- a/zbus/src/message/builder.rs +++ b/zbus/src/message/builder.rs @@ -93,7 +93,7 @@ impl<'a> Builder<'a> { /// /// The function will return an error if invalid flags are given for the message type. pub fn with_flags(mut self, flag: Flags) -> Result { - if self.header.message_type()? != Type::MethodCall + if self.header.message_type() != Type::MethodCall && BitFlags::from_flag(flag).contains(Flags::NoReplyExpected) { return Err(Error::InvalidField); @@ -178,7 +178,7 @@ impl<'a> Builder<'a> { let serial = reply_to.primary().serial_num().ok_or(Error::MissingField)?; self.header.fields_mut().replace(Field::ReplySerial(serial)); - if let Some(sender) = reply_to.sender()? { + if let Some(sender) = reply_to.sender() { self.destination(sender.to_owned()) } else { Ok(self) diff --git a/zbus/src/message/fields.rs b/zbus/src/message/fields.rs index 5b23d40a3..edeb7f923 100644 --- a/zbus/src/message/fields.rs +++ b/zbus/src/message/fields.rs @@ -166,15 +166,15 @@ pub(crate) struct QuickFields { impl QuickFields { pub fn new(buf: &[u8], header: &Header<'_>) -> Result { Ok(Self { - path: FieldPos::new(buf, header.path()?), - interface: FieldPos::new(buf, header.interface()?), - member: FieldPos::new(buf, header.member()?), - error_name: FieldPos::new(buf, header.error_name()?), - reply_serial: header.reply_serial()?, - destination: FieldPos::new(buf, header.destination()?), - sender: FieldPos::new(buf, header.sender()?), - signature: FieldPos::new(buf, header.signature()?), - unix_fds: header.unix_fds()?, + path: FieldPos::new(buf, header.path()), + interface: FieldPos::new(buf, header.interface()), + member: FieldPos::new(buf, header.member()), + error_name: FieldPos::new(buf, header.error_name()), + reply_serial: header.reply_serial(), + destination: FieldPos::new(buf, header.destination()), + sender: FieldPos::new(buf, header.sender()), + signature: FieldPos::new(buf, header.signature()), + unix_fds: header.unix_fds(), }) } diff --git a/zbus/src/message/header.rs b/zbus/src/message/header.rs index f0de26ecd..c361deb47 100644 --- a/zbus/src/message/header.rs +++ b/zbus/src/message/header.rs @@ -219,9 +219,10 @@ macro_rules! get_field { ($self:ident, $kind:ident, $closure:tt) => { #[allow(clippy::redundant_closure_call)] match $self.fields().get_field(FieldCode::$kind) { - Some(Field::$kind(value)) => Ok(Some($closure(value))), - Some(_) => Err(Error::InvalidField), - None => Ok(None), + Some(Field::$kind(value)) => Some($closure(value)), + // SAFETY: `Deserialize` impl for `Field` ensures that the code and field match. + Some(_) => unreachable!("FieldCode and Field mismatch"), + None => None, } }; } @@ -269,56 +270,57 @@ impl<'m> Header<'m> { } /// The message type - pub fn message_type(&self) -> Result { - Ok(self.primary().msg_type()) + pub fn message_type(&self) -> Type { + self.primary().msg_type() } /// The object to send a call to, or the object a signal is emitted from. - pub fn path(&self) -> Result>, Error> { + pub fn path(&self) -> Option<&ObjectPath<'m>> { get_field!(self, Path) } /// The interface to invoke a method call on, or that a signal is emitted from. - pub fn interface(&self) -> Result>, Error> { + pub fn interface(&self) -> Option<&InterfaceName<'m>> { get_field!(self, Interface) } /// The member, either the method name or signal name. - pub fn member(&self) -> Result>, Error> { + pub fn member(&self) -> Option<&MemberName<'m>> { get_field!(self, Member) } /// The name of the error that occurred, for errors. - pub fn error_name(&self) -> Result>, Error> { + pub fn error_name(&self) -> Option<&ErrorName<'m>> { get_field!(self, ErrorName) } /// The serial number of the message this message is a reply to. - pub fn reply_serial(&self) -> Result, Error> { + pub fn reply_serial(&self) -> Option { match self.fields().get_field(FieldCode::ReplySerial) { - Some(Field::ReplySerial(value)) => Ok(Some(*value)), - Some(_) => Err(Error::InvalidField), - None => Ok(None), + Some(Field::ReplySerial(value)) => Some(*value), + // SAFETY: `Deserialize` impl for `Field` ensures that the code and field match. + Some(_) => unreachable!("FieldCode and Field mismatch"), + None => None, } } /// The name of the connection this message is intended for. - pub fn destination(&self) -> Result>, Error> { + pub fn destination(&self) -> Option<&BusName<'m>> { get_field!(self, Destination) } /// Unique name of the sending connection. - pub fn sender(&self) -> Result>, Error> { + pub fn sender(&self) -> Option<&UniqueName<'m>> { get_field!(self, Sender) } /// The signature of the message body. - pub fn signature(&self) -> Result>, Error> { + pub fn signature(&self) -> Option<&Signature<'m>> { get_field!(self, Signature) } /// The number of Unix file descriptors that accompany the message. - pub fn unix_fds(&self) -> Result, Error> { + pub fn unix_fds(&self) -> Option { get_field_u32!(self, UnixFDs) } } @@ -344,16 +346,16 @@ mod tests { f.add(Field::Sender(":1.84".try_into()?)); let h = Header::new(PrimaryHeader::new(Type::Signal, 77), f); - assert_eq!(h.message_type()?, Type::Signal); - assert_eq!(h.path()?, Some(&path)); - assert_eq!(h.interface()?, Some(&iface)); - assert_eq!(h.member()?, Some(&member)); - assert_eq!(h.error_name()?, None); - assert_eq!(h.destination()?, None); - assert_eq!(h.reply_serial()?, None); - assert_eq!(h.sender()?.unwrap(), ":1.84"); - assert_eq!(h.signature()?, None); - assert_eq!(h.unix_fds()?, None); + assert_eq!(h.message_type(), Type::Signal); + assert_eq!(h.path(), Some(&path)); + assert_eq!(h.interface(), Some(&iface)); + assert_eq!(h.member(), Some(&member)); + assert_eq!(h.error_name(), None); + assert_eq!(h.destination(), None); + assert_eq!(h.reply_serial(), None); + assert_eq!(h.sender().unwrap(), ":1.84"); + assert_eq!(h.signature(), None); + assert_eq!(h.unix_fds(), None); let mut f = Fields::new(); f.add(Field::ErrorName("org.zbus.Error".try_into()?)); @@ -363,16 +365,16 @@ mod tests { f.add(Field::UnixFDs(12)); let h = Header::new(PrimaryHeader::new(Type::MethodReturn, 77), f); - assert_eq!(h.message_type()?, Type::MethodReturn); - assert_eq!(h.path()?, None); - assert_eq!(h.interface()?, None); - assert_eq!(h.member()?, None); - assert_eq!(h.error_name()?.unwrap(), "org.zbus.Error"); - assert_eq!(h.destination()?.unwrap(), ":1.11"); - assert_eq!(h.reply_serial()?.map(Into::into), Some(88)); - assert_eq!(h.sender()?, None); - assert_eq!(h.signature()?, Some(&Signature::from_str_unchecked("say"))); - assert_eq!(h.unix_fds()?, Some(12)); + assert_eq!(h.message_type(), Type::MethodReturn); + assert_eq!(h.path(), None); + assert_eq!(h.interface(), None); + assert_eq!(h.member(), None); + assert_eq!(h.error_name().unwrap(), "org.zbus.Error"); + assert_eq!(h.destination().unwrap(), ":1.11"); + assert_eq!(h.reply_serial().map(Into::into), Some(88)); + assert_eq!(h.sender(), None); + assert_eq!(h.signature(), Some(&Signature::from_str_unchecked("say"))); + assert_eq!(h.unix_fds(), Some(12)); Ok(()) } diff --git a/zbus/src/message/mod.rs b/zbus/src/message/mod.rs index 27cbe7b8b..299f10028 100644 --- a/zbus/src/message/mod.rs +++ b/zbus/src/message/mod.rs @@ -507,22 +507,20 @@ impl fmt::Debug for Message { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let mut msg = f.debug_struct("Msg"); let h = self.header(); - if let Ok(t) = h.message_type() { - msg.field("type", &t); - } - if let Ok(Some(sender)) = h.sender() { + msg.field("type", &h.message_type()); + if let Some(sender) = h.sender() { msg.field("sender", &sender); } - if let Ok(Some(serial)) = h.reply_serial() { + if let Some(serial) = h.reply_serial() { msg.field("reply-serial", &serial); } - if let Ok(Some(path)) = h.path() { + if let Some(path) = h.path() { msg.field("path", &path); } - if let Ok(Some(iface)) = h.interface() { + if let Some(iface) = h.interface() { msg.field("iface", &iface); } - if let Ok(Some(member)) = h.member() { + if let Some(member) = h.member() { msg.field("member", &member); } if let Some(s) = self.body_signature() { @@ -543,23 +541,23 @@ impl fmt::Display for Message { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let header = self.header(); let (ty, error_name, sender, member) = ( - header.message_type().ok(), - header.error_name().ok().flatten(), - header.sender().ok().flatten(), - header.member().ok().flatten(), + header.message_type(), + header.error_name(), + header.sender(), + header.member(), ); match ty { - Some(Type::MethodCall) => { + Type::MethodCall => { write!(f, "Method call")?; if let Some(m) = member { write!(f, " {m}")?; } } - Some(Type::MethodReturn) => { + Type::MethodReturn => { write!(f, "Method return")?; } - Some(Type::Error) => { + Type::Error => { write!(f, "Error")?; if let Some(e) = error_name { write!(f, " {e}")?; @@ -570,15 +568,12 @@ impl fmt::Display for Message { write!(f, ": {msg}")?; } } - Some(Type::Signal) => { + Type::Signal => { write!(f, "Signal")?; if let Some(m) = member { write!(f, " {m}")?; } } - _ => { - write!(f, "Unknown message")?; - } } if let Some(s) = sender { diff --git a/zbus/src/proxy/mod.rs b/zbus/src/proxy/mod.rs index 489bd2b2a..2bf79579c 100644 --- a/zbus/src/proxy/mod.rs +++ b/zbus/src/proxy/mod.rs @@ -1233,7 +1233,7 @@ impl<'a> SignalStream<'a> { fn filter(&mut self, msg: &Arc) -> Result { let header = msg.header(); - let sender = header.sender()?; + let sender = header.sender(); if sender == self.src_unique_name.as_ref() { return Ok(true); } diff --git a/zbus/tests/e2e.rs b/zbus/tests/e2e.rs index e63166621..d14828b5e 100644 --- a/zbus/tests/e2e.rs +++ b/zbus/tests/e2e.rs @@ -174,8 +174,8 @@ impl MyIfaceImpl { #[instrument] fn test_header(&self, #[zbus(header)] header: Header<'_>) { debug!("`TestHeader` called."); - assert_eq!(header.message_type().unwrap(), message::Type::MethodCall); - assert_eq!(header.member().unwrap().unwrap(), "TestHeader"); + assert_eq!(header.message_type(), message::Type::MethodCall); + assert_eq!(header.member().unwrap(), "TestHeader"); } #[instrument] @@ -197,7 +197,7 @@ impl MyIfaceImpl { #[zbus(header)] header: Header<'_>, ) -> zbus::fdo::Result<()> { debug!("`TestSingleStructArg` called."); - assert_eq!(header.signature()?.unwrap(), "(is)"); + assert_eq!(header.signature().unwrap(), "(is)"); assert_eq!(arg.foo, 1); assert_eq!(arg.bar, "TestString"); @@ -385,10 +385,7 @@ impl MyIfaceImpl { #[instrument] fn test_no_reply(&self, #[zbus(header)] header: Header<'_>) { debug!("`TestNoReply` called"); - assert_eq!( - header.message_type().unwrap(), - zbus::message::Type::MethodCall - ); + assert_eq!(header.message_type(), zbus::message::Type::MethodCall); assert!(header .primary() .flags() @@ -398,10 +395,7 @@ impl MyIfaceImpl { #[instrument] fn test_no_autostart(&self, #[zbus(header)] header: Header<'_>) { debug!("`TestNoAutostart` called"); - assert_eq!( - header.message_type().unwrap(), - zbus::message::Type::MethodCall - ); + assert_eq!(header.message_type(), zbus::message::Type::MethodCall); assert!(header .primary() .flags() @@ -411,10 +405,7 @@ impl MyIfaceImpl { #[instrument] fn test_interactive_auth(&self, #[zbus(header)] header: Header<'_>) { debug!("`TestInteractiveAuth` called"); - assert_eq!( - header.message_type().unwrap(), - zbus::message::Type::MethodCall - ); + assert_eq!(header.message_type(), zbus::message::Type::MethodCall); assert!(header .primary() .flags() @@ -457,13 +448,12 @@ async fn my_iface_test(conn: Connection, event: Event) -> zbus::Result { let msg = stream.try_next().await?.unwrap(); let hdr = msg.header(); - if hdr.message_type()? == message::Type::MethodReturn && hdr.reply_serial()? == Some(serial) - { + if hdr.message_type() == message::Type::MethodReturn && hdr.reply_serial() == Some(serial) { assert!(!signal_received); method_returned = true; - } else if hdr.message_type()? == message::Type::Signal - && hdr.interface()?.unwrap() == "org.freedesktop.MyService" - && hdr.member()?.unwrap() == "TestResponseNotified" + } else if hdr.message_type() == message::Type::Signal + && hdr.interface().unwrap() == "org.freedesktop.MyService" + && hdr.member().unwrap() == "TestResponseNotified" { assert!(method_returned); signal_received = true; diff --git a/zbus_macros/src/lib.rs b/zbus_macros/src/lib.rs index a0a71bc38..fc3701a58 100644 --- a/zbus_macros/src/lib.rs +++ b/zbus_macros/src/lib.rs @@ -276,7 +276,7 @@ pub fn dbus_proxy(attr: TokenStream, item: TokenStream) -> TokenStream { /// #[zbus(object_server)] /// _server: &ObjectServer, /// ) -> zbus::fdo::Result<()> { -/// let path = hdr.path()?.unwrap(); +/// let path = hdr.path().unwrap(); /// let msg = format!("You are leaving me on the {} path?", path); /// Example::bye(&ctxt, &msg).await?; /// From bf17866b7c7042933c3a27df9308ba7d7d234d32 Mon Sep 17 00:00:00 2001 From: Zeeshan Ali Khan Date: Tue, 15 Aug 2023 00:10:48 +0200 Subject: [PATCH 16/21] =?UTF-8?q?=F0=9F=94=A5=20zb:=20Drop=20all=20use=20o?= =?UTF-8?q?f=20direct=20field=20getters=20of=20Message?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit They'll be deprecated in a following commit. --- zbus/src/connection/mod.rs | 4 ++-- zbus/src/lib.rs | 7 ++++--- zbus/src/match_rule/mod.rs | 8 ++++---- zbus/src/object_server/mod.rs | 9 +++++---- zbus_macros/src/iface.rs | 6 +++--- zbus_macros/src/proxy.rs | 5 +++-- 6 files changed, 21 insertions(+), 18 deletions(-) diff --git a/zbus/src/connection/mod.rs b/zbus/src/connection/mod.rs index d82688d97..9aa7ba45f 100644 --- a/zbus/src/connection/mod.rs +++ b/zbus/src/connection/mod.rs @@ -261,7 +261,7 @@ impl OrderedFuture for PendingMethodCall { data: Ok(msg), ordering, }) => { - if msg.reply_serial() != Some(this.serial) { + if msg.header().reply_serial() != Some(this.serial) { continue; } let res = match msg.message_type() { @@ -1011,7 +1011,7 @@ impl Connection { } } } - let member = match msg.member() { + let member = match hdr.member() { Some(member) => member, None => { warn!("Got a method call with no `MEMBER` field: {}", msg); diff --git a/zbus/src/lib.rs b/zbus/src/lib.rs index 06482cce5..767862cd2 100644 --- a/zbus/src/lib.rs +++ b/zbus/src/lib.rs @@ -253,9 +253,10 @@ mod tests { &(), ) .unwrap(); - assert_eq!(m.path().unwrap(), "/org/freedesktop/DBus"); - assert_eq!(m.interface().unwrap(), "org.freedesktop.DBus.Peer"); - assert_eq!(m.member().unwrap(), "GetMachineId"); + let hdr = m.header(); + assert_eq!(hdr.path().unwrap(), "/org/freedesktop/DBus"); + assert_eq!(hdr.interface().unwrap(), "org.freedesktop.DBus.Peer"); + assert_eq!(hdr.member().unwrap(), "GetMachineId"); m.modify_primary_header(|primary| { primary.set_flags(BitFlags::from(Flags::NoAutoStart)); primary.set_serial_num(11.try_into().unwrap()); diff --git a/zbus/src/match_rule/mod.rs b/zbus/src/match_rule/mod.rs index 4822efb5d..dcad9bf19 100644 --- a/zbus/src/match_rule/mod.rs +++ b/zbus/src/match_rule/mod.rs @@ -229,7 +229,7 @@ impl<'m> MatchRule<'m> { // The interface. if let Some(interface) = self.interface() { - match msg.interface().as_ref() { + match hdr.interface() { Some(msg_interface) if interface != msg_interface => return Ok(false), Some(_) => (), None => return Ok(false), @@ -238,7 +238,7 @@ impl<'m> MatchRule<'m> { // The member. if let Some(member) = self.member() { - match msg.member().as_ref() { + match hdr.member() { Some(msg_member) if member != msg_member => return Ok(false), Some(_) => (), None => return Ok(false), @@ -259,12 +259,12 @@ impl<'m> MatchRule<'m> { // The path. if let Some(path_spec) = self.path_spec() { - let msg_path = match msg.path() { + let msg_path = match hdr.path() { Some(p) => p, None => return Ok(false), }; match path_spec { - PathSpec::Path(path) if path != &msg_path => return Ok(false), + PathSpec::Path(path) if path != msg_path => return Ok(false), PathSpec::PathNamespace(path_ns) if !msg_path.starts_with(path_ns.as_str()) => { return Ok(false); } diff --git a/zbus/src/object_server/mod.rs b/zbus/src/object_server/mod.rs index 075b6b61b..85c6aab7a 100644 --- a/zbus/src/object_server/mod.rs +++ b/zbus/src/object_server/mod.rs @@ -647,10 +647,11 @@ impl ObjectServer { connection: &Connection, msg: &Message, ) -> fdo::Result> { - let path = msg + let hdr = msg.header(); + let path = hdr .path() .ok_or_else(|| fdo::Error::Failed("Missing object path".into()))?; - let iface_name = msg + let iface_name = hdr .interface() // TODO: In the absence of an INTERFACE field, if two or more interfaces on the same // object have a method with the same name, it is undefined which of those @@ -658,7 +659,7 @@ impl ObjectServer { // error, or deliver the message as though it had an arbitrary one of those // interfaces. .ok_or_else(|| fdo::Error::Failed("Missing interface".into()))?; - let member = msg + let member = hdr .member() .ok_or_else(|| fdo::Error::Failed("Missing member".into()))?; @@ -667,7 +668,7 @@ impl ObjectServer { let iface = { let root = self.root.read().await; let node = root - .get_child(&path) + .get_child(path) .ok_or_else(|| fdo::Error::UnknownObject(format!("Unknown object '{path}'")))?; node.interface_lock(iface_name.as_ref()).ok_or_else(|| { diff --git a/zbus_macros/src/iface.rs b/zbus_macros/src/iface.rs index 5db95a748..d77f5b6fa 100644 --- a/zbus_macros/src/iface.rs +++ b/zbus_macros/src/iface.rs @@ -625,12 +625,11 @@ fn get_args_from_inputs( let signal_context_arg = &input.pat; signal_context_arg_decl = Some(quote! { - let #signal_context_arg = match m.path() { + let #signal_context_arg = match hdr.path() { ::std::option::Option::Some(p) => { #zbus::object_server::SignalContext::new(c, p).expect("Infallible conversion failed") } ::std::option::Option::None => { - let hdr = m.header(); let err = #zbus::fdo::Error::UnknownObject("Path Required".into()); return c.reply_dbus_error(&hdr, err).await; } @@ -643,6 +642,8 @@ fn get_args_from_inputs( } let args_from_msg = quote! { + let hdr = m.header(); + #server_arg_decl #conn_arg_decl @@ -655,7 +656,6 @@ fn get_args_from_inputs( match m.body() { ::std::result::Result::Ok(r) => r, ::std::result::Result::Err(e) => { - let hdr = m.header(); let err = <#zbus::fdo::Error as ::std::convert::From<_>>::from(e); return c.reply_dbus_error(&hdr, err).await; } diff --git a/zbus_macros/src/proxy.rs b/zbus_macros/src/proxy.rs index 5b1f50ca3..dc0054fac 100644 --- a/zbus_macros/src/proxy.rs +++ b/zbus_macros/src/proxy.rs @@ -989,9 +989,10 @@ fn gen_proxy_signal( M: ::std::convert::Into<::std::sync::Arc<#zbus::message::Message>>, { let msg = msg.into(); + let hdr = msg.header(); let message_type = msg.message_type(); - let interface = msg.interface(); - let member = msg.member(); + let interface = hdr.interface(); + let member = hdr.member(); let interface = interface.as_ref().map(|i| i.as_str()); let member = member.as_ref().map(|m| m.as_str()); From 260db585f110dcc32a74423209a981f399a35529 Mon Sep 17 00:00:00 2001 From: Zeeshan Ali Khan Date: Tue, 15 Aug 2023 00:11:48 +0200 Subject: [PATCH 17/21] =?UTF-8?q?=F0=9F=97=91=EF=B8=8F=20zb:=20Deprecate?= =?UTF-8?q?=20direct=20fields=20gettes=20of=20Message?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Not only is the performance of the direct fields getters no longer much worse than the getting the fields through the header, but also field getters on the header are much faster the that of Message. Moreover, I've ideas on how to make the header getter even faster. --- zbus/src/message/mod.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/zbus/src/message/mod.rs b/zbus/src/message/mod.rs index 299f10028..5763436cd 100644 --- a/zbus/src/message/mod.rs +++ b/zbus/src/message/mod.rs @@ -359,21 +359,25 @@ impl Message { } /// The object to send a call to, or the object a signal is emitted from. + #[deprecated(note = "Use `Message::header` with `message::Header::path` instead")] pub fn path(&self) -> Option> { self.quick_fields.path(self) } /// The interface to invoke a method call on, or that a signal is emitted from. + #[deprecated(note = "Use `Message::header` with `message::Header::interface` instead")] pub fn interface(&self) -> Option> { self.quick_fields.interface(self) } /// The member, either the method name or signal name. + #[deprecated(note = "Use `Message::header` with `message::Header::member` instead")] pub fn member(&self) -> Option> { self.quick_fields.member(self) } /// The serial number of the message this message is a reply to. + #[deprecated(note = "Use `Message::header` with `message::Header::reply_serial` instead")] pub fn reply_serial(&self) -> Option { self.quick_fields.reply_serial() } From 303cfdcd4203cfd60c14f709c549cf740c5bbd7e Mon Sep 17 00:00:00 2001 From: Zeeshan Ali Khan Date: Tue, 15 Aug 2023 16:00:10 +0200 Subject: [PATCH 18/21] =?UTF-8?q?=F0=9F=94=A5=20zb:=20Drop=20message::Head?= =?UTF-8?q?er::new()=20from=20public=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I don't see users having much need for this and we're about to make `Field*` API private. --- zbus/src/message/header.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zbus/src/message/header.rs b/zbus/src/message/header.rs index c361deb47..def80b54c 100644 --- a/zbus/src/message/header.rs +++ b/zbus/src/message/header.rs @@ -235,7 +235,7 @@ macro_rules! get_field_u32 { impl<'m> Header<'m> { /// Create a new `Header` instance. - pub fn new(primary: PrimaryHeader, fields: Fields<'m>) -> Self { + pub(super) fn new(primary: PrimaryHeader, fields: Fields<'m>) -> Self { Self { primary, fields } } From 6d27d1e76d74ecd229e6715362d35f00df3baab7 Mon Sep 17 00:00:00 2001 From: Zeeshan Ali Khan Date: Tue, 15 Aug 2023 16:03:22 +0200 Subject: [PATCH 19/21] =?UTF-8?q?=F0=9F=94=A5=20zb:=20Drop=20message::Fiel?= =?UTF-8?q?ds::into=5Ffield?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We don't need this method internally, doubt anyone uses it and we're about to turn Field* API private anyway. --- zbus/src/message/fields.rs | 9 --------- 1 file changed, 9 deletions(-) diff --git a/zbus/src/message/fields.rs b/zbus/src/message/fields.rs index edeb7f923..00ad17553 100644 --- a/zbus/src/message/fields.rs +++ b/zbus/src/message/fields.rs @@ -62,15 +62,6 @@ impl<'m> Fields<'m> { self.0.iter().find(|f| f.code() == code) } - /// Consumes the `Fields` and returns a specific [`Field`] by its code. - /// - /// Returns `None` if the message has no such field. - /// - /// [`Field`]: enum.Field.html - pub fn into_field(self, code: FieldCode) -> Option> { - self.0.into_iter().find(|f| f.code() == code) - } - /// Remove the field matching the `code`. /// /// Returns `true` if a field was found and removed, `false` otherwise. From 8b1376b0746d465e7164399527114e5bbc1405fe Mon Sep 17 00:00:00 2001 From: Zeeshan Ali Khan Date: Tue, 15 Aug 2023 16:03:22 +0200 Subject: [PATCH 20/21] =?UTF-8?q?=F0=9F=94=A5=20zb:=20Drop=20message::Head?= =?UTF-8?q?er::into=5Ffields?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We don't need this method internally, doubt anyone uses it and we're about to turn Field* API private anyway. --- zbus/src/message/header.rs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/zbus/src/message/header.rs b/zbus/src/message/header.rs index def80b54c..ec3138319 100644 --- a/zbus/src/message/header.rs +++ b/zbus/src/message/header.rs @@ -264,11 +264,6 @@ impl<'m> Header<'m> { &mut self.fields } - /// Get the message fields, consuming `self`. - pub fn into_fields(self) -> Fields<'m> { - self.fields - } - /// The message type pub fn message_type(&self) -> Type { self.primary().msg_type() From 8db83bd36d86e41f41e1c17097c8cab04cc19027 Mon Sep 17 00:00:00 2001 From: Zeeshan Ali Khan Date: Tue, 15 Aug 2023 16:11:36 +0200 Subject: [PATCH 21/21] =?UTF-8?q?=F0=9F=94=A5=20zb:=20message::Field*=20AP?= =?UTF-8?q?I=20now=20private?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This exposes way too much internal details about the protocol that users do not need. The specific field getters we provide are sufficient for any low-level code. I wrote most of this when I was still very new to Rust and I started with a very bottoms-up approach. Hence we these API had been public until now. I doubt anyone uses these anyway so chances of people's code breaking are low but even if they do, it's a API breaking release. --- zbus/src/lib.rs | 10 ---------- zbus/src/message/field.rs | 4 ++-- zbus/src/message/fields.rs | 2 +- zbus/src/message/header.rs | 4 ++-- zbus/src/message/mod.rs | 14 +++----------- 5 files changed, 8 insertions(+), 26 deletions(-) diff --git a/zbus/src/lib.rs b/zbus/src/lib.rs index 767862cd2..9408503c8 100644 --- a/zbus/src/lib.rs +++ b/zbus/src/lib.rs @@ -70,16 +70,6 @@ pub use message::Builder as MessageBuilder; #[deprecated(note = "Use `message::EndianSig` instead")] #[doc(hidden)] pub use message::EndianSig; -#[deprecated(note = "Use `message::Field` instead")] -#[doc(hidden)] -pub use message::Field as MessageField; -#[deprecated(note = "Use `message::FieldCode` instead")] -#[doc(hidden)] -pub use message::FieldCode as MessageFieldCode; -#[deprecated(note = "Use `message::Fields` instead")] -#[doc(hidden)] -pub use message::Fields as MessageFields; -#[deprecated(note = "Use `message::Flags` instead")] #[doc(hidden)] pub use message::Flags as MessageFlags; #[deprecated(note = "Use `message::Header` instead")] diff --git a/zbus/src/message/field.rs b/zbus/src/message/field.rs index e61d7ea23..90df58c28 100644 --- a/zbus/src/message/field.rs +++ b/zbus/src/message/field.rs @@ -21,7 +21,7 @@ use zvariant::{ObjectPath, Signature, Type, Value}; /// [`Fields`]: struct.Fields.html #[repr(u8)] #[derive(Copy, Clone, Debug, Deserialize_repr, PartialEq, Eq, Serialize_repr, Type)] -pub enum FieldCode { +pub(super) enum FieldCode { /// Code for [`Field::Path`](enum.Field.html#variant.Path) Path = 1, /// Code for [`Field::Interface`](enum.Field.html#variant.Interface) @@ -73,7 +73,7 @@ impl<'f> Field<'f> { /// [are fixed]: struct.PrimaryHeader.html /// [Message Format]: https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-messages #[derive(Clone, Debug, PartialEq, Eq)] -pub enum Field<'f> { +pub(super) enum Field<'f> { /// The object to send a call to, or the object a signal is emitted from. Path(ObjectPath<'f>), /// The interface to invoke a method call on, or that a signal is emitted from. diff --git a/zbus/src/message/fields.rs b/zbus/src/message/fields.rs index 00ad17553..a152b5e5a 100644 --- a/zbus/src/message/fields.rs +++ b/zbus/src/message/fields.rs @@ -16,7 +16,7 @@ const MAX_FIELDS_IN_MESSAGE: usize = 16; /// /// [`Field`]: enum.Field.html #[derive(Debug, Clone, Serialize, Deserialize, Type)] -pub struct Fields<'m>(#[serde(borrow)] Vec>); +pub(super) struct Fields<'m>(#[serde(borrow)] Vec>); assert_impl_all!(Fields<'_>: Send, Sync, Unpin); diff --git a/zbus/src/message/header.rs b/zbus/src/message/header.rs index ec3138319..c971faa3c 100644 --- a/zbus/src/message/header.rs +++ b/zbus/src/message/header.rs @@ -255,12 +255,12 @@ impl<'m> Header<'m> { } /// Get a reference to the message fields. - pub fn fields(&self) -> &Fields<'m> { + fn fields(&self) -> &Fields<'m> { &self.fields } /// Get a mutable reference to the message fields. - pub fn fields_mut(&mut self) -> &mut Fields<'m> { + pub(super) fn fields_mut(&mut self) -> &mut Fields<'m> { &mut self.fields } diff --git a/zbus/src/message/mod.rs b/zbus/src/message/mod.rs index 5763436cd..45f53fd24 100644 --- a/zbus/src/message/mod.rs +++ b/zbus/src/message/mod.rs @@ -22,11 +22,10 @@ mod builder; pub use builder::Builder; mod field; -pub use field::{Field, FieldCode}; +use field::{Field, FieldCode}; mod fields; -pub use fields::Fields; -use fields::QuickFields; +use fields::{Fields, QuickFields}; pub(crate) mod header; use header::MIN_MESSAGE_SIZE; @@ -313,13 +312,6 @@ impl Message { /// zero-cost. While the allocation is small and will hopefully be removed in the future, it's /// best to keep the header around if you need to access it a lot. pub fn header(&self) -> Header<'_> { - Header::new(self.primary_header.clone(), self.fields()) - } - - /// The message header fields. - /// - /// The note on [`Message::header`] applies here too. - pub fn fields(&self) -> Fields<'_> { let mut fields = Fields::new(); let quick_fields = &self.quick_fields; if let Some(p) = quick_fields.path(self) { @@ -350,7 +342,7 @@ impl Message { fields.add(Field::UnixFDs(u)); } - fields + Header::new(self.primary_header.clone(), fields) } /// The message type.