Struct tokio_dbus::BodyBuf
source · pub struct BodyBuf { /* private fields */ }
Expand description
A buffer that can be used to write a body.
§Examples
use tokio_dbus::BodyBuf;
let mut body = BodyBuf::new();
body.store(10u16)?;
body.store(10u32)?;
assert_eq!(body.signature(), "qu");
Implementations§
source§impl BodyBuf
impl BodyBuf
sourcepub fn new() -> Self
pub fn new() -> Self
Construct a new empty body buffer.
§Examples
use tokio_dbus::BodyBuf;
let mut body = BodyBuf::new();
body.store(10u16)?;
body.store(10u32)?;
assert_eq!(body.signature(), "qu");
sourcepub fn with_endianness(endianness: Endianness) -> Self
pub fn with_endianness(endianness: Endianness) -> Self
Construct a new buffer with the specified endianness.
§Examples
use tokio_dbus::{BodyBuf, Endianness};
let buf = BodyBuf::with_endianness(Endianness::LITTLE);
sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clear the buffer.
§Examples
use tokio_dbus::BodyBuf;
let mut body = BodyBuf::new();
body.store(10u16)?;
body.store(10u32)?;
assert_eq!(body.signature(), "qu");
body.clear();
assert_eq!(body.signature(), "");
sourcepub fn signature(&self) -> &Signature
pub fn signature(&self) -> &Signature
Get the signature of the buffer.
§Examples
use tokio_dbus::BodyBuf;
let mut body = BodyBuf::new();
body.store(10u16)?;
body.store(10u32)?;
assert_eq!(body.signature(), "qu");
sourcepub fn endianness(&self) -> Endianness
pub fn endianness(&self) -> Endianness
Get the endianness of the buffer.
§Examples
use tokio_dbus::{BodyBuf, Endianness};
let body = BodyBuf::new();
assert_eq!(body.endianness(), Endianness::NATIVE);
let body = BodyBuf::with_endianness(Endianness::BIG);
assert_eq!(body.endianness(), Endianness::BIG);
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Test if the buffer is empty.
§Examples
use tokio_dbus::{BodyBuf, Endianness};
let mut body = BodyBuf::with_endianness(Endianness::LITTLE);
assert!(body.is_empty());
body.store(10u16)?;
body.store(10u32)?;
assert!(!body.is_empty());
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Remaining data to be read from the buffer.
§Examples
use tokio_dbus::{BodyBuf, Endianness};
let mut body = BodyBuf::with_endianness(Endianness::LITTLE);
assert!(body.is_empty());
body.store(10u16)?;
body.store(10u32)?;
assert_eq!(body.len(), 8);
sourcepub fn get(&self) -> &[u8] ⓘ
pub fn get(&self) -> &[u8] ⓘ
Get a slice out of the buffer that has ben written to.
§Examples
use tokio_dbus::{BodyBuf, Endianness};
let mut body = BodyBuf::with_endianness(Endianness::LITTLE);
body.store(10u16)?;
body.store(10u32)?;
assert_eq!(body.signature(), "qu");
assert_eq!(body.get(), &[10, 0, 0, 0, 10, 0, 0, 0]);
sourcepub fn as_body(&self) -> Body<'_>
pub fn as_body(&self) -> Body<'_>
Access a Body
over the entire contents of the buffer.
This is a reader-like abstraction that has a read cursor and endianness, allowing convenient read access over the contents of the buffer.
It is also used in combination with Message::with_body
to set the
message of a body.
§Examples
use tokio_dbus::{ty, BodyBuf, Endianness};
let mut buf = BodyBuf::with_endianness(Endianness::LITTLE);
buf.store_struct::<(u16, u32)>()?
.store(20u16)
.store(30u32)
.finish();
assert_eq!(buf.signature(), "(qu)");
let mut buf = buf.as_body();
let (a, b) = buf.load_struct::<(u16, u32)>()?;
assert_eq!(a, 20u16);
assert_eq!(b, 30u32);
assert!(buf.is_empty());
sourcepub fn store<T>(&mut self, frame: T) -> Result<()>where
T: Storable,
pub fn store<T>(&mut self, frame: T) -> Result<()>where
T: Storable,
Store a Frame
of type T
in the buffer and add its signature.
This both allocates enough space for the frame and ensures that the
buffer is aligned per the requirements of the frame. /// Write a type to the buffer and update the buffer’s signature to indicate
that the type T
is stored.
§Examples
use tokio_dbus::{BodyBuf, MessageKind, ObjectPath, SendBuf};
const PATH: &ObjectPath = ObjectPath::new_const(b"/org/freedesktop/DBus");
let mut send = SendBuf::new();
let mut body = BodyBuf::new();
body.store(10f64)?;
body.store(20u32)?;
let m = send.method_call(PATH, "Hello")
.with_body(&body);
assert!(matches!(m.kind(), MessageKind::MethodCall { .. }));
assert_eq!(m.signature(), "du");
Write unsized types:
use tokio_dbus::{BodyBuf, MessageKind, ObjectPath, SendBuf};
const PATH: &ObjectPath = ObjectPath::new_const(b"/org/freedesktop/DBus");
let mut send = SendBuf::new();
let mut body = BodyBuf::new();
body.store("Hello World!")?;
body.store(PATH)?;
let m = send.method_call(PATH, "Hello")
.with_body(&body);
assert!(matches!(m.kind(), MessageKind::MethodCall { .. }));
assert_eq!(m.signature(), "so");
sourcepub fn arguments<T>(&mut self, value: T) -> Result<()>where
T: Arguments,
pub fn arguments<T>(&mut self, value: T) -> Result<()>where
T: Arguments,
Extend the body with multiple arguments.
This can be a more convenient variant compared with subsequent calls to
type-dependent calls to BodyBuf::store
.
§Examples
use tokio_dbus::{BodyBuf, MessageKind, ObjectPath, SendBuf};
const PATH: &ObjectPath = ObjectPath::new_const(b"/org/freedesktop/DBus");
let mut send = SendBuf::new();
let mut body = BodyBuf::new();
body.arguments(("Hello World!", PATH, 10u32));
let m = send.method_call(PATH, "Hello")
.with_body(&body);
assert!(matches!(m.kind(), MessageKind::MethodCall { .. }));
assert_eq!(m.signature(), "sou");
sourcepub fn store_array<E>(&mut self) -> Result<StoreArray<'_, E>>where
E: Marker,
pub fn store_array<E>(&mut self) -> Result<StoreArray<'_, E>>where
E: Marker,
Write an array into the buffer.
§Examples
use tokio_dbus::{BodyBuf, Endianness};
let mut buf = BodyBuf::with_endianness(Endianness::LITTLE);
let mut array = buf.store_array::<u32>()?;
array.store(1u32);
array.finish();
assert_eq!(buf.signature(), b"au");
assert_eq!(buf.get(), &[4, 0, 0, 0, 1, 0, 0, 0]);
Writing an empty array still enforces element alignment:
use tokio_dbus::{BodyBuf, Endianness};
let mut buf = BodyBuf::with_endianness(Endianness::LITTLE);
let mut array = buf.store_array::<u64>()?;
array.finish();
assert_eq!(buf.signature(), b"at");
assert_eq!(buf.get(), &[0, 0, 0, 0, 0, 0, 0, 0]);
sourcepub fn write_slice(&mut self, data: &[u8]) -> Result<()>
pub fn write_slice(&mut self, data: &[u8]) -> Result<()>
Write a slice as an byte array.
§Examples
use tokio_dbus::{BodyBuf, Endianness};
let mut buf = BodyBuf::with_endianness(Endianness::LITTLE);
buf.write_slice(&[1, 2, 3, 4])?;
assert_eq!(buf.signature(), "ay");
assert_eq!(buf.get(), &[4, 0, 0, 0, 1, 2, 3, 4]);
sourcepub fn store_struct<E>(&mut self) -> Result<StoreStruct<'_, E>>where
E: Fields,
pub fn store_struct<E>(&mut self) -> Result<StoreStruct<'_, E>>where
E: Fields,
Write a struct into the buffer.
§Examples
use tokio_dbus::{BodyBuf, Endianness};
use tokio_dbus::ty;
let mut buf = BodyBuf::with_endianness(Endianness::LITTLE);
buf.store(10u8);
buf.store_struct::<(u16, u32, ty::Array<u8>, ty::Str)>()?
.store(10u16)
.store(10u32)
.store_array(|w| {
w.store(1u8);
w.store(2u8);
w.store(3u8);
})
.store("Hello World")
.finish();
assert_eq!(buf.signature(), b"y(quays)");
assert_eq!(buf.get(), &[10, 0, 0, 0, 0, 0, 0, 0, 10, 0, 0, 0, 10, 0, 0, 0, 3, 0, 0, 0, 1, 2, 3, 0, 11, 0, 0, 0, 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 0]);
Trait Implementations§
source§impl<'de> AsBody<'de> for &'de BodyBuf
impl<'de> AsBody<'de> for &'de BodyBuf
Convert a borrowed BodyBuf
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);
assert!(matches!(m.kind(), MessageKind::MethodCall { .. }));
assert_eq!(m.signature(), Signature::STRING);
source§impl<'de> AsBody<'de> for &'de mut BodyBuf
impl<'de> AsBody<'de> for &'de mut BodyBuf
Convert a mutably borrowed BodyBuf
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(&mut body);
assert!(matches!(m.kind(), MessageKind::MethodCall { .. }));
assert_eq!(m.signature(), Signature::STRING);