Struct tokio_dbus::Body

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

A read-only view into a buffer suitable for use as a body in a Message.

§Examples

use tokio_dbus::{Result, Body};

fn read(buf: &mut Body<'_>) -> Result<()> {
    assert_eq!(buf.load::<u32>()?, 7u32);
    assert_eq!(buf.load::<u8>()?, b'f');
    assert_eq!(buf.load::<u8>()?, b'o');
    assert_eq!(buf.get(), &[b'o', b' ', b'b', b'a', b'r', 0]);
    Ok(())
}

Implementations§

source§

impl<'a> Body<'a>

source

pub fn endianness(&self) -> Endianness

Get the endianness of the buffer.

§Examples
use tokio_dbus::{Body, BodyBuf, Endianness};

let buf = BodyBuf::new();

let buf: Body<'_> = buf.as_body();
assert_eq!(buf.endianness(), Endianness::NATIVE);

let buf = buf.with_endianness(Endianness::BIG);
assert_eq!(buf.endianness(), Endianness::BIG);
source

pub fn with_endianness(self, endianness: Endianness) -> Self

Adjust endianness of buffer.

§Examples
use tokio_dbus::{Body, BodyBuf, Endianness};

let buf = BodyBuf::new();

let buf: Body<'_> = buf.as_body();
assert_eq!(buf.endianness(), Endianness::NATIVE);

let buf = buf.with_endianness(Endianness::BIG);
assert_eq!(buf.endianness(), Endianness::BIG);
source

pub fn signature(&self) -> &'a Signature

Get the signature of the buffer.

§Examples
use tokio_dbus::{Body, BodyBuf};

let mut buf = BodyBuf::new();

buf.store(10u16)?;
buf.store(10u32)?;

let buf: Body<'_> = buf.as_body();

assert_eq!(buf.signature(), "qu");
source

pub fn get(&self) -> &'a [u8]

Get a slice out of the buffer that has ben written to.

§Examples
use tokio_dbus::{Result, Body};

fn read(buf: &mut Body<'_>) -> Result<()> {
    assert_eq!(buf.load::<u32>()?, 7u32);
    assert_eq!(buf.load::<u8>()?, b'f');
    assert_eq!(buf.load::<u8>()?, b'o');
    assert_eq!(buf.get(), &[b'o', b' ', b'b', b'a', b'r', 0]);
    Ok(())
}
source

pub fn is_empty(&self) -> bool

Test if the buffer is empty.

§Examples
use tokio_dbus::{Body, BodyBuf, Endianness};

let mut buf = BodyBuf::with_endianness(Endianness::LITTLE);
let b: Body<'_> = buf.as_body();
assert!(b.is_empty());

buf.store(10u16)?;
buf.store(10u32)?;

let b: Body<'_> = buf.as_body();
assert!(!b.is_empty());
source

pub fn len(&self) -> usize

Remaining data to be read from the buffer.

§Examples
use tokio_dbus::{Body, BodyBuf, Endianness};

let mut buf = BodyBuf::with_endianness(Endianness::LITTLE);
assert!(buf.is_empty());

buf.store(10u16)?;
buf.store(10u32)?;

let b: Body<'_> = buf.as_body();
assert_eq!(b.len(), 8);
source

pub fn read<T>(&mut self) -> Result<&'a T>
where T: ?Sized + Read,

Read a reference from the buffer.

This is possible for unaligned types such as str and [u8] which implement Read.

§Examples
use tokio_dbus::{Result, Body};

fn read(buf: &mut Body<'_>) -> Result<()> {
    assert_eq!(buf.load::<u32>()?, 4);
    assert_eq!(buf.load::<u8>()?, 1);
    assert_eq!(buf.load::<u8>()?, 2);
    assert!(buf.load::<u8>().is_err());
    assert!(buf.is_empty());
    Ok(())
}
source

pub fn read_until(&mut self, len: usize) -> Body<'a>

Read len bytes from the buffer and make accessible through another Body instance constituting that sub-slice.

§Panics

This panics if len is larger than len().

§Examples
use tokio_dbus::{Result, Body};

fn read(buf: &mut Body<'_>) -> Result<()> {
    let mut read_buf = buf.read_until(6);
    assert_eq!(read_buf.load::<u32>()?, 4);

    let mut read_buf2 = read_buf.read_until(2);
    assert_eq!(read_buf2.load::<u8>()?, 1);
    assert_eq!(read_buf2.load::<u8>()?, 2);

    assert!(read_buf.is_empty());
    assert!(read_buf2.is_empty());

    assert_eq!(buf.get(), &[3, 4, 0]);
    Ok(())
}
source

pub fn load_array<E>(&mut self) -> Result<LoadArray<'a, E>>
where E: Marker,

Read an array from the buffer.

§Examples
use tokio_dbus::{ty, BodyBuf, Endianness};

let mut buf = BodyBuf::with_endianness(Endianness::LITTLE);
let mut array = buf.store_array::<u32>()?;
array.store(10u32);
array.store(20u32);
array.store(30u32);
array.finish();

let mut array = buf.store_array::<ty::Array<ty::Str>>()?;
let mut inner = array.store_array();
inner.store("foo");
inner.store("bar");
inner.store("baz");
inner.finish();
array.finish();

assert_eq!(buf.signature(), b"auaas");

let mut buf = buf.as_body();
let mut array = buf.load_array::<u32>()?;
assert_eq!(array.load()?, Some(10));
assert_eq!(array.load()?, Some(20));
assert_eq!(array.load()?, Some(30));
assert_eq!(array.load()?, None);

let mut array = buf.load_array::<ty::Array<ty::Str>>()?;

let Some(mut inner) = array.load_array()? else {
    panic!("Missing inner array");
};

assert_eq!(inner.read()?, Some("foo"));
assert_eq!(inner.read()?, Some("bar"));
assert_eq!(inner.read()?, Some("baz"));
assert_eq!(inner.read()?, None);
source

pub fn load_struct<E>(&mut self) -> Result<E::Return<'a>>
where E: Fields,

Read a struct from the buffer.

§Examples
use tokio_dbus::{ty, BodyBuf, Endianness};

let mut buf = BodyBuf::with_endianness(Endianness::LITTLE);
buf.store(10u8);

buf.store_struct::<(u16, u32, ty::Array<u8>, ty::Str)>()?
    .store(20u16)
    .store(30u32)
    .store_array(|w| {
        w.store(1u8);
        w.store(2u8);
        w.store(3u8);
    })
    .store("Hello World")
    .finish();

assert_eq!(buf.signature(), "y(quays)");

let mut buf = buf.as_body();
assert_eq!(buf.load::<u8>()?, 10u8);

let (a, b, mut array, string) = buf.load_struct::<(u16, u32, ty::Array<u8>, ty::Str)>()?;
assert_eq!(a, 20u16);
assert_eq!(b, 30u32);

assert_eq!(array.load()?, Some(1));
assert_eq!(array.load()?, Some(2));
assert_eq!(array.load()?, Some(3));
assert_eq!(array.load()?, None);

assert_eq!(string, "Hello World");
source

pub fn load<T>(&mut self) -> Result<T>
where T: Frame,

Load a frame of the given type.

This advances the read cursor of the buffer by the alignment and size of the type. The return value has been endian-adjusted as per endianness().

§Error

Errors if the underlying buffer does not have enough space to represent the type T.

§Examples
use tokio_dbus::{Result, Body};

fn read(buf: &mut Body<'_>) -> Result<()> {
    assert_eq!(buf.load::<u32>()?, 7u32);
    assert_eq!(buf.load::<u8>()?, b'f');
    assert_eq!(buf.load::<u8>()?, b'o');
    assert_eq!(buf.get(), &[b'o', b' ', b'b', b'a', b'r', 0]);
    Ok(())
}

Trait Implementations§

source§

impl<'de> AsBody<'de> for &Body<'de>

Convert a reference to a Body into a Body.

Since Body is cheap to clone, it doesn’t hurt to provide this coercions.

§Examples

use tokio_dbus::{BodyBuf, 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("Hello World!");

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

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

fn as_body(self) -> Body<'de>

Coerce this type into a Body.
source§

impl<'de> AsBody<'de> for Body<'de>

Convert a Body into a Body.

§Examples

use tokio_dbus::{BodyBuf, 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("Hello World!");

let m = send.method_call(PATH, "Hello")
    .with_body(body.as_body());

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

fn as_body(self) -> Body<'de>

Coerce this type into a Body.
source§

impl Clone for Body<'_>

source§

fn clone(&self) -> Self

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 Debug for Body<'_>

source§

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

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

impl From<Body<'_>> for BodyBuf

Construct an aligned buffer from a read buffer.

source§

fn from(buf: Body<'_>) -> Self

Converts to this type from the input type.
source§

impl<'a, 'b> PartialEq<Body<'a>> for Body<'b>

source§

fn eq(&self, other: &Body<'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 PartialEq<BodyBuf> for Body<'_>

source§

fn eq(&self, other: &BodyBuf) -> 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 Body<'a>

source§

impl Send for Body<'_>

source§

impl Sync for Body<'_>

Auto Trait Implementations§

§

impl<'a> RefUnwindSafe for Body<'a>

§

impl<'a> Unpin for Body<'a>

§

impl<'a> UnwindSafe for Body<'a>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where 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 T
where 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 T
where 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 T
where 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 T
where 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.