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

source

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");
source

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);
source

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(), "");
source

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");
source

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);
source

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());
source

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);
source

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]);
source

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());
source

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");
source

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");
source

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]);
source

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]);
source

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

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§

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

Coerce this type into a Body.
source§

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);
source§

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

Coerce this type into a Body.
source§

impl Clone for BodyBuf

source§

fn clone(&self) -> BodyBuf

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 BodyBuf

source§

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

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

impl Default for BodyBuf

source§

fn default() -> Self

Returns the “default value” for a type. 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 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 PartialEq for BodyBuf

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 Eq for BodyBuf

source§

impl StructuralPartialEq for BodyBuf

Auto Trait Implementations§

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.