tokio_dbus/
storable.rs

1use crate::signature::SignatureBuilder;
2use crate::{BodyBuf, Signature};
3
4pub(crate) mod sealed {
5    pub trait Sealed {}
6}
7
8/// Trait used for types which can be stored with a `store()` call.
9///
10/// # Examples
11///
12/// ```
13/// use tokio_dbus::BodyBuf;
14///
15/// let mut body = BodyBuf::new();
16///
17/// body.store(10u16)?;
18/// body.store("Hello World")?;
19///
20/// assert_eq!(body.signature(), "qs");
21/// # Ok::<_, tokio_dbus::Error>(())
22/// ```
23pub trait Storable: self::sealed::Sealed {
24    /// Store a frame into a buffer body.
25    #[doc(hidden)]
26    fn store_to(self, buf: &mut BodyBuf);
27
28    /// Write a signature.
29    #[doc(hidden)]
30    fn write_signature(builder: &mut SignatureBuilder) -> bool;
31}
32
33impl self::sealed::Sealed for String {}
34
35/// [`Storable`] implementation for [`String`].
36///
37/// # Examples
38///
39/// ```
40/// use tokio_dbus::BodyBuf;
41///
42/// let mut body = BodyBuf::new();
43///
44/// body.store(10u16)?;
45/// body.store(String::from("Hello World"))?;
46///
47/// assert_eq!(body.signature(), "qs");
48/// # Ok::<_, tokio_dbus::Error>(())
49/// ```
50impl Storable for String {
51    #[inline]
52    fn store_to(self, buf: &mut BodyBuf) {
53        self.as_str().store_to(buf);
54    }
55
56    #[inline]
57    fn write_signature(builder: &mut SignatureBuilder) -> bool {
58        builder.extend_from_signature(Signature::STRING)
59    }
60}