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
use std::io::{Result, Write};
use crate::fixed::FixedInt;
use crate::varint::VarInt;
#[cfg(feature = "tokio_async")]
use tokio::{io::AsyncWriteExt, prelude::*};
#[cfg(feature = "futures_async")]
use futures_util::{io::AsyncWriteExt, io::AsyncWrite};
pub trait VarIntWriter {
fn write_varint<VI: VarInt>(&mut self, n: VI) -> Result<usize>;
}
#[cfg(any(feature = "tokio_async", feature = "futures_async"))]
#[async_trait::async_trait]
pub trait VarIntAsyncWriter {
async fn write_varint_async<VI: VarInt + Send>(&mut self, n: VI) -> Result<usize>;
}
#[cfg(any(feature = "tokio_async", feature = "futures_async"))]
#[async_trait::async_trait]
impl<AW: AsyncWrite + Send + Unpin> VarIntAsyncWriter for AW {
async fn write_varint_async<VI: VarInt + Send>(&mut self, n: VI) -> Result<usize> {
let mut buf = [0 as u8; 10];
let b = n.encode_var(&mut buf);
self.write_all(&buf[0..b]).await?;
Ok(b)
}
}
impl<Inner: Write> VarIntWriter for Inner {
fn write_varint<VI: VarInt>(&mut self, n: VI) -> Result<usize> {
let mut buf = [0 as u8; 10];
let used = n.encode_var(&mut buf[..]);
self.write_all(&buf[0..used])?;
Ok(used)
}
}
pub trait FixedIntWriter {
fn write_fixedint<FI: FixedInt>(&mut self, n: FI) -> Result<usize>;
}
#[cfg(any(feature = "tokio_async", feature = "futures_async"))]
#[async_trait::async_trait]
pub trait FixedIntAsyncWriter {
async fn write_fixedint_async<FI: FixedInt + Send>(&mut self, n: FI) -> Result<usize>;
}
#[cfg(any(feature = "tokio_async", feature = "futures_async"))]
#[async_trait::async_trait]
impl<AW: AsyncWrite + Unpin + Send> FixedIntAsyncWriter for AW {
async fn write_fixedint_async<FI: FixedInt + Send>(&mut self, n: FI) -> Result<usize> {
let mut buf = [0 as u8; 8];
n.encode_fixed(&mut buf[0..FI::required_space()]);
self.write_all(&buf[0..FI::required_space()]).await?;
Ok(FI::REQUIRED_SPACE)
}
}
impl<W: Write> FixedIntWriter for W {
fn write_fixedint<FI: FixedInt>(&mut self, n: FI) -> Result<usize> {
let mut buf = [0 as u8; 8];
n.encode_fixed(&mut buf[0..FI::required_space()]);
self.write_all(&buf[0..FI::required_space()])?;
Ok(FI::REQUIRED_SPACE)
}
}