pub struct Message { /* private fields */ }
Expand description
A D-Bus Message.
The content of the message are stored in serialized format. To get the body of the message, use
the Message::body
method, and use Body
methods to deserialize it. You may also access
the header and other details with the various other getters.
Also provided are constructors for messages of different types. These will mainly be useful for
very advanced use cases as typically you will want to create a message for immediate dispatch
and hence use the API provided by Connection
, even when using the low-level API.
Note: The message owns the received FDs and will close them when dropped. You can
deserialize to zvariant::OwnedFd
the body (that you get using Message::body
) if you want
to keep the FDs around after the containing message is dropped.
Implementations§
Source§impl Message
impl Message
Sourcepub fn method<'b, 'p: 'b, 'm: 'b, P, M>(
path: P,
method_name: M,
) -> Result<Builder<'b>>where
P: TryInto<ObjectPath<'p>>,
M: TryInto<MemberName<'m>>,
P::Error: Into<Error>,
M::Error: Into<Error>,
pub fn method<'b, 'p: 'b, 'm: 'b, P, M>(
path: P,
method_name: M,
) -> Result<Builder<'b>>where
P: TryInto<ObjectPath<'p>>,
M: TryInto<MemberName<'m>>,
P::Error: Into<Error>,
M::Error: Into<Error>,
Create a builder for message of type Type::MethodCall
.
Sourcepub fn signal<'b, 'p: 'b, 'i: 'b, 'm: 'b, P, I, M>(
path: P,
iface: I,
signal_name: M,
) -> Result<Builder<'b>>where
P: TryInto<ObjectPath<'p>>,
I: TryInto<InterfaceName<'i>>,
M: TryInto<MemberName<'m>>,
P::Error: Into<Error>,
I::Error: Into<Error>,
M::Error: Into<Error>,
pub fn signal<'b, 'p: 'b, 'i: 'b, 'm: 'b, P, I, M>(
path: P,
iface: I,
signal_name: M,
) -> Result<Builder<'b>>where
P: TryInto<ObjectPath<'p>>,
I: TryInto<InterfaceName<'i>>,
M: TryInto<MemberName<'m>>,
P::Error: Into<Error>,
I::Error: Into<Error>,
M::Error: Into<Error>,
Create a builder for message of type Type::Signal
.
Sourcepub fn method_reply(call: &Self) -> Result<Builder<'_>>
pub fn method_reply(call: &Self) -> Result<Builder<'_>>
Create a builder for message of type Type::MethodReturn
.
Sourcepub fn method_error<'b, 'e: 'b, E>(call: &Self, name: E) -> Result<Builder<'b>>
pub fn method_error<'b, 'e: 'b, E>(call: &Self, name: E) -> Result<Builder<'b>>
Create a builder for message of type Type::Error
.
Sourcepub unsafe fn from_bytes(bytes: Data<'static, 'static>) -> Result<Self>
pub unsafe fn from_bytes(bytes: Data<'static, 'static>) -> Result<Self>
Create a message from bytes.
Note: Since the constructed message is not construct by zbus, the receive sequence,
which can be acquired from Message::recv_position
, is not applicable and hence set
to 0
.
§Safety
This method is unsafe as bytes may have an invalid encoding.
pub fn primary_header(&self) -> &PrimaryHeader
Sourcepub fn header(&self) -> Header<'_>
pub fn header(&self) -> Header<'_>
The message header.
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.
Sourcepub fn message_type(&self) -> Type
pub fn message_type(&self) -> Type
The message type.
Sourcepub fn path(&self) -> Option<ObjectPath<'_>>
👎Deprecated since 4.0.0: Use Message::header
with message::Header::path
instead
pub fn path(&self) -> Option<ObjectPath<'_>>
Message::header
with message::Header::path
insteadThe object to send a call to, or the object a signal is emitted from.
Sourcepub fn interface(&self) -> Option<InterfaceName<'_>>
👎Deprecated since 4.0.0: Use Message::header
with message::Header::interface
instead
pub fn interface(&self) -> Option<InterfaceName<'_>>
Message::header
with message::Header::interface
insteadThe interface to invoke a method call on, or that a signal is emitted from.
Sourcepub fn member(&self) -> Option<MemberName<'_>>
👎Deprecated since 4.0.0: Use Message::header
with message::Header::member
instead
pub fn member(&self) -> Option<MemberName<'_>>
Message::header
with message::Header::member
insteadThe member, either the method name or signal name.
Sourcepub fn reply_serial(&self) -> Option<NonZeroU32>
👎Deprecated since 4.0.0: Use Message::header
with message::Header::reply_serial
instead
pub fn reply_serial(&self) -> Option<NonZeroU32>
Message::header
with message::Header::reply_serial
insteadThe serial number of the message this message is a reply to.
Sourcepub fn body(&self) -> Body
pub fn body(&self) -> Body
The body that you can deserialize using Body::deserialize
.
§Example
let send_body = (7i32, (2i32, "foo"), vec!["bar"]);
let message = Message::method("/", "ping")?
.destination("zbus.test")?
.interface("zbus.test")?
.build(&send_body)?;
let body = message.body();
let body: zbus::zvariant::Structure = body.deserialize()?;
let fields = body.fields();
assert!(matches!(fields[0], zvariant::Value::I32(7)));
assert!(matches!(fields[1], zvariant::Value::Structure(_)));
assert!(matches!(fields[2], zvariant::Value::Array(_)));
let reply_body = Message::method_reply(&message)?.build(&body)?.body();
let reply_value : (i32, (i32, &str), Vec<String>) = reply_body.deserialize()?;
assert_eq!(reply_value.0, 7);
assert_eq!(reply_value.2.len(), 1);
Sourcepub fn data(&self) -> &Data<'static, 'static>
pub fn data(&self) -> &Data<'static, 'static>
Get a reference to the underlying byte encoding of the message.
Sourcepub fn recv_position(&self) -> Sequence
pub fn recv_position(&self) -> Sequence
Get the receive ordering of a message.
This may be used to identify how two events were ordered on the bus. It only produces a
useful ordering for messages that were produced by the same zbus::Connection
.
This is completely unrelated to the serial number on the message, which is set by the peer and might not be ordered at all.