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>
impl<'a> Body<'a>
sourcepub fn endianness(&self) -> Endianness
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);
sourcepub fn with_endianness(self, endianness: Endianness) -> Self
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);
sourcepub fn signature(&self) -> &'a Signature
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");
sourcepub fn get(&self) -> &'a [u8] ⓘ
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(())
}
sourcepub fn is_empty(&self) -> bool
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());
sourcepub fn len(&self) -> usize
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);
sourcepub fn read<T>(&mut self) -> Result<&'a T>
pub fn read<T>(&mut self) -> Result<&'a T>
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(())
}
sourcepub fn read_until(&mut self, len: usize) -> Body<'a>
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(())
}
sourcepub fn load_array<E>(&mut self) -> Result<LoadArray<'a, E>>where
E: Marker,
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);
sourcepub fn load_struct<E>(&mut self) -> Result<E::Return<'a>>where
E: Fields,
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");
sourcepub fn load<T>(&mut self) -> Result<T>where
T: Frame,
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>
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§impl<'de> AsBody<'de> for Body<'de>
impl<'de> AsBody<'de> for Body<'de>
§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);