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
use tokio::io::{AsyncWrite, AsyncWriteExt};
use super::AsyncInlinable;
#[async_trait::async_trait]
pub trait AsyncInlinableWrite: AsyncWrite + Send + Unpin {
#[inline]
async fn write_len_with_width<const N: usize>(&mut self, len: usize) -> std::io::Result<usize> {
self.write_all(&len.to_le_bytes()[0..N]).await?;
Ok(N)
}
#[inline]
async fn write_inlined_bytes<const N: usize>(
&mut self,
bytes: &[u8],
) -> std::io::Result<usize> {
assert_eq!(bytes.len() >> N * 8, 0);
let len = &bytes.len().to_le_bytes()[0..N];
self.write_all(len).await?;
self.write_all(bytes).await?;
Ok(N + bytes.len())
}
#[inline]
async fn write_inlined_str<const N: usize>(&mut self, s: &str) -> std::io::Result<usize> {
self.write_inlined_bytes::<N>(s.as_bytes()).await
}
#[inline]
async fn write_inlinable<T: AsyncInlinable + Sync>(
&mut self,
value: &T,
) -> std::io::Result<usize>
where
Self: Sized,
{
T::write_inlined(value, self).await
}
}
impl<T> AsyncInlinableWrite for T where T: AsyncWrite + Send + Unpin {}