Skip to main content

tokio_dbus/body/
as_body.rs

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