1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use crate::error::Result;
use crate::{Frame, Signature, Storable};
pub(crate) mod sealed {
pub trait Sealed {}
}
/// Trait for types which can be written to.
pub trait WriteAligned {
/// Only write to the buffer without appending a signature.
fn write_only<T>(&mut self, value: &T)
where
T: ?Sized + Write;
/// Only store the specified value without appending its signature.
fn store<T>(&mut self, frame: T) -> Result<()>
where
T: Storable;
/// Only store the specified value without appending its signature.
fn store_frame<T>(&mut self, frame: T)
where
T: Frame;
/// Extend the buffer with a slice.
fn extend_from_slice(&mut self, bytes: &[u8]);
/// Extend the buffer with a slice ending with a NUL byte.
fn extend_from_slice_nul(&mut self, bytes: &[u8]);
}
/// Trait for types which can be written unaligned to.
pub trait WriteUnaligned {
/// Only store the specified value without appending its signature.
fn store<T>(&mut self, frame: T)
where
T: Frame;
/// Extend the buffer with a slice.
fn extend_from_slice(&mut self, bytes: &[u8]);
/// Extend the buffer with a slice ending with a NUL byte.
fn extend_from_slice_nul(&mut self, bytes: &[u8]);
}
/// A type who's reference can be written directly to a buffer.
///
/// These types are written using methods such as [`BodyBuf::store`].
///
/// [`BodyBuf::store`]: crate::BodyBuf::store
pub trait Write: self::sealed::Sealed {
/// The signature of the type.
#[doc(hidden)]
const SIGNATURE: &'static Signature;
/// Write `self` into `buf`.
#[doc(hidden)]
fn write_to<B>(&self, buf: &mut B)
where
B: ?Sized + WriteAligned;
/// Write `self` into `buf`.
#[doc(hidden)]
fn write_to_unaligned<B>(&self, buf: &mut B)
where
B: ?Sized + WriteUnaligned;
}
impl self::sealed::Sealed for [u8] {}
/// Write a byte array to the buffer.
///
/// # Examples
///
/// ```
/// use tokio_dbus::BodyBuf;
///
/// let mut buf = BodyBuf::new();
/// buf.store(&b"foo"[..]);
///
/// assert_eq!(buf.signature(), "ay");
/// assert_eq!(buf.get(), &[3, 0, 0, 0, 102, 111, 111]);
/// # Ok::<_, tokio_dbus::Error>(())
/// ```
impl Write for [u8] {
const SIGNATURE: &'static Signature = Signature::new_const(b"ay");
#[inline]
fn write_to<B>(&self, buf: &mut B)
where
B: ?Sized + WriteAligned,
{
buf.store_frame(self.len() as u32);
buf.extend_from_slice(self);
}
#[inline]
fn write_to_unaligned<B>(&self, buf: &mut B)
where
B: ?Sized + WriteUnaligned,
{
buf.store(self.len() as u32);
buf.extend_from_slice(self);
}
}
impl_traits_for_write!([u8], &b"abcd"[..], "qay");
impl self::sealed::Sealed for str {}
/// Write a length-prefixed string to the buffer.
///
/// # Examples
///
/// ```
/// use tokio_dbus::{BodyBuf, Signature};
///
/// let mut buf = BodyBuf::new();
/// buf.store("foo");
///
/// assert_eq!(buf.signature(), Signature::STRING);
/// assert_eq!(buf.get(), &[3, 0, 0, 0, 102, 111, 111, 0])
/// ```
impl Write for str {
const SIGNATURE: &'static Signature = Signature::STRING;
#[inline]
fn write_to<B>(&self, buf: &mut B)
where
B: ?Sized + WriteAligned,
{
buf.store_frame(self.len() as u32);
buf.extend_from_slice_nul(self.as_bytes());
}
#[inline]
fn write_to_unaligned<B>(&self, buf: &mut B)
where
B: ?Sized + WriteUnaligned,
{
buf.store(self.len() as u32);
buf.extend_from_slice_nul(self.as_bytes());
}
}
impl_traits_for_write!(str, "Hello World", "qs");