tokio_dbus/body/
as_body.rs

1use crate::{Body, BodyBuf};
2
3mod sealed {
4    use crate::{Body, BodyBuf};
5    pub trait Sealed<'de> {}
6    impl<'de> Sealed<'de> for &Body<'de> {}
7    impl<'de> Sealed<'de> for Body<'de> {}
8    impl<'de> Sealed<'de> for &'de BodyBuf {}
9    impl<'de> Sealed<'de> for &'de mut BodyBuf {}
10}
11
12/// Trait for types which can be cheaply coerced into a [`Body`].
13///
14/// This is used in combination with [`Message::with_body`] to allow for
15/// convenient construction of a borrowed body.
16///
17/// [`Message::with_body`]: crate::Message::with_body
18///
19/// # Examples
20///
21/// ```
22/// use tokio_dbus::{BodyBuf, MessageKind, ObjectPath, SendBuf, Signature};
23///
24/// const PATH: &ObjectPath = ObjectPath::new_const(b"/org/freedesktop/DBus");
25///
26/// let mut send = SendBuf::new();
27/// let mut body = BodyBuf::new();
28///
29/// body.store("Hello World!");
30///
31/// let m = send.method_call(PATH, "Hello")
32///     .with_body(&body);
33///
34/// assert!(matches!(m.kind(), MessageKind::MethodCall { .. }));
35/// assert_eq!(m.signature(), Signature::STRING);
36/// ```
37pub trait AsBody<'de>: self::sealed::Sealed<'de> {
38    /// Coerce this type into a [`Body`].
39    #[allow(clippy::wrong_self_convention)]
40    fn as_body(self) -> Body<'de>;
41}
42
43/// Convert a reference to a [`Body`] into a [`Body`].
44///
45/// Since [`Body`] is cheap to clone, it doesn't hurt to provide this coercions.
46///
47/// # Examples
48///
49/// ```
50/// use tokio_dbus::{BodyBuf, MessageKind, ObjectPath, SendBuf, Signature};
51///
52/// const PATH: &ObjectPath = ObjectPath::new_const(b"/org/freedesktop/DBus");
53///
54/// let mut send = SendBuf::new();
55/// let mut body = BodyBuf::new();
56///
57/// body.store("Hello World!");
58///
59/// let m = send.method_call(PATH, "Hello")
60///     .with_body(&body.as_body());
61///
62/// assert!(matches!(m.kind(), MessageKind::MethodCall { .. }));
63/// assert_eq!(m.signature(), Signature::STRING);
64/// ```
65impl<'de> AsBody<'de> for &Body<'de> {
66    #[inline]
67    fn as_body(self) -> Body<'de> {
68        self.clone()
69    }
70}
71
72/// Convert a [`Body`] into a [`Body`].
73///
74/// # Examples
75///
76/// ```
77/// use tokio_dbus::{BodyBuf, MessageKind, ObjectPath, SendBuf, Signature};
78///
79/// const PATH: &ObjectPath = ObjectPath::new_const(b"/org/freedesktop/DBus");
80///
81/// let mut send = SendBuf::new();
82/// let mut body = BodyBuf::new();
83///
84/// body.store("Hello World!");
85///
86/// let m = send.method_call(PATH, "Hello")
87///     .with_body(body.as_body());
88///
89/// assert!(matches!(m.kind(), MessageKind::MethodCall { .. }));
90/// assert_eq!(m.signature(), Signature::STRING);
91/// ```
92impl<'de> AsBody<'de> for Body<'de> {
93    #[inline]
94    fn as_body(self) -> Body<'de> {
95        self
96    }
97}
98
99/// Convert a borrowed [`BodyBuf`] into a [`Body`].
100///
101/// # Examples
102///
103/// ```
104/// use tokio_dbus::{BodyBuf, MessageKind, ObjectPath, SendBuf, Signature};
105///
106/// const PATH: &ObjectPath = ObjectPath::new_const(b"/org/freedesktop/DBus");
107///
108/// let mut send = SendBuf::new();
109/// let mut body = BodyBuf::new();
110///
111/// body.store("Hello World!");
112///
113/// let m = send.method_call(PATH, "Hello")
114///     .with_body(&body);
115///
116/// assert!(matches!(m.kind(), MessageKind::MethodCall { .. }));
117/// assert_eq!(m.signature(), Signature::STRING);
118/// ```
119impl<'de> AsBody<'de> for &'de BodyBuf {
120    #[inline]
121    fn as_body(self) -> Body<'de> {
122        BodyBuf::as_body(self)
123    }
124}
125
126/// Convert a mutably borrowed [`BodyBuf`] into a [`Body`].
127///
128/// # Examples
129///
130/// ```
131/// use tokio_dbus::{BodyBuf, MessageKind, ObjectPath, SendBuf, Signature};
132///
133/// const PATH: &ObjectPath = ObjectPath::new_const(b"/org/freedesktop/DBus");
134///
135/// let mut send = SendBuf::new();
136/// let mut body = BodyBuf::new();
137///
138/// body.store("Hello World!");
139///
140/// let m = send.method_call(PATH, "Hello")
141///     .with_body(&mut body);
142///
143/// assert!(matches!(m.kind(), MessageKind::MethodCall { .. }));
144/// assert_eq!(m.signature(), Signature::STRING);
145/// ```
146impl<'de> AsBody<'de> for &'de mut BodyBuf {
147    #[inline]
148    fn as_body(self) -> Body<'de> {
149        BodyBuf::as_body(self)
150    }
151}