Struct tokio_dbus::Message

source ·
pub struct Message<'a> { /* private fields */ }
Expand description

A borrowed D-Bus message.

This is the borrowed variant of OwnedMessage, to convert to an OwnedMessage, use Message::to_owned.

Implementations§

source§

impl<'a> Message<'a>

source

pub fn method_call( path: &'a ObjectPath, member: &'a str, serial: NonZeroU32 ) -> Self

Construct a method call Message.

Examples
use std::num::NonZeroU32;

use tokio_dbus::{Message, ObjectPath, SendBuf};

const PATH: &ObjectPath = ObjectPath::new_const(b"/org/freedesktop/DBus");

let mut send = SendBuf::new();

let m = send.method_call(PATH, "Hello");
let m2 = Message::method_call(PATH, "Hello", m.serial());
assert_eq!(m, m2);
source

pub fn method_return(&self, serial: NonZeroU32) -> Self

Convert this message into a [MessageKind::MessageReturn] message with an empty body where the reply serial matches that of the current message.

The send argument is used to populate the next serial number.

Examples
use std::num::NonZeroU32;

use tokio_dbus::{Message, MessageKind, ObjectPath, SendBuf};

const PATH: &ObjectPath = ObjectPath::new_const(b"/org/freedesktop/DBus");

let mut send = SendBuf::new();

let m = send.method_call(PATH, "Hello")
    .with_sender("se.tedro.DBusExample")
    .with_destination("org.freedesktop.DBus");

let m2 = m.method_return(send.next_serial());
assert!(matches!(m2.kind(), MessageKind::MethodReturn { .. }));

assert_eq!(m.sender(), m2.destination());
assert_eq!(m.destination(), m2.sender());
source

pub fn signal(member: &'a str, serial: NonZeroU32) -> Self

Construct a signal Message.

Examples
use std::num::NonZeroU32;

use tokio_dbus::{Message, ObjectPath, SendBuf};

const PATH: &ObjectPath = ObjectPath::new_const(b"/org/freedesktop/DBus");

let mut send = SendBuf::new();

let m = send.signal("Hello");
let m2 = Message::signal("Hello", m.serial());
assert_eq!(m, m2);
source

pub fn error(&self, error_name: &'a str, serial: NonZeroU32) -> Self

Convert this message into a MessageKind::Error message with an empty body where the reply serial matches that of the current message.

Examples
use std::num::NonZeroU32;

use tokio_dbus::{Message, MessageKind, ObjectPath, SendBuf};

const PATH: &ObjectPath = ObjectPath::new_const(b"/org/freedesktop/DBus");

let mut send = SendBuf::new();

let m = send.method_call(PATH, "Hello")
    .with_sender("se.tedro.DBusExample")
    .with_destination("org.freedesktop.DBus");

let m2 = m.error("org.freedesktop.DBus.UnknownMethod", send.next_serial());
assert!(matches!(m2.kind(), MessageKind::Error { .. }));

assert_eq!(m.sender(), m2.destination());
assert_eq!(m.destination(), m2.sender());
source

pub fn to_owned(&self) -> OwnedMessage

Convert into an owned OwnedMessage.

Examples
use std::num::NonZeroU32;

use tokio_dbus::{Message, OwnedMessage, ObjectPath, SendBuf};

const PATH: &ObjectPath = ObjectPath::new_const(b"/org/freedesktop/DBus");

let mut send = SendBuf::new();

let m = send.method_call(PATH, "Hello").to_owned();
let m2 = OwnedMessage::method_call(PATH.into(), "Hello".into(), m.serial());
assert_eq!(m, m2);
source

pub fn kind(&self) -> MessageKind<'a>

Get the kind of the message.

Examples
use std::num::NonZeroU32;

use tokio_dbus::{Message, MessageKind, ObjectPath, SendBuf};

const PATH: &ObjectPath = ObjectPath::new_const(b"/org/freedesktop/DBus");

let mut send = SendBuf::new();

let m = send.method_call(PATH, "Hello");
assert!(matches!(m.kind(), MessageKind::MethodCall { .. }));

let m2 = m.error("org.freedesktop.DBus.UnknownMethod", send.next_serial());
assert!(matches!(m2.kind(), MessageKind::Error { .. }));
source

pub fn with_body_buf(self, body: &'a BodyBuf) -> Self

Modify the body and signature of the message to match that of the provided body buffer.

Examples
use std::num::NonZeroU32;

use tokio_dbus::{BodyBuf, Message, MessageKind, ObjectPath, SendBuf, Signature};

const PATH: &ObjectPath = ObjectPath::new_const(b"/org/freedesktop/DBus");

let mut send = SendBuf::new();
let mut body = BodyBuf::new();

body.write("Hello World!");

let m = send.method_call(PATH, "Hello")
    .with_body_buf(&body);

assert!(matches!(m.kind(), MessageKind::MethodCall { .. }));
assert_eq!(m.signature(), Signature::STRING);
source

pub fn body(&self) -> ReadBuf<'a>

Get a buffer to the body of the message.

Examples
use std::num::NonZeroU32;

use tokio_dbus::{BodyBuf, Message, MessageKind, ObjectPath, SendBuf, Signature};

const PATH: &ObjectPath = ObjectPath::new_const(b"/org/freedesktop/DBus");

let mut send = SendBuf::new();
let mut body = BodyBuf::new();

body.store(42u32);
body.write("Hello World!");

let m = send.method_call(PATH, "Hello")
    .with_body_buf(&body);

assert!(matches!(m.kind(), MessageKind::MethodCall { .. }));
assert_eq!(m.signature(), Signature::new(b"us")?);

let mut r = m.body();
assert_eq!(r.load::<u32>()?, 42);
assert_eq!(r.read::<str>()?, "Hello World!");
source

pub fn with_body(self, body: ReadBuf<'a>) -> Self

Modify the body of the message.

Examples
use std::num::NonZeroU32;

use tokio_dbus::{BodyBuf, Message, MessageKind, ObjectPath, SendBuf, Signature};

const PATH: &ObjectPath = ObjectPath::new_const(b"/org/freedesktop/DBus");

let mut send = SendBuf::new();
let mut body = BodyBuf::new();

body.store(42u32);
body.write("Hello World!");

let m = send.method_call(PATH, "Hello")
    .with_body(body.read())
    .with_signature(body.signature());

assert!(matches!(m.kind(), MessageKind::MethodCall { .. }));
assert_eq!(m.signature(), Signature::new(b"us")?);

let mut r = m.body();
assert_eq!(r.load::<u32>()?, 42);
assert_eq!(r.read::<str>()?, "Hello World!");
source

pub fn serial(&self) -> NonZeroU32

Get the serial of the message.

Examples
use std::num::NonZeroU32;

use tokio_dbus::{Message, ObjectPath, SendBuf};

const PATH: &ObjectPath = ObjectPath::new_const(b"/org/freedesktop/DBus");

let mut send = SendBuf::new();

let m = send.method_call(PATH, "Hello");
assert_eq!(m.serial().get(), 1);

let m2 = m.with_serial(NonZeroU32::new(1000).unwrap());
assert_eq!(m2.serial().get(), 1000);
source

pub fn with_serial(self, serial: NonZeroU32) -> Self

Modify the serial of the message.

Examples
use std::num::NonZeroU32;

use tokio_dbus::{Message, ObjectPath, SendBuf};

const PATH: &ObjectPath = ObjectPath::new_const(b"/org/freedesktop/DBus");

let mut send = SendBuf::new();

let m = send.method_call(PATH, "Hello");
assert_eq!(m.serial().get(), 1);

let m2 = m.with_serial(NonZeroU32::new(1000).unwrap());
assert_eq!(m2.serial().get(), 1000);
source

pub fn flags(&self) -> Flags

Get the flags of the message.

Examples
use std::num::NonZeroU32;

use tokio_dbus::{Flags, Message, ObjectPath, SendBuf};

const PATH: &ObjectPath = ObjectPath::new_const(b"/org/freedesktop/DBus");

let mut send = SendBuf::new();

let m = send.method_call(PATH, "Hello");
assert_eq!(m.flags(), Flags::default());

let m2 = m.with_flags(Flags::NO_REPLY_EXPECTED);
assert_eq!(m2.flags(), Flags::NO_REPLY_EXPECTED);
source

pub fn with_flags(self, flags: Flags) -> Self

Modify the flags of the message.

Examples
use std::num::NonZeroU32;

use tokio_dbus::{Flags, Message, ObjectPath, SendBuf};

const PATH: &ObjectPath = ObjectPath::new_const(b"/org/freedesktop/DBus");

let mut send = SendBuf::new();

let m = send.method_call(PATH, "Hello");
assert_eq!(m.flags(), Flags::default());

let m2 = m.with_flags(Flags::NO_REPLY_EXPECTED);
assert_eq!(m2.flags(), Flags::NO_REPLY_EXPECTED);
source

pub fn interface(&self) -> Option<&'a str>

Get the interface of the message.

Examples
use std::num::NonZeroU32;

use tokio_dbus::{Message, ObjectPath, SendBuf};

const PATH: &ObjectPath = ObjectPath::new_const(b"/org/freedesktop/DBus");

let mut send = SendBuf::new();

let m = send.method_call(PATH, "Hello");
assert_eq!(m.interface(), None);

let m2 = m.with_interface("org.freedesktop.DBus");
assert_eq!(m2.interface(), Some("org.freedesktop.DBus"));
source

pub fn with_interface(self, interface: &'a str) -> Self

Modify the interface of the message.

Examples
use std::num::NonZeroU32;

use tokio_dbus::{Message, ObjectPath, SendBuf};

const PATH: &ObjectPath = ObjectPath::new_const(b"/org/freedesktop/DBus");

let mut send = SendBuf::new();

let m = send.method_call(PATH, "Hello");
assert_eq!(m.interface(), None);

let m2 = m.with_interface("org.freedesktop.DBus");
assert_eq!(m2.interface(), Some("org.freedesktop.DBus"));
source

pub fn destination(&self) -> Option<&'a str>

Get the destination of the message.

Examples
use std::num::NonZeroU32;

use tokio_dbus::{Message, ObjectPath, SendBuf};

const PATH: &ObjectPath = ObjectPath::new_const(b"/org/freedesktop/DBus");

let mut send = SendBuf::new();

let m = send.method_call(PATH, "Hello");
assert_eq!(m.destination(), None);

let m2 = m.with_destination(":1.131");
assert_eq!(m2.destination(), Some(":1.131"));
source

pub fn with_destination(self, destination: &'a str) -> Self

Modify the destination of the message.

Examples
use std::num::NonZeroU32;

use tokio_dbus::{Message, ObjectPath, SendBuf};

const PATH: &ObjectPath = ObjectPath::new_const(b"/org/freedesktop/DBus");

let mut send = SendBuf::new();

let m = send.method_call(PATH, "Hello");
assert_eq!(m.destination(), None);

let m2 = m.with_destination(":1.131");
assert_eq!(m2.destination(), Some(":1.131"));
source

pub fn sender(&self) -> Option<&'a str>

Get the sender of the message.

Examples
use std::num::NonZeroU32;

use tokio_dbus::{Message, ObjectPath, SendBuf};

const PATH: &ObjectPath = ObjectPath::new_const(b"/org/freedesktop/DBus");

let mut send = SendBuf::new();

let m = send.method_call(PATH, "Hello");
assert_eq!(m.destination(), None);

let m2 = m.with_sender(":1.131");
assert_eq!(m2.sender(), Some(":1.131"));
source

pub fn with_sender(self, sender: &'a str) -> Self

Modify the sender of the message.

Examples
use std::num::NonZeroU32;

use tokio_dbus::{Message, ObjectPath, SendBuf};

const PATH: &ObjectPath = ObjectPath::new_const(b"/org/freedesktop/DBus");

let mut send = SendBuf::new();

let m = send.method_call(PATH, "Hello");
assert_eq!(m.destination(), None);

let m2 = m.with_sender(":1.131");
assert_eq!(m2.sender(), Some(":1.131"));
source

pub fn signature(&self) -> &Signature

Get the signature of the message.

Examples
use std::num::NonZeroU32;

use tokio_dbus::{Message, ObjectPath, SendBuf, Signature};

const PATH: &ObjectPath = ObjectPath::new_const(b"/org/freedesktop/DBus");

let mut send = SendBuf::new();

let m = send.method_call(PATH, "Hello");
assert_eq!(m.signature(), Signature::EMPTY);

let m2 = m.with_signature(Signature::STRING);
assert_eq!(m2.signature(), Signature::STRING);
source

pub fn with_signature(self, signature: &'a Signature) -> Self

Modify the signature of the message.

Examples
use std::num::NonZeroU32;

use tokio_dbus::{Message, ObjectPath, Signature, SendBuf};

const PATH: &ObjectPath = ObjectPath::new_const(b"/org/freedesktop/DBus");

let mut send = SendBuf::new();

let m = send.method_call(PATH, "Hello");
assert_eq!(m.signature(), Signature::EMPTY);

let m2 = m.with_signature(Signature::STRING);
assert_eq!(m2.signature(), Signature::STRING);

Trait Implementations§

source§

impl<'a> Clone for Message<'a>

source§

fn clone(&self) -> Message<'a>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'a> Debug for Message<'a>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl PartialEq<Message<'_>> for OwnedMessage

source§

fn eq(&self, other: &Message<'_>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<OwnedMessage> for Message<'_>

source§

fn eq(&self, other: &OwnedMessage) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> PartialEq for Message<'a>

source§

fn eq(&self, other: &Message<'a>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> Eq for Message<'a>

source§

impl<'a> StructuralEq for Message<'a>

source§

impl<'a> StructuralPartialEq for Message<'a>

Auto Trait Implementations§

§

impl<'a> RefUnwindSafe for Message<'a>

§

impl<'a> !Send for Message<'a>

§

impl<'a> !Sync for Message<'a>

§

impl<'a> Unpin for Message<'a>

§

impl<'a> UnwindSafe for Message<'a>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.